{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/6744","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/6744/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/6744/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/6744/events","html_url":"https://github.com/yiisoft/yii2/issues/6744","id":53327882,"number":6744,"title":"How to force validate when using \"when\" in conditional validation?","user":{"login":"hafeyang","id":745503,"avatar_url":"https://avatars.githubusercontent.com/u/745503?v=3","gravatar_id":"","url":"https://api.github.com/users/hafeyang","html_url":"https://github.com/hafeyang","followers_url":"https://api.github.com/users/hafeyang/followers","following_url":"https://api.github.com/users/hafeyang/following{/other_user}","gists_url":"https://api.github.com/users/hafeyang/gists{/gist_id}","starred_url":"https://api.github.com/users/hafeyang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hafeyang/subscriptions","organizations_url":"https://api.github.com/users/hafeyang/orgs","repos_url":"https://api.github.com/users/hafeyang/repos","events_url":"https://api.github.com/users/hafeyang/events{/privacy}","received_events_url":"https://api.github.com/users/hafeyang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-01-04T06:54:40Z","updated_at":"2015-11-07T13:42:56Z","closed_at":null,"body":"my Model code:\r\n\r\n```\r\n//...\r\npublic function rules()\r\n    {\r\n        return [\r\n            ['companyId', 'required', 'when' => function($model) {\r\n                return $model->Grade == '1';\r\n            }, 'whenClient' => \"function (attribute, value) {\r\n                return $('#user-grade').val() == '1';\r\n            }\"],\r\n        ];\r\n    }\r\n//...\r\n```\r\n\r\nI want validate companyId as required when Grade ==\"1\"\r\n\r\nbut  it seems not work when user-grade changed  in browser,\r\n\r\neven $('#user-grade,#user-companyid').trigger(\"change\"),javascript validation dos not work.\r\n\r\nso I checked yii.activeForm.js:\r\n\r\n```\r\n//...\r\n   var validateAttribute = function ($form, attribute, forceValidate, validationDelay) {\r\n        var data = $form.data('yiiActiveForm');\r\n\r\n        if (forceValidate) {\r\n            attribute.status = 2;\r\n        }\r\n        $.each(data.attributes, function () {\r\n            if (this.value !== getValue($form, this)) { //  value not changed\r\n                this.status = 2;\r\n                forceValidate = true;\r\n            }\r\n        });\r\n        if (!forceValidate) { // value not changed , do not needs to validate\r\n            return;\r\n        }\r\n//...\r\n\r\n```\r\n\r\nso , I wonder how to force validate field value when using conditional validation?"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/154706915","html_url":"https://github.com/yiisoft/yii2/issues/6744#issuecomment-154706915","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/6744","id":154706915,"user":{"login":"uagrace","id":4890772,"avatar_url":"https://avatars.githubusercontent.com/u/4890772?v=3","gravatar_id":"","url":"https://api.github.com/users/uagrace","html_url":"https://github.com/uagrace","followers_url":"https://api.github.com/users/uagrace/followers","following_url":"https://api.github.com/users/uagrace/following{/other_user}","gists_url":"https://api.github.com/users/uagrace/gists{/gist_id}","starred_url":"https://api.github.com/users/uagrace/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uagrace/subscriptions","organizations_url":"https://api.github.com/users/uagrace/orgs","repos_url":"https://api.github.com/users/uagrace/repos","events_url":"https://api.github.com/users/uagrace/events{/privacy}","received_events_url":"https://api.github.com/users/uagrace/received_events","type":"User","site_admin":false},"created_at":"2015-11-07T13:42:56Z","updated_at":"2015-11-07T13:42:56Z","body":"$('#user-form').yiiActiveForm('validateAttribute', 'user-companyid');"}}