{"action":"closed","issue":{"url":"https://api.github.com/repos/phalcon/cphalcon/issues/11082","repository_url":"https://api.github.com/repos/phalcon/cphalcon","labels_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11082/labels{/name}","comments_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11082/comments","events_url":"https://api.github.com/repos/phalcon/cphalcon/issues/11082/events","html_url":"https://github.com/phalcon/cphalcon/issues/11082","id":114466773,"number":11082,"title":"[NFR] How to remove validator from element?","user":{"login":"valVk","id":1130626,"avatar_url":"https://avatars1.githubusercontent.com/u/1130626?v=4","gravatar_id":"","url":"https://api.github.com/users/valVk","html_url":"https://github.com/valVk","followers_url":"https://api.github.com/users/valVk/followers","following_url":"https://api.github.com/users/valVk/following{/other_user}","gists_url":"https://api.github.com/users/valVk/gists{/gist_id}","starred_url":"https://api.github.com/users/valVk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/valVk/subscriptions","organizations_url":"https://api.github.com/users/valVk/orgs","repos_url":"https://api.github.com/users/valVk/repos","events_url":"https://api.github.com/users/valVk/events{/privacy}","received_events_url":"https://api.github.com/users/valVk/received_events","type":"User","site_admin":false},"labels":[{"id":13573146,"url":"https://api.github.com/repos/phalcon/cphalcon/labels/New%20Feature%20Request","name":"New Feature Request","color":"0052cc","default":false},{"id":903580634,"url":"https://api.github.com/repos/phalcon/cphalcon/labels/stale","name":"stale","color":"ffffff","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2015-11-01T12:14:47Z","updated_at":"2018-04-17T04:43:10Z","closed_at":"2018-04-17T04:43:10Z","author_association":"NONE","body":"Elements allow add validators through method Element->[addValidator()](https://github.com/phalcon/cphalcon/blob/master/phalcon/forms/elementinterface.zep#L100)\n\nWe can even add a batch of validators. But there is no any possibility to remove validator on demand.\n\nfor example\n\nI add some stack of validators for field 'Email'\n\n``` php\n\n/**\n * Class Users\n * @package PM\n * @Source(\"users\")\n */\nclass Users extends \\Phalcon\\Mvc\\Model\n{\n    /**\n     * @Column(type=\"string\", nullable=false, column=\"email\")\n     * @FormOptions(type=email)\n     * @FormValidator(\n     *  \"Phalcon\\Validation\\Validator\\PresenceOf\":{'message': 'The e-mail field is required'},\n     *  \"Phalcon\\Validation\\Validator\\Email\":{'message': 'E-mail is not valid'},\n     *  \"Phalcon\\Validation\\Validator\\Uniqueness\":{'message': 'The E-mail is already taken', \"model\":\"PM\\Users\"}\n     * )\n     * @var string\n     */\n    protected $email;\n}\n\n```\n\nLater for example when I'll create an user everything goes fine.\n\nAnother story when I'll try to edit this user.\nValidator `Phalcon\\Validation\\Validator\\Uniqueness` failed every time I hit submit button, because user's email was not changed during edition process and it exists in DB.\n\nSo what I would like to use it in the some kind of way\n\n``` php\n$form = new EntityForm(Users::findFirst($id));\nif ($this->request->isPost()) {\n    if ($oldEmail == $newEmail) {\n        $form->removeValidator($form->get('email'), ['Phalcon\\\\Validation\\\\Validator\\\\Db\\\\Uniqueness']);\n    }\n    if ($form->isValid($this->request->getPost()) != false) {\n        //some code here\n        return $this->response->redirect([\"for\" => \"viewUser\", \"id\" => $id]);\n    }\n}\n```\n\nNow I ended up with this\n\n``` php\n$form = new EntityForm(Users::findFirst($id));\nif ($this->request->isPost()) {\n    if ($oldEmail != $newEmail) {\n        $form->get('email')->addValidator(new Uniqueness(\n        ['message' => 'The E-mail is already taken', \"table\" => \"users\", \"column\" => \"email\"]\n        ));\n    }\n    if ($form->isValid($this->request->getPost()) != false) {\n        //some code here\n        return $this->response->redirect([\"for\" => \"viewUser\", \"id\" => $id]);\n    }\n}\n```\n\nBut I think It will be great to have possibility to remove validators on demand. And more convenient to remove that validation than add it every time it need\n\nOr maybe someone will pointed me to the hidden parts of Phalcon which help me to do what I want.\n\nThanks\n"}}
{"actor":{"display_login":"sergeyklay"}}