This is the login form model
class LoginForm extends Model{ public $account; public $password; /** * @return array the validation rules. */ public function rules() { return [ ['account','app\components\NameValidator','maxLen'=>12] ]; }}
App\components\namevalidator is a validator that I have customized:
namespace App\components;use Yii\validators\validator;class Namevalidator extends validator{public $minLen = 2; Public $maxLen = 14; Public $charset = ' utf-8 '; Public $message; Public Function Validatevalue ($value) {file_put_contents (' hello.txt ', ' 123 ');//test method is executed. $enPattern = '/[a-z]/i '; $zhPattern = '/^[\x{4e00}-\x{9fa5}]*$/u '; $valid =false; $zhStr =preg_replace ($enPattern, ", $value, -1, $count); $enLen = $count; $zhLen =mb_strlen ($zhStr, $this->charset); $len = $enLen + $zhLen * *; if ($len > $this->maxlen | | $len < $this->minlen) {if (! $this->message) $this->message= ' character count is not Correct '; }else if (!preg_match ($zhPattern, $zhStr)) {if (! $this->message) $this->message= ' character unqualified '; }else{$valid =true; } return $valid? Null: [$this->message,[]]; }}
In Passportcontroller, call LoginForm's Validate method for validation:
namespace app\controllers;use Yii;use yii\web\Controller;use app\models\LoginForm;class PassportController extends Controller{ public $enableCsrfValidation=false; public $layout='@app/views/passport/main.php'; public function actionLogin() { if(\yii::$app->request->isPost) { $login=new LoginForm(); $login->load(\yii::$app->request->post()); if($login->validate()) { echo 'valid pass'; }else{ echo $login->errors; } } $this->view->title='登录'; return $this->render('login'); }}
The result is that the Validatevalue method in my custom App\components\namevalidator class is not executed, and the 执行$login->validate()
valid pass string is always output in the Actionlogin method. Don't know what's going on here?
I know it's possible to do this with an inline validator, but I want to ask what's going on here? Why is the verification method not implemented here?
Reply content:
This is the login form model
class LoginForm extends Model{ public $account; public $password; /** * @return array the validation rules. */ public function rules() { return [ ['account','app\components\NameValidator','maxLen'=>12] ]; }}
App\components\namevalidator is a validator that I have customized:
namespace App\components;use Yii\validators\validator;class Namevalidator extends validator{public $minLen = 2; Public $maxLen = 14; Public $charset = ' utf-8 '; Public $message; Public Function Validatevalue ($value) {file_put_contents (' hello.txt ', ' 123 ');//test method is executed. $enPattern = '/[a-z]/i '; $zhPattern = '/^[\x{4e00}-\x{9fa5}]*$/u '; $valid =false; $zhStr =preg_replace ($enPattern, ", $value, -1, $count); $enLen = $count; $zhLen =mb_strlen ($zhStr, $this->charset); $len = $enLen + $zhLen * *; if ($len > $this->maxlen | | $len < $this->minlen) {if (! $this->message) $this->message= ' character count is not Correct '; }else if (!preg_match ($zhPattern, $zhStr)) {if (! $this->message) $this->message= ' character unqualified '; }else{$valid =true; } return $valid? Null: [$this->message,[]]; }}
In Passportcontroller, call LoginForm's Validate method for validation:
namespace app\controllers;use Yii;use yii\web\Controller;use app\models\LoginForm;class PassportController extends Controller{ public $enableCsrfValidation=false; public $layout='@app/views/passport/main.php'; public function actionLogin() { if(\yii::$app->request->isPost) { $login=new LoginForm(); $login->load(\yii::$app->request->post()); if($login->validate()) { echo 'valid pass'; }else{ echo $login->errors; } } $this->view->title='登录'; return $this->render('login'); }}
The result is that the Validatevalue method in my custom App\components\namevalidator class is not executed, and the 执行$login->validate()
valid pass string is always output in the Actionlogin method. Don't know what's going on here?
I know it's possible to do this with an inline validator, but I want to ask what's going on here? Why is the verification method not implemented here?
Validator::validateattribute () and Validator::validatevalue () are two ways to implement
Take a closer look at the document
Commissioning can be made with Yii::log ()