Welcome Guest, Not a member yet? Register   Sign In
Accessing validation field values in 1.7?
#1

[eluser]degu[/eluser]
Hi,

this is probably a stupid question, but how do I access field values with the new form validation library? In 1.6, you could use:

Code:
$this->validation->title

It looks like there is no equivalent in 1.7? Or am I missing something? I know that I can use set_value, but this is a helper method to repopulate forms. And
Code:
$this->form_validation->_field_data['title']['post_data']
doesn't look correct either.

So, what's the recommended way to access field values in a controller with CI 1.7? Thanks.
#2

[eluser]Huan[/eluser]
I think you could use $this->input->post('field_name'). This is what I'm using after upgrading to CI 170
#3

[eluser]degu[/eluser]
I think the drawback of using the input library is that the values are not sanitized. E.g., if you use trim or htmlspecialchars in your validation rules, the values from the validation class haven been run through those functions. The values you get from the input library are still the original values submitted by the user AFAIK. There must be a better way to access the submitted and validated data, no? I would have expected something like

Code:
$this->form_validation->value('name')

What am I missing? I mean, this is probably one of the most basic features of the validation library.
#4

[eluser]nizsmo[/eluser]
Accessing $this->input->post('your_field_name') after $this->form_validation->run() has been called (and assuming successful) the POST value will have been modified and "sanitized" according to your form_validation rules.
EG if you have a rule required|md5 for your $this->input->post('password') field, the $this->input->post('password') will be md5'ed automatically. At least, this has been my experience with CI 1.7 so far.
#5

[eluser]Randy Casburn[/eluser]
In 1.6.3 once validation->run() was executed the values in validation and input were identical : hence sanitized. I doubt that would have change. We can take a look to validate that easily enough.

Randy
#6

[eluser]degu[/eluser]
Thanks for the clarification. So it looks like the input class is the way to go?
#7

[eluser]nizsmo[/eluser]
Yea you shouldn't have any problems accessing the values with the input class, as it will have been sanitized already. you can test it out to double make sure Smile
#8

[eluser]SneakyDave[/eluser]
Quote:I would have expected something like
Code:
$this->form_validation->value('name')

I was searching for a solution to a different problem, but I use this in my controllers rather than $this->input->post() just to be consistent between my views and controllers:

Code:
$this->form_validation->set_value('name')

For example in the controller for an edit function, $file is the contents retrieved from a flat file (or database).
Code:
$data['name'] = array(
       'name'        => 'name',
       'id'          => 'name',
       'value'       => $this->form_validation->set_value('name',$file->set_value('name')),
       'maxlength'   => '100',
       'size'        => '50',
                     );

The view...
Code:
...
<li><strong>Your Name:</strong><br />&lt;?=form_input($name)?&gt;<br />
&lt;?=form_error('name')?&gt;</li><br />
...

I'd like to know if there's a way to set the form POST values to something else in the controller though. I can't seem to find that without assigning the values to new variables, and changing them. For instance, I have an input field with a lastupdate date/time value. It is modifiable by the user, but if they don't modify it, I want to use the current date/time when I do a database or file update, but I can't change the value of the text input directly.
#9

[eluser]a&w[/eluser]
I was just looking this up myself. Seems like the Form_Validation page and/or form helper page (I checked both) in the user guide drop the ball a little bit here on an otherwise excellent review of the topic.

I think the user guide just needs a little snippet at the end what is preferred practice for accessing form data.

Using:
Code:
$this->form_validation->set_value('name',$file->set_value('name')),
seems to have the slight advantage in that you could specify a default value at the same time (in case the db is set to NOT NULL and has no default specified).
#10

[eluser]a&w[/eluser]
I bumped the user guide thread about this if anyone wants to chime in. These threads are cross linked so posting in either thread should be good?
http://ellislab.com/forums/viewthread/97045/




Theme © iAndrew 2016 - Forum software by © MyBB