प्रपत्र विकल्प का उपयोग करना, जैसे आप सुझाव देते हैं, आप कुछ के साथ अपने प्रपत्र प्रकार परिभाषित कर सकते जैसे:
class FormType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('first_name',TextType::class,array('disabled'=>$option['first_name_disabled']));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array('first_name_disabled'=>false));
}
}
और फिर अपने नियंत्रक के साथ प्रपत्र बनाने के लिए:
$form=$this->createForm(MyType::class, $yourEntity, array('first_name_disabled'=>$disableFirstNameField));
लेकिन यदि अक्षमता का मान इकाई में मान पर निर्भर करता है, तो आपको एक फॉर्मवेन्ट का उपयोग करना चाहिए:
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('first_name',TextType::class);
// Here an example with PreSetData event which disables the field if the value is not null :
$builder->addEventListener(FormEvents::PRE_SET_DATA,function(FormEvent $event){
$lastName = $event->getData()->getLastName();
$event->getForm()->add('first_name',TextType::class,array('disabled'=>($lastName !== null)));
});
}