Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Implement Phalcon\Validation\Validator\Uniqueness. #1367

Closed
temuri416 opened this issue Oct 12, 2013 · 3 comments
Closed

[NFR] Implement Phalcon\Validation\Validator\Uniqueness. #1367

temuri416 opened this issue Oct 12, 2013 · 3 comments

Comments

@temuri416
Copy link
Contributor

Hi,

Currently, there is Phalcon\Mvc\Model\Validator\Uniqueness class that only works when invoked by Model validation event: https://phalcon-php-framework-documentation.readthedocs.org/en/latest/reference/models.html#validating-data-integrity.

IMO, there is a need for a standalone analogue of the same validator, which would work separate from a Model.

It should be used similar to Zend\Validator\Db\NoRecordExists (http://framework.zend.com/manual/2.2/en/modules/zend.validator.set.html#zend-validator-db-basic-usage).

Here's the use case.

  1. There is User Phalcon\Mvc model (with unique "email" field)
  2. There is Invitation Phalcon\Mvc model (with "recipient_email" field)
  3. Service members (derived from Users) can email out invitations for other users to join
  4. Service members should not be allowed to send emails to Users already in the system (identified by email address)
  5. Service members should not be allowed to resend invitations to the same email address more than once (case when first invitation was sent and before recipient could accept it - existing member sends another one)

Class skeletons:

class Invitation extends Phalcon\Mvc\Model
{
    public function validation()
    {
        $this->validate(new Uniqueness([
            'field' => $emailField,
            'message' => 'You have already invited that person - give them a chance to respond!' // Condition #5 above
        ]));
    }
}

class User extends Phalcon\Mvc\Model
{
    public function validation()
    {
        $this->validate(new Uniqueness([
            'field' => $emailField,
            'message' => 'Duplicate email.'
        ]));
    }
}

Now, there is a "Send Invitation" form, which prompts for invitee's email address. That form is "bound" to Invitation entity upon POST:

$invitation = Invitation::findFirst;
$form->bind($_POST, $invitation);
if ($form->isValid()) {
    $invitation->save();
}

Obviously, the form should fail validation, if provided email address is not unique in either User or Invitation models.

And this is exactly where there's a problem:

class FormInvite extends Form
{
    public function __construct()
    {
        $emailElement = new Email('email');
        $this->add($el);
    }
}

I would love to be able to attach the (non-existent) Phalcon\Validation\Validator\Uniqueness validator to $emailElement. Or, being able to reuse Phalcon\Mvc\Model\Validator\Uniqueness (which isn't possible because it is specifically, a model validator).

IMO, since Form has this amazing entity binding mechanism, I'd expect it would also validate POSTed values, knowing that model validation would eventually fail.

What are my options now, if I wanted to inform user about email uniqueness violation?

Well, two things could be done:

  1. I'd have to hook into form's ->isValid() phase and there - I'd have to run User::save(), check for Model validation errors, go back to FormInvite and manually set validation errors; or:
  2. I'd have to write lots of code that would duplicate Model's Uniqueness validator, to be able to attach it to Form's elements.

Nasty!

So, all that hassle can be avoided by doing one of the following (or, ideally - both!):

  1. Implement Phalcon\Validation\Validator\Uniqueness that can be attached to form elements; or
  2. Improve Form validation when an entity is bound, to run submitted values by the Model's validator.

Thoughts/comments?

Thank you,
Temuri

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@temuri416
Copy link
Contributor Author

@phalcon @sjinks

Guys, could you please comment - how much work would it be to make \Phalcon\Form auto-validate itself based on bound Model's required fields or set validator?

Should I wait until this is possibly implemented, or should I rather roll out my own standalone Uniqueness validator?

Thank you.

@phalcon
Copy link
Collaborator

phalcon commented Oct 19, 2013

At this moment, I think roll out your own standalone Uniqueness validator is the way to go, if you have problems implementing it in PHP please let me know

@sergeyklay
Copy link
Contributor

Fixed in the 2.1.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants