V
V
Vasily2014-07-18 12:51:28
symfony
Vasily, 2014-07-18 12:51:28

Why are some form fields in Symfony2 set to required="required" while others are not?

Recently I began to get acquainted with Symfony2, before that I sat on Kohana for a long time, started making forms, through FormType, based on the finished Entity, a form is created through FormBuilder and here's the mystery - some fields have the required="required" property, while others don't for some reason. In general, there is no particular difficulty, I understand that everything can be configured through the validator, but this magic haunts. Below is the code, I think it will tell more.
Here is the essence in brief and the fields that I would like to have required="required"

namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

class User extends BaseUser
{
   .....
    /**
     *
     * @var string
     * 
     * @ORM\Column(name="first_name", type="string", nullable=true)
     */
    protected $firstName;

    /**
     *
     * @var string
     * 
     * @ORM\Column(name="last_name", type="string", nullable=true)
     */
    protected $lastName;
    
    /**
     *
     * @var string
     * 
     * @ORM\Column(name="phone", type="string", nullable=true)
     */
    protected $phone;
   .........

Here is the form builder
public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('phone')
                ->add('first_name')
                ->add('last_name')
                
                ->add('save', 'submit');
    }

Form creation
$user = $this->get('security.context')->getToken()->getUser();

        $user_profile_form = $this->createForm(new UserProfileType(), $user)
                ->createView();

Insert into the template
{{ form(user_profile_form) }}
and at the exit we have
<div id="user_profile">
<div>
<label for="user_profile_phone">Phone</label>
<input id="user_profile_phone" type="text" name="user_profile[phone]">
</div>
<div>
<label class="required" for="user_profile_first_name">First name</label>
<input id="user_profile_first_name" type="text" value="Иван" required="required" name="user_profile[first_name]">
</div>
<div>
<label class="required" for="user_profile_last_name">Last name</label>
<input id="user_profile_last_name" type="text" required="required" name="user_profile[last_name]">
</div>
<div>
<input id="user_profile__token" type="hidden" value="iR_ZubBD0JJrTlioAktspRZQyRKS_57AqHAr58RBx0Q" name="user_profile[_token]">
</div>

First name and last name come with required="required" and for some reason phone comes without it, why is this happening? I read the docks, searched for these fields according to the project, looked at the validators, did not find anything similar.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel Solovyov, 2014-07-18
@dedik

just manually specify in the builder which fields should be required

$builder
    ->add('phone', 'text', ['required' => true]) // и т.д.
    ->add('first_name')
    ->add('last_name')
    ->add('save', 'submit');

L
LiVsI, 2014-07-18
@LiVsI

nullable=true in Entity means the field is not required. That is, in the form too! If you always need phone, then specify nullable=false or don't specify at all.
Regarding the fact that required="required" is written for the First name and last name fields - look in your project - it is not written on the test one.
If you need to require the mandatory presence of a field value at the form level, use options: ->add('phone', 'text', array('required'=>true))

F
faost, 2014-07-23
@faost

By default, required = true, but if you do not specify a field type, the value will be determined based on validation. That is, you need to see your validation. You could also override the form template where this attribute is actually drawn.

Q
Quber, 2014-08-09
@Quber

Nullable = true means that the field can be empty. In the builder, you still need to specify the 'required' => false parameter so that validation for the field is optional.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question