You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There is User Phalcon\Mvc model (with unique "email" field)
There is Invitation Phalcon\Mvc model (with "recipient_email" field)
Service members (derived from Users) can email out invitations for other users to join
Service members should not be allowed to send emails to Users already in the system (identified by email address)
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 extendsPhalcon\Mvc\Model
{
publicfunctionvalidation()
{
$this->validate(newUniqueness([
'field' => $emailField,
'message' => 'You have already invited that person - give them a chance to respond!'// Condition #5 above
]));
}
}
class User extendsPhalcon\Mvc\Model
{
publicfunctionvalidation()
{
$this->validate(newUniqueness([
'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
{
publicfunction__construct()
{
$emailElement = newEmail('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:
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:
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!):
Implement Phalcon\Validation\Validator\Uniqueness that can be attached to form elements; or
Improve Form validation when an entity is bound, to run submitted values by the Model's validator.
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?
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
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.
Class skeletons:
Now, there is a "Send Invitation" form, which prompts for invitee's email address. That form is "bound" to Invitation entity upon POST:
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:
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:
Nasty!
So, all that hassle can be avoided by doing one of the following (or, ideally - both!):
Thoughts/comments?
Thank you,
Temuri
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: