{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/9383","labels_url":"https://api.github.com/repos/yiisoft/yii2/issues/9383/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2/issues/9383/comments","events_url":"https://api.github.com/repos/yiisoft/yii2/issues/9383/events","html_url":"https://github.com/yiisoft/yii2/issues/9383","id":100332701,"number":9383,"title":"Ability to ActiveRecord method getErrors() return errors with input form id's as keys","user":{"login":"Akdmeh","id":3251266,"avatar_url":"https://avatars.githubusercontent.com/u/3251266?v=3","gravatar_id":"","url":"https://api.github.com/users/Akdmeh","html_url":"https://github.com/Akdmeh","followers_url":"https://api.github.com/users/Akdmeh/followers","following_url":"https://api.github.com/users/Akdmeh/following{/other_user}","gists_url":"https://api.github.com/users/Akdmeh/gists{/gist_id}","starred_url":"https://api.github.com/users/Akdmeh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Akdmeh/subscriptions","organizations_url":"https://api.github.com/users/Akdmeh/orgs","repos_url":"https://api.github.com/users/Akdmeh/repos","events_url":"https://api.github.com/users/Akdmeh/events{/privacy}","received_events_url":"https://api.github.com/users/Akdmeh/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-08-11T14:59:47Z","updated_at":"2015-08-11T15:30:31Z","closed_at":"2015-08-11T15:30:30Z","body":"Several days ago you added into yii 2.0.6 ability to show user-defined error messages in yiiActiveForm via JS:\r\nhttps://github.com/yiisoft/yii2/commit/11de09bf8b5b2186d75f8cddfb530f4511674f5d\r\nIt's great feature and I waited for it to use without dirty hacks.\r\nBut new JS method updateMessages() takes only input id's, which usually generated like [modelname-attributename] via Html::getInputId($model, $attribute).\r\nMaybe you should create new method in ActiveRecord, which will be return errors with keys, generated equally to that in input field?\r\nExample:\r\n\r\nSomewhere in controller:\r\n```php\r\nif(!$model->validate() && Yii::$app->request->isAjax){\r\nYii::$app->response->format = \\yii\\web\\Response::FORMAT_JSON;\r\nreturn $model_in->getErrorsWithInputIds();\r\n}\r\n```\r\nWhere **getErrorsWithInputIds** is hypothetical new method.\r\n\r\nThen somewhere in view:\r\n```javascript\r\nsuccess: function (response) {\r\n            if(response.status=='success') {\r\n                ...\r\n            } else {\r\n                $('#form').yiiActiveForm(\"updateMessages\", response)\r\n            }\r\n        }\r\n```\r\n\r\nFor now I must use some \"hack\":\r\n```php\r\nif(!$model->validate() && Yii::$app->request->isAjax){\r\nYii::$app->response->format = \\yii\\web\\Response::FORMAT_JSON;\r\n$errors=$model->getErrors();\r\n$new_error_array=[];\r\nforeach($errors as $k=>$v){\r\n    $new_error_array[\\yii\\helpers\\Html::getInputId($model, $k)]=$v;\r\n}\r\nreturn $new_error_array;\r\n}\r\n```\r\nIt's not a magical pill - it will creates problems if you, for example, redefined id attribute in Html::activeInput. That's why my examples isn't solution to all problems.\r\n\r\nSo, what you think about it, or I just don't see elephant in the living room and the better way exists?..\r\n\r\nP.S. Traditional for all ex-USSR user excuses for poor language"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2/issues/comments/129930689","html_url":"https://github.com/yiisoft/yii2/issues/9383#issuecomment-129930689","issue_url":"https://api.github.com/repos/yiisoft/yii2/issues/9383","id":129930689,"user":{"login":"Akdmeh","id":3251266,"avatar_url":"https://avatars.githubusercontent.com/u/3251266?v=3","gravatar_id":"","url":"https://api.github.com/users/Akdmeh","html_url":"https://github.com/Akdmeh","followers_url":"https://api.github.com/users/Akdmeh/followers","following_url":"https://api.github.com/users/Akdmeh/following{/other_user}","gists_url":"https://api.github.com/users/Akdmeh/gists{/gist_id}","starred_url":"https://api.github.com/users/Akdmeh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Akdmeh/subscriptions","organizations_url":"https://api.github.com/users/Akdmeh/orgs","repos_url":"https://api.github.com/users/Akdmeh/repos","events_url":"https://api.github.com/users/Akdmeh/events{/privacy}","received_events_url":"https://api.github.com/users/Akdmeh/received_events","type":"User","site_admin":false},"created_at":"2015-08-11T15:30:30Z","updated_at":"2015-08-11T15:30:30Z","body":"Sorry, now I understand. I forget about ActiveForm::validate.\r\nStupid misunderstand.\r\nMaybe it's time to sleep for me.\r\nMy regrets."}}