- Code: Select all
$this->createFormBuilder(null, array(
'validation_constraint' => new Collection(array(
'randominput' => array(
new NotBlank(),
new Email(),
new MyCustomConstraint()
)
))
->add('randominput', 'text');
Submit result (with required attribute removed from html, with firebug):

The message from Email() constraint does not apper because inside that constraint exists a piece of code, witch I think is a clone/hardcode for NotBlank() constraint
- Code: Select all
if (null === $value || '' === $value) {
return;
}
I think the Email() constraint should be a child of NotBlank()...
I want MyCustomConstraint() to not be executed if NotBlank() founds a violation. So it will be good if will be some option, for example "breakNextConstraintExecutionOnFirstViolation" => true. So if I set 10 constraints for one field, and 3rd constraint sets a violation, then next 7 constraints will not be executed.
If that kind of logic/option does not exists in symfony2, it will be good if I can access the 'validation' service from MyCustomConstraint class and reuse existing constraints but not write hardcode for each new constraint:
- Code: Select all
class MyCustomConstraintValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
// use the validator to validate the value, not hardcode
if (count( $this->get('validator')->validateValue(
$value,
new NotBlank()
)) > 0)
{
return;
}
$this->context->addViolation('MyCustomConstraint Message...');
}
}
So my question is: What should I do to prevent multiple violation messages for one filed and do not use in every new constraint hardcode?
P.S. In my previous projects (not on symfony), I made forms showing only one error message. So user completes fields one by one and see only one error but not submitting the form and that fills every fields with red errors (and scares some users). But now at least I want to resolve this issue.
