Welcome Guest, Not a member yet? Register   Sign In
Validation errors and rules issue
#1

[eluser]Wonder Woman[/eluser]
Hi,

I've recently gotten in to CodeIgniter and I've set up a small form which I need to validate and then pre-populate the form fields with the submitted data.

When the form is submitted the error messages appear and the fields which had data are pre-populated, however the error messages which appear are missing the field names.

At the moment they appear like this:
Code:
The field is required.

And they should appear like this:
Code:
The title field is required.

When I remove the set fields code, exampled:
Code:
$fields['postcode'] = $this->input->post('postcode');
It works fine but it seems this code is overriding the rules, example:
Code:
$rules['postcode'] = "required";

If you could advise me on how to correct this I would be very grateful.

Many thanks.
#2

[eluser]Maglok[/eluser]
I take it you are using the Form Helper?

Could you show some more code?

Something that might be it is the order of the parameters you feed to the validate class

Code:
<?php echo form_input('soort', set_value('soort')? set_value('soort'):$row->soort); ?>

That would display an input called 'Soort' with the value that was filled out if it didn't validate or the value set from the database if it wasn't filled out.

Then in the controller
Code:
$this->form_validation->set_rules('soort', 'Soort', 'required');
#3

[eluser]Wonder Woman[/eluser]
Thank you for replying.

Yes I'm using the form helper, my input boxes currently look like this:

Code:
<input type="input" id="postcode" class="postcode" name="postcode" value="<?php echo $this->validation->postcode;?>" />

I just wonder what I'm doing wrong because when I hide the set fields in the controller, then the error messages display the field name...
#4

[eluser]Maglok[/eluser]
That looks solid enough.

Perhaps you are assigning the set_fields and set_rules in an inappropriate manner?

I personally tend to use the Form Generator library, so this has been a while. Can you quote the controller bit? Doesn't seem very view related.
#5

[eluser]Wonder Woman[/eluser]
Yes sure, the code for the controller is:

Code:
function Property_Manager()
{
   parent::Controller();
   $this->load->helper(array('form', 'url'));
   $this->load->library('validation');
}

function index()
{
   // form validation
   $rules['address'] ="required";
   $rules['postcode'] ="required";
   $this->validation->set_rules($rules);
            
   // repopulate the form
   $fields['address'] = $this->input->post('address');
   $fields['postcode'] = $this->input->post('postcode');
   $this->validation->set_fields($fields);
            
   // check to see if form has been submitted
   if($this->validation->run()==FALSE){
   }
   else {
   }
  
   // load the view
   $this->load->view('property_manager_view');
}

Thanks for helping. Smile
#6

[eluser]Maglok[/eluser]
Ah there we go. The user guide mentions this:

Quote:The array keys are the actual names of the form fields, the value represents the full name that you want shown in the error message.

Since you use $this->input->post('address'); you are using the value. It seems you should be using the name of the field you want to use and repopulate:

Code:
$fields['postcode'] = 'Postcode';

http://ellislab.com/codeigniter/user-gui...ation.html for more info

A bit strange how it handles it, but just fill out the name you want to appear there then, I think it can figure out what to put in the field by itself.
#7

[eluser]Wonder Woman[/eluser]
Yay, that has worked thank you, I didn't understand how it worked until you said that, I'm still trying to get my head around CodeIgniter so no doubt a few more posts to come on here.

Thank you for a speedy response.

Ashley Smile




Theme © iAndrew 2016 - Forum software by © MyBB