Shubham Dixit Shubham Dixit - 6 months ago 9x
PHP Question

How to use email validation inside a custom validation function?

I have created a custom validation function. Now I want to validate email inside that function, how can I do that?
Below is my code:

public function rules()
return [
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'validateEmail'],

public function validateEmail($attribute, $params) {
$this->addError($attribute,'Phone number should be of 10 digits');
else{// Email validation using emailvalidator.}



You can call the email validator directly. The message will be added to the model when validateAttribute is used. If you want to use the default message you can leave it out:

public function validateEmail($attribute, $params) {
    else {
        $emailValidator = new \yii\validators\EmailValidator();
        $emailValidator->message = "Invalid Email";
        $emailValidator->validateAttribute($this, $attribute));