Author: Manuel Lemos
Updated on: 2017-02-13
Posted on: 2011-03-10
Package: PHP Forms Generation and Validation
Categories: Validation
This article tells how you can easily implement conditional validation logic by explaining how to make one field be validated only if a radio or checkbox is site or some other special custom logic that you need to implement.
Dependent Validation
There are situations in which you need to have forms with fields that should be validated only in certain conditions. That could be either when radio or checkbox is checked, or some other more complex condition occurs.
Take a look for instance at this example form. As you may see there is a group of radio buttons. Some have text fields right next to their labels. Each of those text fields only need to be validated when the respective option radio button is checked.
Setting up Dependent Validation
The main PHP Forms Generation and Validation class has a built-in feature that can easily be used to setup dependent validation between two inputs.
In the definition of the input that will be validated or not, you need to use the DependentValidation parameter to specify the identifier of another input, the dependency input, which is typically a radio or checkbox input.
The CHECKED property of the dependency input will determine whether the dependent input will be validated or not. If the CHECKED property is set to 1, the validation will happen, otherwise the validation will be skipped as if the input is always valid.
Here is sample code on how to setup the validation of a text input depending on the value of a radio input:
$form->AddInput(array( 'TYPE' => 'radio', 'NAME' => 'apply', 'ID' => 'apply_by_email', 'LABEL' => 'Send resumes to an <u>e</u>-mail address', 'ACCESSKEY' => 'e', 'VALUE' => 'by_email', )); $form->AddInput(array( 'TYPE' => 'text', 'NAME' => 'application_email', 'ID' => 'application_email', 'ValidateAsEmail' => 1, 'ValidationErrorMessage' => 'It was not specified a valid e-mail address to send resumes.', 'DependentValidation' => 'apply_by_email', ));
Custom Dependent Validation Condition
It is also possible to setup special type of dependent validation defined by a condition that is more complex than just checking the state of a checkbox or radio button.
For that you need to define a new custom input class that implements a virtual input which behaves like a checkbox or radio button when you check its state. That new custom input type is virtual because it does not render any thing. It just responds to calls to check if its state is 1 (CHECKED) or not.
The first thing you need to do is to determine what exactly is the condition that will be evaluated to define whether the validation will be performed or not.
Lets create a simple custom input type that will return that its checked state is 1 when two checkbox inputs named check_a and check_b are both checked.
This is just a virtual example input, so it does not take an parameters, it does not perform any validation, not it renders any part of the form.
class form_custom_condition_class extends form_custom_class { var $server_validate=0; Function AddInput(&$form, $arguments) { return(''); }
The GetJavascriptCheckedState function must return a string with a JavaScript expression that will evaluate the virtual checked state of this input.
Since we want to make it return that it is checked when both check_a and check_b inputs are checked, we call the GetJavascriptCheckedState function of the main forms class to generate JavaScript code that checks the state of those inputs.
Function GetJavascriptCheckedState(&$form, $form_object) { $check_a_state = $form->GetJavascriptCheckedState( $form_object, 'check_a'); $check_b_state = $form->GetJavascriptCheckedState( $form_object, 'check_b'); return('('.$check_a_state.' && '.$check_b_state.')'); }
Similarly, the GetCheckedState function must return a boolean value that evaluates on the server side (PHP) whether the check_a and check_b checkbox inputs are both checked.
Function GetCheckedState(&$form) { $check_a_state = $form->GetCheckedState('check_a'); $check_b_state = $form->GetCheckedState('check_b'); return($check_a_state && $check_b_state); } };
Now we are ready to define the custom input that will establish the dependent validation of one input using a custom condition, which in our example is to check be both check_a and check_b inputs are checked.
$form->AddInput(array( 'TYPE'=>'checkbox', 'ID'=>'check_a', 'NAME'=>'check_a', 'CHECKED'=>1, 'LABEL'=>'Condition <u>A</u>', 'ACCESSKEY'=>'A' )); $form->AddInput(array( 'TYPE' => 'checkbox', 'ID' => 'check_b', 'NAME' => 'check_b', 'CHECKED' => 1, 'LABEL' => 'Condition <u>B</u>', 'ACCESSKEY' => 'B' )); $form->AddInput(array( 'TYPE' => 'custom', 'ID' => 'custom_condition', 'CustomClass' => 'form_custom_condition_class', )); $form->AddInput(array( 'TYPE' => 'text', 'ID' => 'dependent', 'NAME' => 'dependent', 'LABEL' => '<u>D</u>ependent', 'ACCESSKEY' => 'D', 'ValidateAsNotEmpty' => 1, 'ValidationErrorMessage'=> 'It was not entered a value in the dependent field.', 'DependentValidation' => 'custom_condition' ));
You need to be a registered user or login to post a comment
1,433,597 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
1. Javascript - Frank Cauley (2011-03-17 20:22)
Validating forms without javascript... - 1 reply
Read the whole comment and replies