• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Codeigniter form validation callbacks

#1
[eluser]Monarobase[/eluser]
Hello, using the form validation helper I have run into a limitation and would like to know if there is a clean way to do this or not.

In my form I've got an element called "name" which is a drop down list with "Yes", "No", "Other" and a text input called name_other and I want to specify that if "name_other" is selected in the drop down list then the name_other text field is required and otherwise is not.

I could do this using a callback but 15 fields like this would mean 15 callbacks unless I could retreive the field name in the callback in which case I could check "name_other" with a function like this :

Code:
function _check_other($val,$input_name)
{
   if($val == 'other') {
      if($_POST[$input_name.'_other'] == '') {
      
      }  
   }
}

Can I do something like this ? And if not would in not be an interesting feature to be able to have an extra $input_name value passed onto the callback function ?

#2
[eluser]flaky[/eluser]
For the moment you can't do that.

#3
[eluser]Monarobase[/eluser]
Ok thankyou,

I have noticed though that "%s" when passed retrieves the human name, it's a shame that you can't also retrieve the variable name using something similar...

#4
[eluser]Dyllon[/eluser]
You could easily extend the form_validation class to do this or alternatively you can add the field name as a parameter to the callback

Code:
$rules = array(
        'field' => 'name',
        'label' => 'Name',
        'rules' => 'callback__check_other[name]'
    )

function _check_other($str, $params) {
//do stuff
}

#5
[eluser]Monarobase[/eluser]
Thankyou, that's just what I needed !


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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