The sfValidatorPropelUnique in symfony
Nov17
Today, I wanted to do a signup form but I wanted distinct email addresses to be recorded. After some googlisations, I’ve spotted two problems.
As my field was an email address I thought something like “damn easy, I just have to use something like this: ”
-
new sfValidatorEmail(); ,
-
new sfValidatorPropelUnique(array(
-
'model' => $this->getModelName(),
-
'column' => 'email_1'
-
),
-
array(
-
'invalid' => 'This email address is already assigned to an account.',
-
)
-
)
-
));
Though, after few tries I had an error that I’ve never seen before, saying that it has to be used as a postValidator. Before I understood how to deal with that, I’ve figured out something: I should simply add in my schema
-
index: unique
in my email field… Damn… But since I’m definitly 2 cool, here is the correct syntax to use in your UserForm.class.php (basically, it shouldn’t be used if you defined correctly your schema.yml though!) :
-
new sfValidatorPropelUnique(array(
-
'model' => 'User',
-
'column' => array('email_1')
-
),
-
array(
-
'invalid' => 'An account is already registered with this email address.',
-
)
-
));
Once again, thanks symfony to allow us escaping from so much troubles !
