Note that there are some explanatory texts on larger screens.

plurals
  1. POsymfony2 choice form valid
    primarykey
    data
    text
    <p>I have got a simple smyfony2 form with one choices element. When I choose "kerosin" or "diesel" the form won't validate, what is correct. When I won't choose any of the three options and submit the form empty, $form->validate() will return true, but it shouldn't. Any ideas? Using the HTML5 required is not a solution for me.</p> <p>This is my Form AbstractType:</p> <pre><code>public function buildForm(FormBuilderInterface $builder, array $options) { // Form erzeugen $builder-&gt;add('treibstoff', 'choice', array( 'choices' =&gt; array( 'kerosin' =&gt; 'form.quiz1.kerosin', 'benzin' =&gt; 'form.quiz1.benzin', 'diesel' =&gt; 'form.quiz1.diesel', ), 'multiple' =&gt; false, 'expanded' =&gt; true, 'label' =&gt; ' ' )) -&gt;getForm(); } public function setDefaultOptions(OptionsResolverInterface $resolver) { // Validierung erzeugen $collectionConstraint = new Collection(array( 'treibstoff' =&gt; array( new Choice(array( 'choices' =&gt; array('benzin'), 'message' =&gt; 'form.quiz.falscheantwort', 'strict' =&gt; true ) ) ) )); $resolver-&gt;setDefaults(array( 'validation_constraint' =&gt; $collectionConstraint )); } public function getName() { ... </code></pre> <p>Validation works like this:</p> <pre><code> if($Request-&gt;getMethod() == "POST") { $form-&gt;bind($Request); if($form-&gt;isValid()) { echo "valid"; </code></pre> <p>Thanks in advance.</p> <p>Edit:</p> <p>I changed the setDefaultOptions like suggested and added NotBlank. That worked out for me:</p> <pre><code>public function setDefaultOptions(OptionsResolverInterface $resolver) { // Validierung erzeugen $collectionConstraint = new Collection(array( 'treibstoff' =&gt; array( new Choice(array( 'choices' =&gt; array('benzin'), 'message' =&gt; 'form.quiz.falscheantwort', 'strict' =&gt; true, ) ), new NotBlank() ) )); $resolver-&gt;setDefaults(array( 'validation_constraint' =&gt; $collectionConstraint )); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload