Prevent multiple validation errors

Discussion relating to version 1.2.x stable

Prevent multiple validation errors

Postby moldcraft » Tue Jan 01, 2013 9:03 pm

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):
Image

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.
moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm

Re: Prevent multiple validation errors

Postby moldcraft » Wed Jan 02, 2013 10:45 am

Up
moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm

Re: Prevent multiple validation errors

Postby moldcraft » Thu Jan 03, 2013 10:32 pm

mm, your response on this topic will affect the way my project will grow and what solution I will use in all my forms
somebody?
moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm

Re: Prevent multiple validation errors

Postby moldcraft » Wed Jan 09, 2013 7:17 pm

moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm

Re: Prevent multiple validation errors

Postby moldcraft » Mon Jan 14, 2013 4:21 pm

I found here http://stackoverflow.com/questions/11627337 how to set group_sequence in yml. But how to set this in dynamic forms (my case) ?
moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm

Re: Prevent multiple validation errors

Postby gazgaz » Tue Jan 15, 2013 10:51 am

Hi i see your message on one of my precedent topics.

I can't help you for your problem, i haven't really understand your problem.

But for the form, i use @assert on the entity for saying wich type is notblank for exemple.
gazgaz
Junior Member
 
Posts: 15
Joined: Fri Nov 30, 2012 10:29 am

Re: Prevent multiple validation errors

Postby moldcraft » Tue Jan 15, 2013 12:12 pm

gazgaz wrote:Hi i see your message on one of my precedent topics.

I can't help you for your problem, i haven't really understand your problem.

But for the form, i use @assert on the entity for saying wich type is notblank for exemple.


I do not use @assert because I have no entity, my form is dynamic, I pass constraints classes directly to each field.
My problem is: if I set two constraints for one field, then both makes validation and creates two error messages for one filed. I need second constraint to be skipped if first generates an error/violation. I found how to do this in yml config via group sequence, but I can't find how to set the group sequence parameter in dynamic forms
moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm

Re: Prevent multiple validation errors

Postby moldcraft » Sat Feb 23, 2013 12:18 am

Problem was accepted and will be solved in the future here
Thanx

(moderator, close this topic please)
moldcraft
Junior Member
 
Posts: 19
Joined: Wed Oct 03, 2012 5:05 pm


Return to symfony 1.2

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

cron