PHP Classes

DependantValidation

Recommend this page to a friend!

      PHP Forms Class with HTML Generator and JavaScript Validation  >  PHP Forms Class with HTML Generator and JavaScript Validation package blog  >  How to Show Google Ma...  >  All threads  >  DependantValidation  >  (Un) Subscribe thread alerts  
Subject:DependantValidation
Summary:DependantValidation
Messages:7
Author:bartb
Date:2010-09-21 11:48:12
Update:2010-09-22 08:59:32
 

  1. DependantValidation   Reply   Report abuse  
Picture of bartb bartb - 2010-09-21 11:48:12
Hi! Thanks for your superb class!
I have a question about DependantValidation. Is it possible to make an input field dependant of multiple other input fields? I've tried to enter an array in the DependantValidation attribute, but that didn't do the job.
thanks, Bart

  2. Re: DependantValidation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-09-22 01:57:17 - In reply to message 1 from bartb
Dependent validation is just for determine whether an input is going to be validated or not depending on the value of another input.

Is that what you want or you want to make the value of an input depend on the values of other inputs?

  3. Re: DependantValidation   Reply   Report abuse  
Picture of bartb bartb - 2010-09-22 05:33:16 - In reply to message 2 from Manuel Lemos
Thanks for your reply. I mean the first. Let me give an example:
I have a set of radiobuttons named a, b and c and a text field. What I want to do is validate the text field if radiobutton a or b is checked, but not when c is checked. Is this possible?
Thanks, Bart

  4. Re: DependantValidation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-09-22 06:20:25 - In reply to message 3 from bartb
Yes, that is easy because radio buttons have boolean values: checked or not checked.

You just need to set the DependentValidation property of the input you want to validate conditionally to the ID of the radio button you want it to be dependent.

Take a look at the test_dependent_validation.php example script:

phpclasses.org/browse/file/19360.ht ...

  5. Re: DependantValidation   Reply   Report abuse  
Picture of bartb bartb - 2010-09-22 06:45:33 - In reply to message 4 from Manuel Lemos
Thanks, but that is not quite what i meant.
In your example you have 1 checkbox and 1 textfield. That is clear to me.

But what if you have 3 radiobuttons as a set and 1 textfield and the textfield must be validated only if radio 1 or 2 is checked, but not when checkbox 3 is checked. So, the textfield is dependant of 2 radiobuttons.
Can I configure like this:

"DependantValidation" => array('radiobutton1', 'radiobutton2'),

thanks for your effort, Bart

  6. Re: DependantValidation   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2010-09-22 08:20:46 - In reply to message 5 from bartb
Oh, I see. In that case you need to implement a custom input plug-in class that checks all the dependent inputs like this:


class form_custom_dependent_validation_class extends form_custom_class
{
var $inputs = array();

Function AddInput(&$form, $arguments)
{
if(!IsSet($arguments['DependentValidations'])
|| GetType($arguments['DependentValidations']) != 'array'
|| count($this->inputs = $arguments['DependentValidations']) == 0)
return('it was not specified the list of dependent validation inputs');
return('');
}

Function GetJavascriptCheckedState(&$form, $form_object)
{
$javascript = '';
$ti = count($this->inputs);
for($i = 0; $i < $ti; ++$i)
{
if(strlen($js = $form->GetJavascriptCheckedState($form_object, $this->inputs[$i])) == 0)
return('');
if($i > 0)
$javascript .= '|| ';
$javascript .= $js;
}
return($javascript);
}

Function GetCheckedState(&$form)
{
$ti = count($this->inputs);
for($i = 0; $i < $ti; ++$i)
{
if($form->GetCheckedState($this->inputs[$i]))
return(1);
}
return(0);
}

Function ValidateInput(&$form)
{
return('');
}
};

Then you add the custom input like this:

$form->AddInput(array(
'TYPE'=>'custom',
'ID'=>'custom_condition',
'NAME'=>'custom_condition',
'CustomClass'=>'form_custom_dependent_validation_class',
'DependentValidations'=>array(
'radio_1',
'radio_2',
)
));

  7. Re: DependantValidation   Reply   Report abuse  
Picture of bartb bartb - 2010-09-22 08:59:32 - In reply to message 6 from Manuel Lemos
wow, superb!
thanks a lot for your (quick!) solution.