Joke Collection Website - Public benefit messages - What are the custom validation rules for rules in yii2?
What are the custom validation rules for rules in yii2?
The requirement is that when the value of A selected by the user is equal to 1, the value of B must be filled in. When the value of a is equal to 2, it doesn't matter if the value of b is not written. Among them, a is required.
First, add the following rules to its association model.
/**
* @inheritdoc
*/
Common function rule ()
{
Return [
[['B'],' requiredByASpecial'],
];
}
Then implement the requiredByASpecial method in the model.
/**
* custom authentication b
*/
Public function required by aspect ($ attribute, $params)
{
If ($ this->; A== 1)
{
If ($ this->; B=== ' ')
$ this-& gt; Add an error ($ attribute, "The value of b cannot be empty." );
}
}
When a is equal to 1 and the value of b is empty, the error message "The value of b cannot be empty" is returned.
- Previous article:Oppo SMS has no prompt tone.
- Next article:How to query historical bills by SMS?
- Related articles
- Don't you understand the result after filming? Dr. Rainbow teaches you how to read video reports.
- Spring Festival composition
- What are the reverse documentary software?
- Words describing the opening of an old shop
- What should my family do if I can't receive Alipay bank card binding verification SMS?
- When a couple says goodbye, does it mean the complete end between them?
- Please recommend a better container transportation system?
- 2022 Xiaohongshu non-mainstream sad copywriting
- What's the matter with the message that it hasn't been sent yet?
- Hello, how can I send a text message to 10086 to open the data traffic overlay package (10 yuan, Beijing Mobile)?