Codeigniter validation_errors () always returns empty

I have a simple form adding the following validation rules to my db. The "form" is in autoload.php.

Whenever any of the validation rules is violated, the page does not display error output, when none is broken, the form passes as expected.

here is my controller function

public function add() { $this->load->library('form_validation'); $this->load->library('layout'); // field name, error message, validation rules $this->form_validation->set_rules('type', 'Type', 'required|xss_clean'); $this->form_validation->set_rules('title_type', 'Title type', 'required|xss_clean'); $this->form_validation->set_rules('first_name', 'First name', 'trim|required|min_length[1]|max_length[150]|xss_clean'); $this->form_validation->set_rules('surname', 'Surname', 'trim|required|min_length[1]|max_length[150]|xss_clean'); $this->form_validation->set_rules('job_title', 'Job title', 'trim|min_length[5]|max_length[300]required|xss_clean'); $this->form_validation->set_rules('office_number', 'Office number', 'trim|min_length[2]|max_length[11]|xss_clean'); $this->form_validation->set_rules('telephone', 'Telephone', 'trim|numeric|xss_clean'); $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|xss_clean'); $this->form_validation->set_rules('website', 'Website', 'trim|prep_url|xss_clean'); $this->form_validation->set_rules('background', 'Background', 'trim|xss_clean'); $data = array( 'title' => 'Add staff member' ); if ($this->form_validation->run() == FALSE) { //Was errors $this->session->set_flashdata('message','You missed some details, please try again.'); $this->layout->load('default', 'add_person', $data); } else { $this->people_model->add_person(); $this->layout->load('default', 'added_person', $data); } } //add 

And in my view of the form I have

  <?php echo validation_errors('<p class="msg msg-error">') ?> 

Someone can understand what I did wrong, I tried everything I could come up with.

The var_dump validation_errors () parameter gives

 string '' (length=0) 

edit: strange, if I swap these two lines around, I get:

  $this->load->library('form_validation'); $this->load->library('layout'); //lib code http://pastebin.com/K1rBV512 Message: Undefined property: People::$form_validation Filename: controllers/people.php Line Number: 27 
+4
source share
3 answers

You need to pass the $data array to your model. All input should receive POST from the VIEW page. You do not show any specific errors, so it is difficult for you to diagnose your problem.

Make sure you POST all inputs like this in MODEL :

 `'first_name' => $this->input->post('first_name'),` 


In VIEW :

 <?php echo validation_errors('<p class="msg msg-error">') ?> 

In your VIEW change it to the following:

 <p class="msg msg-error"><?php echo validation_errors(); ?></p> 
+1
source

The error is not related to the session, but I do not see how you load the session library.

0
source

I believe that the problem is in your view, not in the controller. The problem seems to be that your $ _POST is empty and there is nothing to check.

It seems unanswered, hope this helps others.

If that happens try var_dump on $ _POST. If empty, make sure you have

then inside, make sure you have your inputs with the property "name" and not "id"

Hope this helps someone

0
source

Source: https://habr.com/ru/post/1441302/


All Articles