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?
In the Add Field/ Edit Field overlay, you can check the Make this field as required checkbox to make the field mandatory.
You could try code below:
$builder
->add('password','password',
array(
'label' => 'Password',
'required' => is_null($builder->getData()->getId())
)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With