php - yii2 user-defined validation of mobile number -
i new yii2. want validate mobile number custom validation function. how can validate mobile no in yii2 , how can use user-defined rule yii2 , how can add error message attribute in yii2 after form post? in advance
you need edit model. lets have following model:
class user extends activerecord implements identityinterface { ... /** * @inheritdoc */ public function rules() { return [ [['email', 'password', 'id'], 'required'], [['email', 'username'], 'unique'], ['mobile', 'customvalidation'] //<----this custom validation } public function customvalidation(){ //perform validation here if(/*has error*/){ $this->adderror("mobile","your mobile number not valid."); } } }
the adderror method's first parameter atribute want add error , second parameter message want show.
hope helps ;)
Comments
Post a Comment