drfqfuhej48511519 2014-10-24 18:42
浏览 26

Symfony嵌入式表格条件验证

I have a form which contains three objects:

$builder
    ->add('customer', new CustomerType())
    ->add('shippingAddress', new AddressType())
    ->add('billingAddress', new AddressType())
    ->add('sameAsShipping', 'checkbox', ['mapped' => false])
;

Each of the embedded forms has their own validation constraints and they work. In my main form, I have cascade_validation => true so that all of the embedded form validation constraints are applied. This also works.

I am having trouble 'disabling' the validation on the billingAddress form if the sameAsShipping checkbox is enabled. I can't make the validation in the AddressType form conditional because it always needs to be enforced for the shippingAddress form.

  • 写回答

2条回答 默认 最新

  • duanliao6077 2015-02-14 12:28
    关注

    I've solved this same problem by using validation groups.

    First, this is important: use the validation_groups option in your AddressType to set the validation groups of every constraint of each field in the type:

    <?php
    
    use Symfony\Component\Validator\Constraints as Assert;
    use Symfony\Component\Form\FormBuilderInterface;
    
    class AddressType extends \Symfony\Component\Form\AbstractType
    {
        function buildForm(FormBuilderInterface $builder, array $options)
        {
            $groups = $options['validation_groups'];
    
            $builder->add('firstName', 'text', ['constraints' => new Assert\NotBlank(['groups' => $groups])]);
            $builder->add('lastName', 'text', ['constraints' => new Assert\NotBlank(['groups' => $groups])]);
        }
    }
    

    Then, in the parent form pass different validation groups to the two fields:

    <?php
    
    $formBuilder = $this->get('form.factory')
        ->createNamedBuilder('checkout', 'form', null, [
            'cascade_validation' => true,
        ])
        ->add('billingAddress', 'address', [
            'validation_groups' => 'billingAddress'
        ])
        ->add('shippingAddress', 'address', [
            'validation_groups' => 'shippingAddress'
        ]);
    

    Then, determine determine your validation groups by looking at the value of the checkbox.

    if ($request->request->get('sameAsShipping')) {
        $checkoutValidationGroups = ['Default', 'billingAddress'];
    } else {
        $checkoutValidationGroups = ['Default', 'billingAddress', 'shippingAddress'];
    }
    

    You can then validate only either the billingAddress or the shippingAddress, or both using the validation group mechanism.

    I chose to use a button:

    $formBuilder->add('submitButton', 'submit', ['validation_groups' => $checkoutValidationGroups]);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法