0
votes

I don't understand why the rendering of this subform doesn't render the required tag on my TextType firstName;

My form in based on a Order entity
OrderFormType has a CollectionType of Tent, based on TentFormType
TentFormType has a CollectionType of Camper, based on CamperFormType

So Order > Tent > Camper

namespace AppBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
//...

class CamperFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('firstName', TextType::class, [
            'required' => true, //Should even not been usefull since SF2.8
            'label'    => 'First name',
            'attr'     => [
                'placeholder' => 'First name'
            ],
        ]);

        //...
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class'      => 'AppBundle\Entity\Camper',
            'csrf_protection' => true,
            'error_bubbling'  => true,
            'csrf_field_name' => '_token',
            //...
        ]);
    }
}

The fields are simply rendered with a form_widget:

{{ form_widget(form.firstName) }}
{{ form_widget(form.lastName) }}

But that not add the required field:

<input id="app_order_form_type_tents_0_campers_0_firstName" name="app_order_form_type[tents][0][campers][0][firstName]" placeholder="First name" class="form-control" type="text">
<input id="app_order_form_type_tents_0_campers_0_lastName" name="app_order_form_type[tents][0][campers][0][lastName]" placeholder="Last name" class="form-control" type="text">

I could do

{{ form_widget(form.firstName, {'attr': {'required': 'required'}}) }}
{{ form_widget(form.lastName, {'attr': {'required': 'required'}}) }}

But it shouldn't be required with my FormType...

Does anyone knows why ?

--EDIT--

My Camper Entity

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Camper.
 *
 * @ORM\Table(name="camper")
 * @ORM\Entity()
 */
class Camper
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     * @Assert\NotBlank()
     *
     * @ORM\Column(name="firstName", type="string", length=255, nullable=false)
     */
    private $firstName;

    // ...

}
1
Can you just paste your entity here? - Maulik Savaliya
Just did it :) ! - Bouffe

1 Answers

0
votes

I'm sorry, i can not use comments so i put a suggestion here... Try:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}