Welcome Guest, Not a member yet? Register   Sign In
form_validation and optional fields
#1

[eluser]devbro[/eluser]
If you have the following form:
<input type='text' name='a1' value='<?=set_value('a1')?>' />
<input type='text' name='a2' value='<?=set_value('a2')?>' />
<input type='text' name='a3' value='<?=set_value('a3')?>' />



and the following validations:
Code:
$this->form_validation->set_rules('a1', 'Message', 'trim|required|min_length[10]');
if($this->form_validation->run())


then set_value() will return nothing for a2 and a3.
#2

[eluser]skunkbad[/eluser]
This isn't a bug. You aren't running a2 or a3 through the form validation, so it has nothing to return. Just use set_rules, but don't add any rules.
#3

[eluser]devbro[/eluser]
I know that. but since a2 and a3 are coming from $_POST then it is reasonable to expect set_value() return their value.
#4

[eluser]skunkbad[/eluser]
[quote author="devbro" date="1249553642"]I know that. but since a2 and a3 are coming from $_POST then it is reasonable to expect set_value() return their value.[/quote]

If you really want to have all of the post vars auto validated, and add rules when desired, you might consider extending the form validation class. Honestly, this tripped me up when I first used the form validation class, so I hear what you are saying. On the other hand, I like the feeling of knowing that I am in full control of my data. I can't really think of a good reason, but maybe there is a reason why every post var shouldn't be validated???
#5

[eluser]devbro[/eluser]
it is not what kind of form would need this but it is how i approach my forms.
I put the data that needs to be displayed in $_POST. then set_value grabs and put them in the form for me. But loading form_validation causes this behavior change.




Theme © iAndrew 2016 - Forum software by © MyBB