Codeigniter 2.1 send data from one controller to another - php

I have function like this:
function gi_insert()
{
if(!IS_AJAX){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags');
$this->form_validation->set_rules('body', 'Tekst', 'trim|required|strip_tags');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|strip_tags|valid_email');
$this->form_validation->set_rules('tag', 'Tagovi', 'trim|required|strip_tags');
if(isset($_POST['category_new'])){
$this->form_validation->set_rules('category_new', 'Kategorija', 'trim|strip_tags');
}
$p = $this->uri->segment(3);
if ($this->form_validation->run() == FALSE)
{
$errors = validation_errors();
redirect("admin/create/$p", 'location');
}
else
{
$this->gi->gi_insert();
redirect('admin/pregled/' . $this->uri->segment(3));
}
} else
{
$this->gi->gi_insert();
}
}
How can I send validation errors to admin/create controller? At the moment, this is working, but I don't get error report (page where errors should appear is containig
<?php echo validation_errors(); ?>
)

validation_errors() only works if the $_POST data is still set, since you redirect to admin/create, $_POST is empty and the function no longer works, returning nothing.
You could send the errors with a piece of flashdata to the page.

Related

Unable to access an error message corresponding to your field name in CI3

I have checked all the possible things that might go wrong with my code but still it showing me this error. Please help .
Unable to access an error message corresponding to your field name Username.(check_username)
And this is my code below:
public function index()
{
if ( ! file_exists(APPPATH.'/views/login.php'))
{
/* Whoops, we don't have a page for that! */
show_404();
}
$this->load->helper('security');
$this->load->library('form_validation'); // Including Validation Library.
$this->form_validation->set_error_delimiters('<div class="error">', '</div>'); // Displaying Errors in Div
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username'); // Validation for Username Field
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean'); // Validation for Password field.
if ($this->form_validation->run() == FALSE) {
$this->load->view('login');
}
function check_username($username)
{
if ($username == 'test') {
$this->form_validation->set_message('check_username','already exists.');
return false;
} else {
return TRUE;
}
}
}
Do $config['global_xss_filtering'] = TRUE; in config file.
in Codeigniter-3 xss_filtering is not a part of form_validation.
You have to add the validation language in the system folder. Check the video
https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=6&cad=rja&uact=8&sqi=2&ved=0ahUKEwi71dGkn73MAhVBQI4KHeZJC2gQtwIIPTAF&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D56EDSocDhjk&usg=AFQjCNE_nhBpNwUOz9nkrcRYYgL50p34Kw&sig2=PSUISbtldDaWWdTyKJ6CPw&bvm=bv.121070826,d.c2E

validation error in Codeigniter

May I know why my insert function will display the validation message first even i do not insert any data yet.once I enter the insert page,it display the validation message.i have try to move the form validation to the below of the if else statement,it do not show the validation message but it cannot submit the data to the database. below is my controller function.
public function Insert_Result($Course_ID=null)
{
$this-load-helper('form');
$this-load-library('form_validation');
/* Model */
$this-load-model('ResultEvaluation');
/* Session */
$session_data = $this-session-userdata('logged_in');
$data['Ins_ID'] = $session_data['Ins_ID'];
$this-session-set_userdata($data);
/* Form Validation*/
//$this-form_validation-set_rules('Course_ID', 'Course_ID', 'required');
$this-form_validation-set_rules('Matric_No', 'Matric_No','required');
$this-form_validation-set_rules('Student_Name', 'Student_Name', 'required');
$this-form_validation-set_rules('Result_Mark_1', 'Result_Mark_1', 'required');
$this-form_validation-set_rules('Result_Mark_2', 'Result_Mark_2', 'required');
$this-form_validation-set_rules('Result_Mark_3', 'Result_Mark_3', 'required');
$this-form_validation-set_rules('Result_Mark_4', 'Result_Mark_4', 'required');
$this-form_validation-set_rules('Result_Mark_5', 'Result_Mark_5', 'required');
if ($this-form_validation-run() === FALSE)
{
$data['results'] = $this-ResultEvaluation-get_record();
$data['query'] = $this-ResultEvaluation-view($Course_ID);
$this-load-view('templates/header');
$this-load-view('Insert_Result', $data);
$this-load-view('templates/footer');
}
else
{
$my_action = $this-input-post('submit');
if ($my_action == 'Submit')
{
$this-ResultEvaluation-insert_record($Course_ID);
redirect('Result_Evaluation/Student_Result_List/'.$Course_ID,
'refresh');
}
}
$my_action = $this-input-post('submit');
if ($my_action == 'Cancel')
{
redirect('Result_Evaluation/Student_Result_List/'.$Course_ID,
'refresh');
}
}
I think this should help
public function index(){
$this->load->library(array('form_validation'));
$this->form_validation->set_rules('field_name', 'Text','trim|required');
if($this->form_validation->run()==TRUE){
// do something
}else{
// validation error
redirect("to/your/controller");
break;
}
}
for more information visit : http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html

Display flashdata message in codeigniter callback function

I've this registration function and the related callback to check if an username already exists in the database. It works all perfectly, but I have problems with flashdata message
Here's the code:
/*
* Checks registration
*/
public function verify(){
$this->form_validation->set_rules('nome', 'Nome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('cognome', 'Cognome', 'trim|required|min_length[2]|xss_clean');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|xss_clean|callback_check_username');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password_conf', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('error_reg', 'Something goes wrong, please check your registration form');
redirect('registration');
}
else
{
$this->registration_m->add_user();
$this->session->set_flashdata('success_reg', 'Registration Successful!');
redirect('registration');
}
}
public function check_username($username){
$result = $this->registration_m->check_username_m($username);
if ($result) {
$this->session->set_flashdata('username', 'Username già in uso, riprovare.');
return false;
} else {
return true;
}
}
As you can see there's a redirect on the function verify(), and the flashdata before is correctly shown in case of errors.
My Question is: is there a way to show (in case of error) the flashdata inside the callback function without changing the logic of this piece of code?
Hope I was clear at all, cheers.
Modify your code as below
public function check_username($username){
$result = $this->registration_m->check_username_m($username);
if ($result) {
$this->form_validation->set_message('check_username', 'The %s field can not be the empty');
return false;
} else {
return true;
}
}
See differences in code
$this->session->set_flashdata('username', 'Username già in uso, riprovare.');
$this->form_validation->set_message('check_username', 'The %s field can not be the empty');

Validating Forms

I have a form that I submit with jQuery ajax and have it being sent to a controller function called submit to validate and do any other tasks I need to with the form data. I'm trying to find out why my form validation library isn't showing an error when the username doesn't contain only lowercase letters and numbers.
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
POST Value after form submission:
username TestingUSER
EDIT:
As far as I know it gets to the php server side properly.
PHP:
public function submit()
{
$output_status = 'Notice';
$output_title = 'Not Processed';
$output_message = 'The request was unprocessed!';
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|strtolower');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
$this->form_validation->set_rules('remember', 'Remember Me', 'trim|xss_clean|integer');
if ($this->form_validation->run() == TRUE)
{
}
else
{
$output_status = 'Error';
$output_title = 'Form Not Validated';
$output_message = validation_errors();
}
echo json_encode(array('output_status' => $output_status, 'output_title' => $output_title, 'output_message' => $output_message));
}
EDIT 2:
Based off of Sheikh answer. I am getting a response back that says "Unable to access an error message corresponding to your field name." It does say Form Not Validated for the title so the message isn't working.
public function check_username($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-z]#', $str))
{
return TRUE;
}
$this->form_validation->set_message('username', 'This is not have an accepted value!');
return FALSE;
}
EDIT 3:
What I'm wanting to do is have it report back that there there are validation errors but not the specific errors in the pnotify response. However I do want it to display the specific errors under the form elements.
jQuery Code:
http://pastebin.com/1KehMJkh
Login Form:
http://pastebin.com/EfpBfbfN
I think you can use a callback function in your controller
public function check_username($str)
{
if (preg_match('#[a-z0-9]#', $str)) {
return TRUE;
}
$this->form_validation->set_message('check_username', 'This is not have an accepted value!');
return FALSE;
}
Validation rules for username
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_check_username');
You may like this too.

Form Validation in CodeIgniter?

I am using form validation of CodeIgniter, it works fine but when, form validation fails, it does not display the validation errors, by using
<?php echo validation_errors();?>
I am using
function insertProduct(){
$this->load->library('form_validation');
$this->form_validation->set_rules('pname','ProductName','trimirequired');
if($this->form_validation->run()){
$this->addProduct();
}
else{
$this->load->model('inventory/stock');
}
In your view you should have something like (this example shows errors individually);
<?php echo form_error('p_name'); ?>
<label for="p_name">Product Name</label>
<input type="text" id="p_name" name="p_name" value="<?php echo set_value('p_name'); ?>" />
You need to tell the method in your controller to render a view on success/failure of the form validation.
If you change your insertProduct method to the following, it 'should' solve your issue.
function insertProduct(){
$this->load->library('form_validation');
$this->form_validation->set_rules('pname','ProductName','trimirequired');
if($this->form_validation->run()){
$this->addProduct();
$this->load->view('{name_of_your_view}');
} else{
$this->load->model('inventory/stock');
$this->load->view('{name_of_your_view}');
}
}
Where 'name_of_your_view' is the view that you've placed the validation_errors() code in.
This example from the CodeIgniter tutorial pages explains how to validated submitted data to display validation errors at the header of the form like you might expect:
http://codeigniter.com/user_guide/tutorial/create_news_items.html
The example code for the creation function looks like this:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
As others have said, though, you need to add a view to handle success and return them to the form to display errors on failure.
We can change the line containing following code:
$this->form_validation->set_rules('pname','ProductName','trimirequired');
to:
$this->form_validation->set_rules('pname','ProductName','trim|required');
if($this->form_validation->run($this) == false)
{
$this->addProduct();
}

Categories