8000 Halt subsequent constraints on first failure · Issue #2947 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

Halt subsequent constraints on first failure #2947

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

Closed
satazor opened this issue Dec 22, 2011 · 10 comments
Closed

Halt subsequent constraints on first failure #2947

satazor opened this issue Dec 22, 2011 · 10 comments

Comments

@satazor
Copy link
satazor commented Dec 22, 2011

Let's say that an entity have the following constraints applied to a property:

/*
 * @Assert\NotBlank(
 *     message="The username cannot be blank."
 * )
 * @Assert\MinLength(
 *     limit=2,
 *     message="The username must have at least {{ limit }} characters."
 * )
 * @Assert\Regex(
 *     pattern="/^[a-z][a-z0-9\-_]+$/i",
 *     message="The username must start with a letter and can contain alphanumerical characters, -, and _."
 * )

When a form validates the propery:

  • If the property is blank, the min length and regex constraints are not applied
  • If the property is not blank but the the min length constraint fails, the regex constraint is still applied, resulting in two errors

I understand that this was the desired default behaviour, but for some use cases one would want to halt on first failure.
I remember that symfony1.x had the halt_on_error option for this, though I couldn't found any reference to achieve this in symfony2.

Is this an undocumented feature? or is this still not implemented?

@webmozart
Copy link
Contributor

This is indeed an undocumented feature and can be achieved using group sequences. In group sequences, you can define validation groups to be validated in order. Once a group fails, further groups won't be validated.

/** @GroupSequence({"Basic", "Strict"}) */
class MyClass 
{
    /**
     * @NotBlank(groups="Basic")
     * @Regex(pattern="...", groups="Strict")
     */
    private $property;
}

Does this solve your issue?

@stof
Copy link
Member
stof commented Jan 16, 2012

@bschussek you should contribute to the doc. I think even most core devs ignored about this feature.

@henrikbjorn
Copy link
Contributor

@bschussek the group sequence thing is that per property or will it be skipped for the whole object?

@webmozart
Copy link
Contributor

@henrikbjorn It's for the whole object. One group must validate completely (constraints on the object and all of its properties in this group) in order to allow for the next group to be validated.

@marcw
Copy link
Contributor
marcw commented Jan 16, 2012

@bschussek If you don't have time to contribute full-features docs, please at least write some wireframe the community could expand by hacking around. I'm using Symfony2 daily and didn't know about this feature.

@webmozart
Copy link
Contributor

@marcw I'll see what I can do. I have some time throughout the next days.

@marcw
Copy link
Contributor
marcw commented Jan 16, 2012

@bschussek thanks!

@ghost
Copy link
ghost commented Jan 16, 2013

I know how to set group_sequence for a form in yml config and for a form class. But how to set groupSequence in dynamic forms ?. My example:

$this->createFormBuilder(null, array(
    'validation_constraint' => new Collection(array(
        'randominput' => array(
            new Email(),
            new MyCustomConstraint()
        )
))
->add('randominput', 'text');

Help please (sorry for my english)

@ghost
Copy link
ghost commented Jan 17, 2013

I think it will be muuch easier and cleaner for me just to set one parameter for an input, for example ->add('randominput', 'text', array('break_validation_on_first_violation'=>true)
and not to worry anymore about setting groups for each constraint, carry about the order of group and constraints, setting group sequence for form. Anyway my groups will not have names like in your examples, I carry only about the order of constraints, so every time I will create forms with group sequence, it will be something like:

/** @GroupSequence({"g1", "g2", "g3", ...}) */
class MyClass 
{
    /**
     * @NotBlank(groups="g1")
     * @Regex(pattern="...", groups="g2")
     * @Email(groups="g3")
     * @...
     */
    private $property;
}

My idea is so simple: I have set some constraints for an input, I want they to be applied in order I set them in array and if some constraint finds a violation, skip the rest. Why do I have to validate thru the rest of the constraints because it's clear that the input value is not valid anymore?

@rybakit
Copy link
Contributor
rybakit commented Feb 4, 2013

@moldcraft, you may create a custom validator, which will stop at the first violation (example).

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

No branches or pull requests

6 participants
0