This is part of my entity class :

/**
 * @var integer
 *
 * @ORM\Column(name="student", type="integer", nullable=true)
 */
private $student;

This part of my form class :

 $builder
    ->add('student', 'choice', ['label'=> false,
    'expanded' => true,
    'choices' => (Array)new StudentEnum(),
   ])
        ;

Ad this is output :

<input id="xxxxx_0" type="radio" value="4" required="required" name="xxxxx[student]">
<label class="required" for="xxxxxV_student_0">Nie</label>

...

My problem is that my input tag should not have attribute "required" becouse I have set nullable=true in entity.

有帮助吗?

解决方案

The solution is required => false and empty_value => false

$builder
        ->add('student', 'choice', [
                'label'=> false,
                'expanded' => true,
                'choices' => (Array)new StudentEnum(),
                'required' => false,
                'empty_value' => false
        ]);

其他提示

As described here,

required
type: Boolean default: true

required option default value is set to true, so you should then set it to false.

builder->add('student', 'choice', array(
          'label'=> false,
          'expanded' => true,
          'required' => false,
          //...
   ))
;

Also, you can read from documentation that,

This is superficial and independent from validation. At best, if you let Symfony guess your field type, then the value of this option will be guessed from your validation information.

You need then to set a validation rule that take into account the fact that your field should not be required in order to let your form set the right value of required.

This may probably help.

Since Symfony 3.0 empty_value is removed and you need to use placeholder instead:

 $builder
        ->add(
              'student', 
              'choice', 
              [
                'label'=> false,
                'expanded' => true,
                'choices' => (Array)new StudentEnum(),
                'required' => false,
                'placeholder' => null
              ]
         );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top