Theme: What is a validation id on adult dating sites
What is a validation id on adult dating sites | 18 yr old girls dating men in their 30s |
What is a validation id on adult dating sites | Star wars afraid of inter interracial dating |
What is a validation id on adult dating sites | Dating app requires application based on instagram |
What is a validation id on adult dating sites | Largest adult dating site |
What is a validation id on adult dating sites | Dating app cofcee |
Validating Input ¶
As a rule of thumb, you should never trust the data received from end users and should always validate it before putting it to good use.
Given a model populated with user inputs, you can validate the inputs by calling the yii\base\Model::validate() method. The method will return a boolean value indicating whether the validation succeeded or not. If not, you may get the error messages from the yii\base\Model::$errors property. For example,
Declaring Rules ¶
To make 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 the validation rules for the model are declared:
The rules() method should return an array of rules, each of which is an array of the following format:
For each rule you must specify at least which attributes the rule applies to and what is the type of the rule. You can specify the rule type in one of the following forms:
- the alias of a core validator, such as , , , etc. Please refer to the Core Validators for the complete list of core validators.
- the name of a validation method in the model class, or an anonymous function. Please refer to the Inline Validators subsection for more details.
- a fully qualified validator class name. Please refer to the Standalone Validators subsection for more details.
A rule can be used to validate one or multiple attributes, and an attribute may be validated by one or multiple rules. A rule may be applied in certain scenarios only by specifying the option. If you do not specify an option, it means the rule will be applied to all scenarios.
When the method is called, it does the following steps to perform validation:
- Determine which attributes should be validated by getting the attribute list from yii\base\Model::scenarios() using the current scenario. These attributes are called active attributes.
- Determine which validation rules should be used by getting the rule list from yii\base\Model::rules() using the current scenario. These rules are called active rules.
- Use each active rule to validate each active attribute which is associated with the rule. The validation rules are evaluated in the order they are listed.
According to the above validation steps, an attribute will be validated if and only if it is an active attribute declared in and is associated with one or multiple active rules declared in .
Note: It is handy to give names to rules i.e.
You can use it in a child model:
Customizing Error Messages ¶
Most validators have default error messages that will be added to the model being validated when its attributes fail the validation. For example, the required validator will add a message "Username cannot be blank." to a model when the attribute fails the rule using this validator.
You can customize the error message of a rule by specifying the property when declaring the rule, like the following,
Some validators may support additional error messages to more precisely describe different causes of validation failures. For example, the number validator supports tooBig and tooSmall to describe the validation failure when the value being validated is too big and too small, respectively. You may configure these error messages like configuring other properties of validators in a validation rule.
Validation Events ¶
When yii\base\Model::validate() is called, it will call two methods that you may override to customize the validation process:
Conditional Validation ¶
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 when property to define such conditions. For example,
The when property takes a PHP callable with the following signature:
If you also need to support client-side conditional validation, you should configure the whenClient property which takes a string representing a JavaScript function whose return value determines whether to apply the rule or not. For example,
Data Filtering ¶
User inputs often need to be filtered or preprocessed. For example, you may want to trim the spaces around the input. You may use validation rules to achieve this goal.
The following examples shows how to trim the spaces in the inputs and turn empty inputs into nulls by using the trim and default core validators:
You may also use the more general filter validator to perform more complex data filtering.
As you can see, these validation rules do not really validate the inputs. Instead, they will process the values and save them back to the attributes being validated.
A complete processing of user input is shown in the following example code, which will ensure only integer values are stored in an attribute:
The above code will perform the following operations on the input:
- Trim whitespace from the input value.
- Make sure empty input is stored as in the database; we differentiate between a value being "not set" and the actual value . If is not allowed you can set another default value here.
- Validate that the value is an integer greater than 0 if it is not empty. Normal validators have $skipOnEmpty set to .
- Make sure the value is of type integer, e.g. casting a string to integer . Here we set $skipOnEmpty to , which is by default on the filter validator.
Handling Empty Inputs ¶
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 validator. For example,
By default, an input is considered empty if its value is an empty string, an empty array or a . You may customize the default empty detection logic by configuring the yii\validators\Validator::isEmpty() property with a PHP callable. For example,
Note: Most validators do not handle empty inputs if their yii\validators\Validator::$skipOnEmpty property takes the default value . They will simply be skipped during validation if their associated attributes receive empty inputs. Among the core validators, only the , , , , and validators will handle empty inputs.
Ad Hoc Validation ¶
Sometimes you need to do ad hoc validation for values that are not bound to any model.
If you only need to perform one type of validation (e.g. validating email addresses), you may call the validate() method of the desired validator, like the following:
Note: Not all validators support this type of validation. An example is the unique core validator which is designed to work with a model only.
Note: The yii\base\Validator::skipOnEmpty property is used for yii\base\Model validation only. Using it without a model has no effect.
If you need to perform multiple validations against several values, you can use yii\base\DynamicModel which supports declaring both attributes and rules on the fly. Its usage is like the following:
The yii\base\DynamicModel::validateData() method creates an instance of , defines the attributes using the given data ( and in this example), and then calls yii\base\Model::validate() with the given rules.
Alternatively, you may use the following more "classic" syntax to perform ad hoc data validation:
After validation, you can check if the validation succeeded or not by calling the hasErrors() method, and then get the validation errors from the errors property, like you do with a normal model. You may also access the dynamic attributes defined through the model instance, e.g., and .
Creating Validators ¶
Besides using the core validators included in the Yii releases, you may also create your own validators. You may create inline validators or standalone validators.
Inline Validators ¶
An inline validator is one defined in terms of a model method or an anonymous function. The signature of the method/function is:
If an attribute fails the validation, the method/function should call yii\base\Model::addError() to save the error message in the model so that it can be retrieved back later to present to end users.
Below are some examples:
Note: Since version 2.0.11 you can use yii\validators\InlineValidator::addError() for adding errors instead. That way the error message can be formatted using yii\i18n\I18N::format() right away. Use and in the error message to refer to an attribute label (no need to get it manually) and attribute value accordingly:
Note: By default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules. If you want to make sure a rule is always applied, you may configure the skipOnEmpty and/or skipOnError properties to be in the rule declarations. For example:
Standalone Validators ¶
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 fails the validation, call yii\base\Model::addError() to save the error message in the model, like you do with inline validators.
For example, the inline validator above could be moved into new [[components/validators/CountryValidator]] class. In this case we can use yii\validators\Validator::addError() to set customized message for the model.
If you want your validator to support validating a value without a model, you should also override yii\validators\Validator::validate(). You may also override yii\validators\Validator::validateValue() instead of and because by default the latter two methods are implemented by calling .
Below is an example of how you could use the above validator class within your model.
Multiple Attributes Validation ¶
Sometimes validators involve multiple attributes. Consider the following form:
Creating validator ¶
Let's say we need to check if the family income is enough for children. We can create inline validator for that which will run only when is more than 0.
Note that we can't use all validated attributes () when attaching validator. This is because the same validator will run for each attribute (3 times in total) and we only need to run it once for the whole attribute set.
You can use any of these attributes instead (or use what you think is the most relevant):
Implementation of can be like this:
You can ignore parameter because validation is not related to just one attribute.
Adding errors ¶
Adding error in case of multiple attributes can vary depending on desired form design:
- Select the most relevant field in your opinion and add error to it's attribute:
- Select multiple important relevant attributes or all attributes and add the same error message to them. We can store message in separate variable before passing it to to keep code DRY.
Or use a loop:
- Add a common error (not related to particular attribute). We can use the not existing attribute name for adding error, for example , because attribute existence is not checked at that point.
As a result, we will not see error message near form fields. To display it, we can include the error summary in view:
Note: Creating validator which validates multiple attributes at once is well described in the community cookbook.
Client-Side Validation ¶
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 a validator that supports client-side validation in addition to server-side validation.
Info: While client-side validation is desirable, it is not a must. Its main purpose is to provide users with a better experience. Similar to input data coming from end users, you should never trust client-side validation. For this reason, you should always perform server-side validation by calling yii\base\Model::validate(), as described in the previous subsections.
Using Client-Side Validation ¶
Many core validators 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, below declares two rules: one uses the required core validator which is supported on both client and server-sides; the other uses the inline validator which is only supported on the server side.
The HTML form built by the following code contains two input fields and . If you submit the form without entering anything, you will find the error messages requiring you to enter something appear right away without any communication with the server.
Behind the scene, yii\widgets\ActiveForm will read the validation rules declared in the model and generate appropriate JavaScript code for validators that support client-side validation. When a user changes the value of an input field or submit the form, the client-side validation JavaScript will be triggered.
If you want to turn off client-side validation completely, you may configure the yii\widgets\ActiveForm::$enableClientValidation property to be . You may also turn off client-side validation of individual input fields by configuring their yii\widgets\ActiveField::$enableClientValidation property to be false. When is configured at both the input field level and the form level, the former will take precedence.
Info: Since version 2.0.11 all validators extending from yii\validators\Validator receive client-side options from separate method - yii\validators\Validator::getClientOptions(). You can use it:
- if you want to implement your own custom client-side validation but leave the synchronization with server-side validator options;
- to extend or customize to fit your specific needs:
Implementing Client-Side Validation ¶
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 that performs the validation on the client-side. Within the JavaScript code, you may use the following predefined variables:
- : the name of the attribute being validated.
- : the value being validated.
- : an array used to hold the validation error messages for the attribute.
- : an array which deferred objects can be pushed into (explained in the next subsection).
In the following example, we create a which validates if an input is a valid status input against the existing status data. The validator supports both server-side and client-side validation.
Tip: The above code is given mainly to demonstrate how to support client-side validation. In practice, you may use the in core validator to achieve the same goal. You may write the validation rule like the following:
Tip: If you need to work with client validation manually i.e. dynamically add fields or do some custom UI logic, refer to Working with ActiveForm via JavaScript in Yii 2.0 Cookbook.
Deferred Validation ¶
If you need to perform asynchronous client-side validation, you can create Deferred objects. For example, to perform a custom AJAX validation, you can use the following code:
In the above, the variable is provided by Yii, which is an array of Deferred objects. The jQuery method creates a Deferred object which is pushed to the array.
You can also explicitly create a Deferred object and call its method when the asynchronous callback is hit. The following example shows how to validate the dimensions of an uploaded image file on the client-side.
Note: The method must be called after the attribute has been validated. Otherwise the main form validation will not complete.
For simplicity, the array is equipped with a shortcut method which automatically creates a Deferred object and adds it to the array. Using this method, you can simplify the above example as follows,
AJAX Validation ¶
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. You can use AJAX-based validation in this case. It will trigger an AJAX request in the background to validate the input while keeping the same user experience as the regular client-side validation.
To enable AJAX validation for a single input field, configure the enableAjaxValidation property of that field to be and specify a unique form :
To enable AJAX validation for all inputs of the form, configure enableAjaxValidation to be at the form level:
Note: When the property is configured at both the input field level and the form level, the former will take precedence.
You also need to prepare the server so that it can handle the AJAX validation requests. This can be achieved by a code snippet like the following in the controller actions:
The above code will check whether the current request is an AJAX. If yes, it will respond to this request by running the validation and returning the errors in JSON format.
Info: You can also use Deferred Validation to perform AJAX validation. However, the AJAX validation feature described here is more systematic and requires less coding effort.
When both and are set to , AJAX validation request will be triggered only after the successful client validation. Note that in case of validating a single field that happens if either , or is set to , AJAX request will be sent when the field in question alone successfully passes client validation.
-
-