Difference between #21 and #30 of
Reference: Model rules validation

Changes

Title unchanged

Reference: Model rules validation

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

Form validation, model, validation, reference, validation rules, validator

Content changed

[...]
If this option is not set, the rule will be applied in any scenario.
See the section [Scenarios][#Scenarios] for details.
+ *message*: replaces the default error message if validation fails.
+ *...validation parameters...*: any number of extra parameters to be used by the specified validator.

**Note**: You may get into trouble when using model's attributes as additional parameters for validators. Take a look at: [https://github.com/yiisoft/yii/issues/3001](https://github.com/yiisoft/yii/issues/3001 "https://github.com/yiisoft/yii/issues/3001")
 

### Choice of validators
[...]
}

public
function validateLogin() { // [...]
 
}
 
```

### Scenarios
[...]
through the `"on" => "scenario"` parameter.


 
## Validation rules reference

For each rule, the list of its specific arguments is given with the eventual default value *(in italics)*.
[...]
8. **types**, a list of file name extensions that are allowed to be uploaded. *(null, meaning all extensions)*
9. **wrongType**, the error message used when the uploaded file has an extension name that is not listed among extensions.

 
### `filter` : [CFilterValidator]
 
Transforms the data being validated based on a filter.
 
 
1. **filter**, the filter method.
10. **mimeTypes**, a list of MIME-types of the file that are allowed to be uploaded. *(requires fileinfo PHP extension)*
 
11. **wrongMimeType**, the error message used when the uploaded file has a MIME-type that is not listed among mimeTypes.
 
 
### `filter` : [CFilterValidator]
 
Transforms the data being validated based on a filter.
 
 
1. **filter**, the filter method.
 
 
*Examples:*
 
 
- With a custom function
 
 
```php 
array('mycode','filter','filter'=>'strtoupper'),
 
```
 
- With a custom method
 
 
```php 
array('eanupc','filter','filter'=>array($this,'keepProductCode')),
 
array('eanupc','filter','filter'=>array(self,'formatProductCode')),
 
...
 
public function keepProductCode($code){
 
// returns a new value (uses $this) ...
 
}
 
static public function formatProductCode($code){
 
// returns a new value (cannot use $this) ...
 
}
 
```
 
*NOTE:* The filter validator maybe use with other validators for the same attribute. In this case one must pay attention to the order of the validators list as the filter validator will change the attribute value. 
 


### `in` : [CRangeValidator]
[...]
4. **strict**, whether the comparison is strict (both type and value must be the same). *(false)*

*Example:*
 
 
 
```php 
array('language','in','range'=>array('en','fr','zn'),'allowEmpty'=>false),
 
```
 
 
### `length` : [CStringValidator]
Validates that the attribute value is of certain length.
[...]
6. **tooSmall**, user-defined error message used when the value is too small.

*Example:*
 
 
 
```php 
array('quantity','numerical',
 
    'integerOnly'=>true,
 
    'min'=>1,
 
    'max'=>250,
 
    'tooSmall'=>'You must order at least 1 piece',
 
    'tooBig'=>'You cannot order more than 250 pieces at once'),
 
```
 
 
### `match` : [CRegularExpressionValidator]
Validates that the attribute value matches to the specified regular expression.
[...]
* [CUniqueValidator]
* [CUnsafeValidator]
* [CUrlValidator]

 
99 0
114 followers
Viewed: 884 992 times
Version: 1.1
Category: How-tos
Written by: krillzip
Last updated by: Rodrigo
Created on: Nov 29, 2009
Last updated: 10 years ago
Update Article

Revisions

View all history