Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make field required only if the form is new?

Tags:

php

symfony

I have the following buildForm method:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder                        
        ->add('firstname','text',array('label'=>'First Name'))
        ->add('lastname','text',array('label'=>'Last Name'))
        ->add('dob','date',array('widget'=>'single_text','label'=>'DOB'))
        ->add('username','text',array('label'=>'Username'))
        ->add('password','password',array('label'=>'Password'))
        ->add('filesPassword','password',array('label'=>'My Files Password','required'=>false))
        ->add('email','email',array('label'=>'Email'))
        ->add('language','entity',array('class'=>'GWD\AdminBundle\Entity\Languages','label'=>'Language'))
        ->add('theme','entity',array('class'=>'GWD\AdminBundle\Entity\Themes','label'=>'Theme'))
        ->add('roles','entity',array('class'=>'GWD\AdminBundle\Entity\Role','label'=>'Role'))
    ;
}

How can I dynamically set the password field to be required only upon creating a new record and set it non required when updating a record?

like image 722
MikeGA Avatar asked Oct 08 '14 01:10

MikeGA


People also ask

How do you make a field to be required to be filled?

In the Add Field/ Edit Field overlay, you can check the Make this field as required checkbox to make the field mandatory.


1 Answers

You could try code below:

$builder
  ->add('password','password',
        array(
               'label' => 'Password', 
               'required' => is_null($builder->getData()->getId())
             )
       )
like image 58
kapa89 Avatar answered Oct 19 '22 01:10

kapa89