• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conditional validation

#1
[eluser]noonereally[/eluser]
Hello,

I've one small problem and i can't find right answer to it.
How do I validate input depending on the value selected by user.

There are few controls like checkboxes, drop downs, inputs on page,
and some fields must be required if user selects particular value from drop down.

For example:
Code:
Dropdown->Anonymous
          Registered

username Input1 Password input2

Checkbox1

If user selects from dropdown "registered" i must validate username and password, and if user select "anonymous" i don't care about username and password...

Can this be done with CI Validation class(I'm using it).
Hopefully I described everything clear enough. If not feel free to say so. Smile)

Thank you.

#2
[eluser]hlz[/eluser]
You could do something like this in your controller:
Code:
if($this->input->post('dropdown') == 'registered')
{
    $this->validation->set_rules($rules);
}

or

if($this->input->post('dropdown') == 'registered')
{
    $this->validation->run();
}

#3
[eluser]noonereally[/eluser]
Thanks, I think i'm getting it now. Smile

#4
[eluser]landitus[/eluser]
Thanks hiz for that piece of code!!

#5
[eluser]Silviu[/eluser]
Try this:

http://ellislab.com/forums/viewthread/202523/#946255

You can define all your rules in application/config/form_validtion.php and load rule groups as needed.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.