PHP validation

Oct 28, 2015
PHP validation

  • Settings (top)


    php.png

    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.
      • If it returns 'true' then the validation is successful,
      • if it returns false then the validation fails.
      • The element's submitted value is found in the variable $data.
      • 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':
    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(top)


    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!');
     

    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:

    PHP:

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

    Code (Text):

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

    PHP:

    echo '{regions___dropdown}';
    exit;

     
    the output will be

    Code (Text):

    Warwickshire
     
    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...