Never had this problem before.
- Fill the form with a phone, leaving lastname blank
- Submit the form (and the validation groups become
Default
andCreate
) - The error "Last name is required." is mapped on the wrong
$phone
field, while should be mappend to$lastName
itself property
Can you reproduce the same issue?
$phone
property is in the Create
validation group, while $phone in Default
implicit group:
class User
{
/**
* @Assert\NotBlank(groups={"Create"}, message="Last name is required.")
*
* @var string
*/
protected $lastName;
/**
* @Assert\NotBlank(message="Phone is required.")
*
* @var string
*/
protected $phone;
}
I determine the validation groups based on submitted data:
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('lastName', 'text');
$builder->add('phone', 'text');
$builder->add('submit', 'submit');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults([
'required' => false,
'data_class' => 'Acme\HelloBundle\Entity\User',
'validation_groups' => function (FormInterface $form) {
return null === $form->getData()->getId()
? ['Default', 'Create']
: ['Default', 'Edit'];
}
]);
}
}