mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
@ -23,7 +23,7 @@ if ($model->validate()) {
|
||||
```
|
||||
|
||||
|
||||
## Declaring Rules <a name="declaring-rules"></a>
|
||||
## Declaring Rules <span id="declaring-rules"></span>
|
||||
|
||||
To make `validate()` really work, you should declare validation rules for the attributes you plan to validate.
|
||||
This should be done by overriding the [[yii\base\Model::rules()]] method. The following example shows how
|
||||
@ -95,7 +95,7 @@ an active attribute declared in `scenarios()` and is associated with one or mult
|
||||
declared in `rules()`.
|
||||
|
||||
|
||||
### Customizing Error Messages <a name="customizing-error-messages"></a>
|
||||
### Customizing Error Messages <span id="customizing-error-messages"></span>
|
||||
|
||||
Most validators have default error messages that will be added to the model being validated when its attributes
|
||||
fail the validation. For example, the [[yii\validators\RequiredValidator|required]] validator will add
|
||||
@ -120,7 +120,7 @@ to describe the validation failure when the value being validated is too big and
|
||||
You may configure these error messages like configuring other properties of validators in a validation rule.
|
||||
|
||||
|
||||
### Validation Events <a name="validation-events"></a>
|
||||
### Validation Events <span id="validation-events"></span>
|
||||
|
||||
When [[yii\base\Model::validate()]] is called, it will call two methods that you may override to customize
|
||||
the validation process:
|
||||
@ -134,7 +134,7 @@ the validation process:
|
||||
the validation is completed.
|
||||
|
||||
|
||||
### Conditional Validation <a name="conditional-validation"></a>
|
||||
### Conditional Validation <span id="conditional-validation"></span>
|
||||
|
||||
To validate attributes only when certain conditions apply, e.g. the validation of one attribute depends
|
||||
on the value of another attribute you can use the [[yii\validators\Validator::when|when]] property
|
||||
@ -174,7 +174,7 @@ function whose return value determines whether to apply the rule or not. For exa
|
||||
```
|
||||
|
||||
|
||||
### Data Filtering <a name="data-filtering"></a>
|
||||
### Data Filtering <span id="data-filtering"></span>
|
||||
|
||||
User inputs often need to be filtered or preprocessed. For example, you may want to trim the spaces around the
|
||||
`username` input. You may use validation rules to achieve this goal.
|
||||
@ -196,7 +196,7 @@ As you can see, these validation rules do not really validate the inputs. Instea
|
||||
and save them back to the attributes being validated.
|
||||
|
||||
|
||||
### Handling Empty Inputs <a name="handling-empty-inputs"></a>
|
||||
### Handling Empty Inputs <span id="handling-empty-inputs"></span>
|
||||
|
||||
When input data are submitted from HTML forms, you often need to assign some default values to the inputs
|
||||
if they are empty. You can do so by using the [default](tutorial-core-validators.md#default) validator. For example,
|
||||
@ -229,7 +229,7 @@ with a PHP callable. For example,
|
||||
`required`, and `trim` validators will handle empty inputs.
|
||||
|
||||
|
||||
## Ad Hoc Validation <a name="ad-hoc-validation"></a>
|
||||
## Ad Hoc Validation <span id="ad-hoc-validation"></span>
|
||||
|
||||
Sometimes you need to do *ad hoc validation* for values that are not bound to any model.
|
||||
|
||||
@ -298,13 +298,13 @@ You may also access the dynamic attributes defined through the model instance, e
|
||||
`$model->name` and `$model->email`.
|
||||
|
||||
|
||||
## Creating Validators <a name="creating-validators"></a>
|
||||
## Creating Validators <span id="creating-validators"></span>
|
||||
|
||||
Besides using the [core validators](tutorial-core-validators.md) included in the Yii releases, you may also
|
||||
create your own validators. You may create inline validators or standalone validators.
|
||||
|
||||
|
||||
### Inline Validators <a name="inline-validators"></a>
|
||||
### Inline Validators <span id="inline-validators"></span>
|
||||
|
||||
An inline validator is one defined in terms of a model method or an anonymous function. The signature of
|
||||
the method/function is:
|
||||
@ -366,7 +366,7 @@ class MyForm extends Model
|
||||
> ```
|
||||
|
||||
|
||||
### Standalone Validators <a name="standalone-validators"></a>
|
||||
### Standalone Validators <span id="standalone-validators"></span>
|
||||
|
||||
A standalone validator is a class extending [[yii\validators\Validator]] or its child class. You may implement
|
||||
its validation logic by overriding the [[yii\validators\Validator::validateAttribute()]] method. If an attribute
|
||||
@ -395,7 +395,7 @@ instead of `validateAttribute()` and `validate()` because by default the latter
|
||||
by calling `validateValue()`.
|
||||
|
||||
|
||||
## Client-Side Validation <a name="client-side-validation"></a>
|
||||
## Client-Side Validation <span id="client-side-validation"></span>
|
||||
|
||||
Client-side validation based on JavaScript is desirable when end users provide inputs via HTML forms, because
|
||||
it allows users to find out input errors faster and thus provides a better user experience. You may use or implement
|
||||
@ -407,7 +407,7 @@ a validator that supports client-side validation *in addition to* server-side va
|
||||
described in the previous subsections.
|
||||
|
||||
|
||||
### Using Client-Side Validation <a name="using-client-side-validation"></a>
|
||||
### Using Client-Side Validation <span id="using-client-side-validation"></span>
|
||||
|
||||
Many [core validators](tutorial-core-validators.md) support client-side validation out-of-the-box. All you need to do
|
||||
is just use [[yii\widgets\ActiveForm]] to build your HTML forms. For example, `LoginForm` below declares two
|
||||
@ -470,7 +470,7 @@ validation of individual input fields by configuring their [[yii\widgets\ActiveF
|
||||
property to be false.
|
||||
|
||||
|
||||
### Implementing Client-Side Validation <a name="implementing-client-side-validation"></a>
|
||||
### Implementing Client-Side Validation <span id="implementing-client-side-validation"></span>
|
||||
|
||||
To create a validator that supports client-side validation, you should implement the
|
||||
[[yii\validators\Validator::clientValidateAttribute()]] method which returns a piece of JavaScript code
|
||||
@ -530,7 +530,7 @@ JS;
|
||||
> ]
|
||||
> ```
|
||||
|
||||
### Deferred Validation <a name="deferred-validation"></a>
|
||||
### Deferred Validation <span id="deferred-validation"></span>
|
||||
|
||||
If you need to perform asynchronous client-side validation, you can create [Deferred objects](http://api.jquery.com/category/deferred-object/).
|
||||
For example, to perform a custom AJAX validation, you can use the following code:
|
||||
@ -606,7 +606,7 @@ JS;
|
||||
```
|
||||
|
||||
|
||||
### AJAX Validation <a name="ajax-validation"></a>
|
||||
### AJAX Validation <span id="ajax-validation"></span>
|
||||
|
||||
Some validations can only be done on the server side, because only the server has the necessary information.
|
||||
For example, to validate if a username is unique or not, it is necessary to check the user table on the server side.
|
||||
|
Reference in New Issue
Block a user