1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

PHP validation

Jun 7, 2018
PHP validation

  • Settings (top)


    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 (top)

    Ensure a date elements value (stored in the value $data) is greater than another's whose full element name is 'tablename___elementname':
    $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(top)

    As of 14/08/2013 (fabrik 3.0.x and 3.1) you can do this....

    $this->setMessage('oh dear you can not do that!');

    repeat group data(top)

    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:


    echo "<pre>";print_r($data);
    then upon form submission you will see an array :

    Code (Text):

        [0] => Warwickshire
        [1] => Yorkshire
    If you replace the validation PHP with


    echo '{regions___dropdown}';

    the output will be

    Code (Text):

    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
  • Loading...