CodeIgniter Forums
Codeigniter form validation callbacks - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Codeigniter form validation callbacks (/showthread.php?tid=26592)



Codeigniter form validation callbacks - El Forum - 01-18-2010

[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 ?


Codeigniter form validation callbacks - El Forum - 01-18-2010

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


Codeigniter form validation callbacks - El Forum - 01-18-2010

[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...


Codeigniter form validation callbacks - El Forum - 01-18-2010

[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
}



Codeigniter form validation callbacks - El Forum - 01-18-2010

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