dependency validation, form logic, connecting form fields validation
Dependency validation
Adding dependency validation is the key to implement clever form logic: it lets you define how to validate fields based on some criteria.
Actually dependency validation is not a validation type like the date or string validation: instead it's a setting for all the validation types available in Clonefish.
Adding dependency practically means adding a condition group to a validation of an element: if conditions in the group are met, the element gets validated as normally. If conditions don't meet, the validation is not applied.
For example, you can make a text input required using a string validation. To make this validation depend on the value of a specific select option value, you need to add the appropriate condition ("when the select options equals X") to the string validation of the text field.
Let's see how it works:
'theselect' => Array(
'displayname' => 'Where did you hear about us?' 'type' => 'select', 'values' => Array( 'internet' => 'internet' 'newspaper' => 'newspaper' 'friend' => 'friend' 'other' => 'other (please specify!)' // we want more info in this case ), ), 'thetextfield' => Array(
'type' => 'inputText',
'displayname' => 'Other:',
'validation' => Array(
Array(
'anddepend' => Array(
Array(
'js' => ' <FORM.theselect> == "other" ',
'php' => ' <FORM.theselect> == "other" ',
)
),
'type' => 'required'
),
),
),
As you can see, the validation rules are equipped with a condition group (the anddepend
array). Conditions in a group are always in a relation: AND or OR relation is available (anddepend
or ordepend
, respectively).
You can simply tell how the conditions are related by putting them in a validation setting named as anddepend
or ordepend
(at least one of these array keys must exist in the validation setting). If you have only a single condition (like in the above example), it doesn't matter which one you choose.
Now about conditions: conditions must be defined for both PHP and JavaScript. (this approach may be familiar from the custom validation). While defining a condition, Clonefish helps you with JavaScript and PHP shortcuts: <FORM.elementname>
returns the actual element value, so you don't have to dig through the JS DOM or PHP POST arrays to get the element value. This shortcut works for all element types and calls the appropriate function for you both in JS and PHP:
- for inputCheckbox it returns
true
orfalse
- or onvalue or
false
if you have providedonvalue
for the checkbox
- for inputCheckboxDynamic it returns an array including checkbox values, or
false
if no checkboxes are checked in the group - for multiple selects (select, selectDynamic) it returns an array of option values or
false
- for any other fields it returns the field value or selected field value, if present (it may not present for an inputRadio without a default value), or
false
otherwise
Even though the above may sound complex at first, check the detailed example: all the things above are actually quite simple! Make sure to try it live - follow the link below the example array!
$config = Array(
'wanttea' => Array(
'type' => 'inputRadio',
'displayname' => 'Would you like some tea?',
'values' => Array( 0 => 'no', 1 => 'yes' ),
'validation' => Array(
Array( 'type' => 'required' ),
),
),
// if the user drinks tea, make him/her choose a flavor!
'teaflavor' => Array(
'type' => 'inputRadio',
'displayname' => 'Choose a flavor!',
'values' => Array(
0 => 'almond',
1 => 'cherry',
2 => 'ginger',
3 => 'lemon',
4 => 'peach',
5 => 'vanilla',
),
'validation' => Array(
Array(
'anddepend' => Array(
Array(
'js' => '<FORM.wanttea> == 1',
'php' => '<FORM.wanttea> == 1',
)
),
'type' => 'required'
),
),
),
// if the user chooses not to drink tea, try to sell some coffee then:
'wantcoffee' => Array(
'type' => 'inputRadio',
'displayname' => 'Some coffee?',
'values' => Array( 0 => 'no', 1 => 'yes' ),
'validation' => Array(
Array(
'anddepend' => Array(
Array(
'js' => '<FORM.wanttea> == 0',
'php' => '<FORM.wanttea> == 0',
)
),
'type' => 'required'
),
),
),
// if the users said yes for tea OR coffee, we require him/her to
// select at least one addon (okay, it's not that polite, but it's just
// an example ;) )
'addons' => Array(
'type' => 'inputCheckboxDynamic',
'displayname' => 'Addons',
'values' => Array(
'sugar' => 'sugar',
'brown sugar' => 'brown sugar',
'cream' => 'cream',
'milk' => 'milk',
),
'validation' => Array(
Array(
'ordepend' => Array(
Array(
'php' => '<FORM.wanttea> == 1',
'js' => '<FORM.wanttea> == 1'
),
Array(
'php' => '<FORM.wantcoffee> == 1',
'js' => '<FORM.wantcoffee> == 1'
),
),
'type' => 'required'
),
),
),
// no coffee, no tea? It MUST be something else then ;)
'custom' => Array(
'type' => 'select',
'displayname' => 'Please choose something else!',
'values' => Array(
0 => '',
1 => 'Coke',
2 => 'Orange juice',
3 => 'Mineral water',
4 => 'Energy drink',
),
'validation' => Array(
Array(
'anddepend' => Array(
Array(
'php' => '<FORM.wanttea> == 0',
'js' => '<FORM.wanttea> == 0'
),
Array(
'php' => '<FORM.wantcoffee> == 0',
'js' => '<FORM.wantcoffee> == 0'
),
),
'type' => 'number',
'minimum' => 1
),
),
),
);
Live examples:
form validation for developers!
clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain formslearn more