7.0 KiB
Model validation reference
This guide section doesn't describe how validation works but instead describes all Yii validators and their parameters. In order to learn model validation basics please refer to Model, Validation subsection.
Standard Yii validators
Standard Yii validators could be specified using aliases instead of referring to class names. Here's the list of all validators budled with Yii with their most useful properties:
boolean: BooleanValidator
Checks if the attribute value is a boolean value.
trueValue, the value representing true status. (1)falseValue, the value representing false status. (0)strict, whether to compare the type of the value andtrueValue/falseValue. (false)
captcha: CaptchaValidator
Validates that the attribute value is the same as the verification code displayed in the CAPTCHA. Should be used together with CaptchaAction.
caseSensitivewhether the comparison is case sensitive. (false)captchaActionthe route of the controller action that renders the CAPTCHA image. ('site/captcha')
compare: CompareValidator
Compares the specified attribute value with another value and validates if they are equal.
compareAttributethe name of the attribute to be compared with. (currentAttribute_repeat)compareValuethe constant value to be compared with.operatorthe operator for comparison. ('==')
date: DateValidator
Verifies if the attribute represents a date, time or datetime in a proper format.
formatthe date format that the value being validated should follow accodring to http://www.php.net/manual/en/datetime.createfromformat.php. ('Y-m-d')timestampAttributethe name of the attribute to receive the parsing result.
default: DefaultValueValidator
Sets the attribute to be the specified default value.
valuethe default value to be set to the specified attributes.
double: NumberValidator
Validates that the attribute value is a number.
maxlimit of the number. (null)minlower limit of the number. (null)
email: EmailValidator
Validates that the attribute value is a valid email address.
allowNamewhether to allow name in the email address (e.g.John Smith <john.smith@example.com>). (false).checkMXwhether to check the MX record for the email address. (false)checkPortwhether to check port 25 for the email address. (false)enableIDNwhether validation process should take into account IDN (internationalized domain names). (false)
exist: ExistValidator
Validates that the attribute value exists in a table.
classNamethe ActiveRecord class name or alias of the class that should be used to look for the attribute value being validated. (ActiveRecord class of the attribute being validated)attributeNamethe ActiveRecord attribute name that should be used to look for the attribute value being validated. (name of the attribute being validated)
file: FileValidator
Verifies if an attribute is receiving a valid uploaded file.
typesa list of file name extensions that are allowed to be uploaded. (any)minSizethe minimum number of bytes required for the uploaded file.maxSizethe maximum number of bytes required for the uploaded file.maxFilesthe maximum file count the given attribute can hold. (1)
filter: FilterValidator
Converts the attribute value according to a filter.
filterPHP callback that defines a filter.
Typically a callback is either the name of PHP function:
array('password', 'filter', 'filter' => 'trim'),
Or an anonymous function:
array('text', 'filter', 'filter' => function ($value) {
// here we are removing all swear words from text
return $newValue;
}),
in: RangeValidator
Validates that the attribute value is among a list of values.
rangelist of valid values that the attribute value should be among.strictwhether the comparison is strict (both type and value must be the same). (false)notwhether to invert the validation logic. (false)
integer: NumberValidator
Validates that the attribute value is an integer number.
maxlimit of the number. (null)minlower limit of the number. (null)
match: RegularExpressionValidator
Validates that the attribute value matches the specified pattern defined by regular expression.
patternthe regular expression to be matched with.notwhether to invert the validation logic. (false)
required: RequiredValidator
Validates that the specified attribute does not have null or empty value.
requiredValuethe desired value that the attribute must have. (any)strictwhether the comparison between the attribute value and requiredValue is strict. (false)
safe: SafeValidator
Serves as a dummy validator whose main purpose is to mark the attributes to be safe for massive assignment.
string: StringValidator
Validates that the attribute value is of certain length.
lengthspecifies the length limit of the value to be validated. Can beexactly X,array(min X),array(min X, max Y).maxmaximum length. If not set, it means no maximum length limit.minminimum length. If not set, it means no minimum length limit.encodingthe encoding of the string value to be validated. (\yii\base\Application::charset)
unique: UniqueValidator
Validates that the attribute value is unique in the corresponding database table.
classNamethe ActiveRecord class name or alias of the class that should be used to look for the attribute value being validated. (ActiveRecord class of the attribute being validated)attributeNamethe ActiveRecord attribute name that should be used to look for the attribute value being validated. (name of the attribute being validated)
url: UrlValidator
Validates that the attribute value is a valid http or https URL.
validSchemeslist of URI schemes which should be considered valid. array('http', 'https')defaultSchemethe default URI scheme. If the input doesn't contain the scheme part, the default scheme will be prepended to it. (null)enableIDNwhether validation process should take into account IDN (internationalized domain names). (false)
Validating values out of model context
Sometimes you need to validate a value that is not bound to any model such as email. In Yii Validator class has
validateValue method that can help you with it. Not all validator classes have it implemented but the ones that can
operate without model do. In our case to validate an email we can do the following:
$email = 'test@example.com';
$validator = new yii\validators\EmailValidator();
if ($validator->validateValue($email)) {
echo 'Email is valid.';
} else {
echo 'Email is not valid.'
}
TBD: refer to http://www.yiiframework.com/wiki/56/ for the format