Custom rule validation not working in Yii2 Form that extends Model

Hi! I’ve been working with yii2 for quite a while but I’m new in this forum. I’ve searched through the topics but couldn’t find the solution for my problem so I post this topic. Hopefully it’s not a repost.

I have an issue with custom validations. There is a model called Contact Form like this:

    class ContactForm extends Model


        public $name;

        public $email;

        public $subject;

        public $body;

    	public $response;

        public $response2;

        public function rules()


            return [

                [['name', 'email', 'subject', 'body'], 'required'],

                ['email', 'email'],

	    	['response', function ($attribute, $params) {

                    $this->addError($attribute, 'Wrong response 1');

                },  'skipOnError' => false],

    		['response2', 'validresponse2'],




        public function validresponse2($attribute, $params) {

    	    $this->addError($attribute, 'Wrong response 2');



There are 2 attributes (response and response2) which have custom validation.

Both custom validation are not working. They didn’t pop up an error message at all. When I put echo "asd; die(); inside those functions, They didn’t die too.

Here is how I validate the form in my controller

    $model = new ContactForm();

    $model->subject = "New Message";


    if (isset($_POST['ContactForm'])) {

        $model->attributes = $_POST['ContactForm'];

        $model->response = "";

        if ($model->validate()) {

            echo "validated successfully";




And it is always validated successfully

Is my rule setting wrong? I’ve done it using Yii 1 and it works fine.

It’s alright now, I’ve found the solution.

As suggested by someone, I set skipOnEmpty to false so that empty attribute will also trigger validation.

Hope this can help others with same issue as mine.