MikeGA
MikeGA

Reputation: 1262

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

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?

Upvotes: 8

Views: 3650

Answers (3)

kapa89
kapa89

Reputation: 615

You could try code below:

$builder
  ->add('password','password',
        array(
               'label' => 'Password', 
               'required' => is_null($builder->getData()->getId())
             )
       )

Upvotes: 12

MikeGA
MikeGA

Reputation: 1262

The following code works for me with only one caveat, the password field gets added at the end of the form and not in the order I wanted it even though I thought that by adding the event where I wanted the field to appear in, it would do the trick but it didn't.

<?php

namespace GWD\AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;

class AdministratorsType extends AbstractType
{

    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'));

        $builder->addEventListener(FormEvents::PRE_SET_DATA,
            function(FormEvent $event) use ($builder) {
                $administrator = $event->getData();
                if (!$administrator || null === $administrator->getId()) {
                    $event->getForm()->add('password','password',array('label'=>'Password','required'=>true));
                } else {
                    $event->getForm()->add('password','password',array('label'=>'Password','required'=>false));

                }
            }
        );

        $builder
            ->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'))
        ;

    }


    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'GWD\AdminBundle\Entity\Administrators'
        ));
    }


    public function getName()
    {
        return 'gwd_adminbundle_administrators';
    }
}

Upvotes: 1

ShoeLace1291
ShoeLace1291

Reputation: 4698

You could add another var to your array where you define labels:

->add('firstname','text',array('label'=>'First Name', 'required' => false))

Upvotes: -2

Related Questions