View on GitHub

YiiConditionalValidator

If-then validation rules on Yii Framework using core validators

Download this project as a .zip file Download this project as a tar.gz file

Note: This version (1.0.0) is not compatible with earlier versions.

General Information

YiiConditionalValidator (YCV) validates some attributes depending on certains conditions (rules). You can use any core validator as you usually would do or any other class based or inline validator. An interesting feature is that you can use dot.notation in your rules to achieve data in related models and you can even use the own YiiConditionalValidator inside itself to perform more complex conditions;

Basically, YCV executes the rules set in the param if and if there are no errors executes the rules set in the param then.

Tip: Fork me (and help me) on GitHub!!

Syntax

array('safeAttribsList', 'path.to.YiiConditionalValidator',
    'if' => array(
        //rule1: array('attrX, attrY', 'required', ...)
        //ruleN: ...
    )
    'then' => array(
        //rule1: array('attrZ, attrG', 'required', ...)
        //ruleN: ...
    )
)

Note: Errors in the rules set in the param if are discarded after checking. Only errors in the rules set in param then are really kept.

Examples

If customer_type is "active" then birthdate and city are required:

public function rules()
{
    return array(
        array('customer_type', 'ext.YiiConditionalValidator',
            'if' => array(
                array('customer_type', 'compare', 'compareValue'=>"active"),
            ),
            'then' => array(
                array('birthdate, city', 'required'),
            ),
        ),
    );
}

If customer_type is "inactive" then birthdate and city are required and city must be "sao_paulo", "sumare" or "jacarezinho":

public function rules()
{
    return array(
        array('customer_type', 'ext.YiiConditionalValidator',
            'if' => array(
                array('customer_type', 'compare', 'compareValue'=>"active"),
            ),
            'then' => array(
                array('birthdate, city', 'required'),
                array('city', 'in', 'range' => array("sao_paulo", "sumare", "jacarezinho")),
            ),
        ),
    );
}

If information starts with 'http://' and has at least 24 chars length then the own information must be a valid url:

public function rules()
{
    return array(
        array('information', 'ext.YiiConditionalValidator',
            'if' => array(
                array('information', 'match', 'pattern'=>'/^http:\/\//'),
                array('information', 'length', 'min'=>24, 'allowEmpty'=>false),
            ),
            'then' => array(
                array('information', 'url'),
            ),
        ),
    );
}

Validation using related data

Note: This feature may not fit into situations too much complex.

You can use dot.notation in attribute name to fetch data from a related model in your rules.

Example:

Assuming that Customer has a relation 'profile', you could check (in customer rules) if the profile.username is not empty before validate something:

//Customer Model
public function rules()
{
    return array(
        array('information', 'ext.YiiConditionalValidator',
            'if' => array(
                //would only return true if profile.username is not empty
                array('profile.username', 'required'),
            ),
            'then' => array(
                array('someAttrib', 'someValidation', ...),
            ),
        ),
    );
}

Installation

  1. Put YiiConditionalValidator.php in your application.extensions folder;

Requirements

Help and reference

Change Log

[Version 1.0.0]

[Version 0.2.0]

ToDo