Cphalcon: [NFR] How to remove validator from element?

Created on 1 Nov 2015  路  8Comments  路  Source: phalcon/cphalcon

Elements allow add validators through method Element->addValidator()

We can even add a batch of validators. But there is no any possibility to remove validator on demand.

for example

I add some stack of validators for field 'Email'

/**
 * Class Users
 * @package PM
 * @Source("users")
 */
class Users extends \Phalcon\Mvc\Model
{
    /**
     * @Column(type="string", nullable=false, column="email")
     * @FormOptions(type=email)
     * @FormValidator(
     *  "Phalcon\Validation\Validator\PresenceOf":{'message': 'The e-mail field is required'},
     *  "Phalcon\Validation\Validator\Email":{'message': 'E-mail is not valid'},
     *  "Phalcon\Validation\Validator\Uniqueness":{'message': 'The E-mail is already taken', "model":"PM\Users"}
     * )
     * @var string
     */
    protected $email;
}

Later for example when I'll create an user everything goes fine.

Another story when I'll try to edit this user.
Validator 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.

So what I would like to use it in the some kind of way

$form = new EntityForm(Users::findFirst($id));
if ($this->request->isPost()) {
    if ($oldEmail == $newEmail) {
        $form->removeValidator($form->get('email'), ['Phalcon\\Validation\\Validator\\Db\\Uniqueness']);
    }
    if ($form->isValid($this->request->getPost()) != false) {
        //some code here
        return $this->response->redirect(["for" => "viewUser", "id" => $id]);
    }
}

Now I ended up with this

$form = new EntityForm(Users::findFirst($id));
if ($this->request->isPost()) {
    if ($oldEmail != $newEmail) {
        $form->get('email')->addValidator(new Uniqueness(
        ['message' => 'The E-mail is already taken', "table" => "users", "column" => "email"]
        ));
    }
    if ($form->isValid($this->request->getPost()) != false) {
        //some code here
        return $this->response->redirect(["for" => "viewUser", "id" => $id]);
    }
}

But 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

Or maybe someone will pointed me to the hidden parts of Phalcon which help me to do what I want.

Thanks

stale

Most helpful comment

Same question here... I can't believe there is no way to remove a validator...

All 8 comments

It's there any update regarding this request?

I think it's as important as removing an element from the form.

Thanks.

Same question here... I can't believe there is no way to remove a validator...

But Uniquqness validator is already ignoring current entity like is if it's found model and we are doing update, then it's adding id != :id:

This NFR may be outdated, because it is 1 year old. Probably current implementation of Uniqueness takes this into account. But main goal for this NFR is have possibility to remove validator from a stack in case if it does not need.

Any chance this gets looked at?

Thanks!

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

@sergeyklay This is still an issue, can we have this addressed?

Thanks!

Never mind, this does the job:

$element->addValidators([], false);
Was this page helpful?
0 / 5 - 0 ratings