fre5h/DoctrineEnumBundle

"Not a valid choice" on partial forms

Closed this issue · 3 comments

dehy commented

Hi there!

It seems DoctrineEnumBundle's @DoctrineAssert does not validate correctly form fields that are not included in partial forms of an entity.

Imagine you have a Player entity with name and position properties. Creating and updating such player with a full form (with name and position fields) works correctly. But if I create a "partial form" with only the name field, the validation fails on the position field with error "The value you selected is not a valid choice."

Here is a full demo project of the issue (6.7 MB, link valid until 22th october 2018) : https://framadrop.org/r/D8FGgrhJ9V#BbTxkmz3Z2VuzIRmaNLzDIKoOkZardDNIT0+498QaFk=

Just run the php bin/console server:run and point to http://localhost:8000/app_dev.php/demo then create a random player and edit it. Submit the full form : it works. Submit the partial form : it fails. More infos of the failure in the profiler http://localhost:8000/app_dev.php/_profiler

[edit]
Happens on symfony 3.4 with DoctrineEnumBundle 5.3.0. Not tested on sf 4.x and DoctrineEnumbundle 6.x

dehy commented

So in fact it seems to be a string/int with strict checking problem. When the field is visible, the value during validation is an int. When invisible (hidden) or non present, the value is a string. (in Validator/Constraints/EnumValidator.php)

If I add a string to int cast just before the parent::validate(...), if works. But this is a quick fix

   public function validate($value, Constraint $constraint)
   {
       [...]
       $constraint->choices = $entity::getValues();

       try {
           // Try to make it int
           $value = intval($value);
       } catch (\Exception $e) {
           // If it fails, do not touch it
       }

       parent::validate($value, $constraint);
   }

When will this be merged? :)

fre5h commented

@jorenvh1 do you have the same problem? what is the version of bundle and doctrine?