如何将验证组添加到RepeatedType?我想在约束验证期间为特定组启用RepeatedType验证(例如' updatePasswords')。
这是我的表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('updatePasswordCheckBox', CheckboxType::class)
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat password'),
'invalid_message' => "Passwords don't match!",
'groups' => "updatePasswordChecked",
))
->add('save', SubmitType::class, array('label' => 'Save'));
}
更新: 根据密码的状态复选框,将触发相应的验证组:
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'ImmoBundle\Entity\User',
'csrf_protection' => true,
'validation_groups' => function(FormInterface $form){
$data = $form->getData();
if ($data->getUpdatePasswordCheckBox() == 1)
{
return array('Default', 'updatePasswordChecked');
}
else
{
return array('Default', 'update');
}
}
));
}