Answer the question
In order to leave comments, you need to log in
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;
.........
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('phone')
->add('first_name')
->add('last_name')
->add('save', 'submit');
}
$user = $this->get('security.context')->getToken()->getUser();
$user_profile_form = $this->createForm(new UserProfileType(), $user)
->createView();
{{ form(user_profile_form) }}
<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>
Answer the question
In order to leave comments, you need to log in
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');
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))
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.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question