switch-validator Apply different sets of rules depending of an attribute's value. Pretty much like a php switch statement.

  1. Requirements
  2. Usage

This is a validation that depending on the attribute value can apply other validators (not necessary on the same attribute).

Requirements

tested with Yii 1.1.12 but should work with all Yii 1.1.*

Usage

The validator contains 2 attributes, one for the choices (cases) that the attribute can have and one when the validators that will be applied when none of the cases were matched ( default keyword when using a switch )

So imagine that you want to do a validation like this. You have an attribute type that is the type of the file to upload, and depending the type you want to apply certain validations to the file field (or different like the case). Given this stage you should put something like this in your rules function.

//... more rules validators here
array('type', 'ext.validators.FSwitchValidator', 
        'cases'=>array(
           'image'=>array(
                array('file',  'required')
                array('file',  'file', 'types'=>'jpg,png') 
           ),
           'video'=>array(
               array('url', 'required'),
               array('url', 'validVideoUrl'),
           )
        ),
        'default'=>array('url', 'url', 'defaultSchema'=>'http') //simple array if only one validator
   ),
  //... more rules validators here 
3 0
11 followers
356 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Validation
Developed by: onemoverx
Created on: Sep 26, 2012
Last updated: 12 years ago

Downloads

show all

Related Extensions