symfony: [Form] Prevent model update if form validation fails

As of Symfony 2.1, each field supports custom constraints in the “constraints” option. Constraints in this option should (probably) prevent the appropriate field in the model from being updated. Updating the model before launching validation was repeatedly criticized in Symfony2. This change could give the best of both worlds.

Example:

<?php

$builder->add('ip', 'text', array(
    // setIp() will not be called if the regex fails
    'constraints' => new Regex('~^(\d{3}\.){3}\d{3}/\d{1,3}$~'),
));

I’m not sure yet whether this can be implemented at all. Thoughts?

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 56 (37 by maintainers)

Most upvoted comments

@stof A fair point. It can be worked around, but I realise now if the core of the issue (not setting in the first place) is being solved, this becomes somewhat unnecessary.

@bschussek IMO it’s the form that’s being validated, so if the form is invalid by way of any constraints failing in its children, no data should be set.

We cannot generically roll back side effects of the setters (for example internal counters that are incremented upon each call etc.).