Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Validation

Domagoj Gojak edited this page Aug 26, 2015 · 8 revisions

List of available rules:

More validation rules comming soon!

NotEmpty

Usage:

<?php $rule = new Formjack\Rule\NotEmpty($invalidMessage, $negate); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new TextField('full_name', array(
        // ...
        'rules' => array(
            new Formjack\Rule\NotEmpty('Please enter your name.');
        )
    ));
));

Description:

Checks whether the given value is empty or not.

Parameters:

  • $invalidMessage (string) (optional)
    Message that will describe the validation error.
    Default: ''

  • $negate (bool) (optional)
    Inverse logic.
    Default: false

Returns:

Returns true if the given value is not empty. Returns false otherwise.
NOTE: If $negate is set to true return value will be negated.

Email

Usage:

<?php $rule = new Formjack\Rule\Email($invalidMessage, $negate); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new EmailField('email', array(
        // ...
        'rules' => array(
            new Formjack\Rule\Email('Please enter your email address.');
        )
    ));
));

Description:

Checks if the given value is a valid email address.

Parameters:

  • $invalidMessage (string) (optional)
    Message that will describe the validation error.
    Default: ''

  • $negate (bool) (optional)
    Inverse logic.
    Default: false

Returns:

Returns true if the given value is a valid email address. Returns false otherwise.
NOTE: If $negate is set to true return value will be negated.

Numeric

Usage:

<?php $rule = new Formjack\Rule\Numeric($invalidMessage, $negate); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new NumberField('age', array(
        // ...
        'rules' => array(
            new Formjack\Rule\Numeric('Given value is not a valid number.');
        )
    ));
));

Description

Checks whether the given value is a valid number.

Parameters

  • $invalidMessage (string) (optional)
    Message that will describe the validation error.
    Default: ''

  • $negate (bool) (optional)
    Inverse logic.
    Default: false

Returns

Returns true if the given value is a valid number. Returns false otherwise.
NOTE: If $negate is set to true return value will be negated.

Length

Usage:

<?php $rule = new Formjack\Rule\Length($min, $max, $inclusive, $invalidMessage, $negate); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new PasswordField('password', array(
        // ...
        'rules' => array(
            new Formjack\Rule\Length(6, 15, false, 'Invalid password length.');
        )
    ));
));

Description:

Checks whether the length of a given value is within defined range.

Parameters:

  • $min (int|null) (optional)
    Min length. If null, validator will not validate min length.
    Default: null

  • $max (int|null) (optional)
    Max length. If null, validator will not validate max length.
    Default: null

  • $inclusive (bool) (optional)
    Allow for the value to me equal to min and max values.
    Default: false

  • $invalidMessage (string) (optional)
    Message that will describe the validation error.
    Default: ''

  • $negate (bool) (optional)
    Inverse logic.
    Default: false

Returns:

Returns true if the length of a given value is within defined range. Returns false otherwise.
NOTE: If $negate is set to true return value will be negated.

CompanyVAT

Usage:

<?php $rule = new Formjack\Rule\CompanyVAT($countryCode, $invalidMessage, $negate); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new TextField('vat_number', array(
        // ...
        'rules' => array(
            new Formjack\Rule\CompanyVAT(Formjack\Rule\CompanyVAT::COUNTRY_HR, 'Given VAT number is not valid.');
        )
    ));
));

Description:

Checks whether the given values is a valid company VAT number.

Parameters:

  • $countryCode (string) (required)
    Country code. List of avaliable countries can be found here: http://ec.europa.eu/taxation_customs/vies/

  • $invalidMessage (string) (optional)
    Message that will describe the validation error.
    Default: ''

  • $negate (bool) (optional)
    Inverse logic.
    Default: false

Returns:

Returns true if the given value is a valid company VAT number. Returns false otherwise.
NOTE: If $negate is set to true return value will be negated.

Checked

Usage:

<?php $rule = new Formjack\Rule\Checked($invalidMessage, $negate); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new CheckboxField('legal', array(
        // ...
        'rules' => array(
            new Formjack\Rule\Checked('You muse agree with the given terms.');
        )
    ));
));

Description:

Checks whether the checkbox is checked or not.

Parameters:

  • $invalidMessage (string) (optional)
    Message that will describe the validation error.
    Default: ''

  • $negate (bool) (optional)
    Inverse logic.
    Default: false

Returns:

Returns true if the checkbox is checked. Returns false otherwise.
NOTE: If $negate is set to true return value will be negated.

When

Usage:

<?php $rule = new Formjack\Rule\When($condition, $rules); ?>

Example:

<?php

// ...

$form = new Form('Example', array(
    new CountrySelectField('country', array(
        /* ... */
    )),
    new StateSelectField('state', array(
        // ...
        'rules' => array(
            new Formjack\Rule\When(function($data){
                return ($data['country'] == 'US');
            }, array(
                new NotEmpty('You must choose a state.')
            ));
        )
    ));
));

Description:

Conditional validation rule.

Parameters:

  • $condition(array $data) (callable) (required)
    Callable function that returns a boolean value. If return value is equal to true rules defined within the second parameter will be taken into account during the validation process. If return value is equal to false rules defined within the second parameter won't be taken into account during the validation process. Formjack will pass a $data parameter to each callable $condition so that you can access field values from within the function.

  • $rules (array) (optional)
    Array of rules that will be used in the validation process if the callable $condition returns true.
    Default: array()