Settings
A customizable
Validation rule. Runs PHP code to test for
Validation success or failure
- Error message - The message to display next to the element if the Validation fails. This text should explain why the Validation failed and what steps the user should take to remedy the error.
- Condition - A PHP expression which if returns false, means that the Validation is not run.
- PHP -PHP code to run.
- The element's submitted value is found in the variable $data.
- Other element values can be found in $_REQUEST (use JFactory::getApplication()->input->get() to access).
- If the code returns TRUE then the Validation is successful,
- If it returns FALSE then the Validation fails.
- You can also return a string which can be used if the 'Match or replace' option is set to 'replace'
- Match or Replace - If set to match then the Validation behaves as normal and on Validation failure returns you to the form showing the error message. If set to 'replace' then the submitted value is replaced with the data returned from the 'PHP code' script
- Tip text - additonal informational text which is displayed as a tip when the user hovers over the element's label. This can provide specific information about the Validation, enabling the user to correctly fill in the field.
Placeholders may be used in the PHP code or condition fields.
Examples
Ensure a date
Elements value (stored in the value $data) is greater than another's whose full element name is 'tablename___elementname':
PHP:
$app = JFactory::getApplication();
$f = $app->input->get('tablename___elementname', 'default value', 'string'); //'string' = filter e.g. 'INT','STRING'
$f = JFactory::getDate($f)->toUnix();
$data = JFactory::getDate($data)->toUnix();
return $data > $f;
Set a custom error message
As of 14/08/2013 (fabrik 3.0.x and 3.1) you can do this....
PHP:
$this->setMessage('oh dear you can not do that!');
In Fabrik 4 do
$thisValidation->setMessage('oh dear you can not do that!');
repeat group data
When your element is in a repeating group, then the placeholder contains the value for the current repeat group's values.
For example you have a form with a repeat group 'regions' and within that group you have a field 'regions___label'. Lets say the form is submitted with two repeat
Groups, in the first repeat group the label is 'Warwickshire' and in the second group the label is 'Yorkshire'.
If you add the following code as your validation:
PHP:
echo "<pre>";print_r($data);
exit;
then upon form submission you will see an array :
Code:
Array
(
[0] => Warwickshire
[1] => Yorkshire
)
If you replace the validation PHP with
PHP:
echo '{regions___dropdown}';
exit;
the output will be
As the first validation runs and our exit will stop the code. If there was no exit the validation would be run for each repeat group, with the successive values 'Warwickshire' and 'Yorkshire' being used