PHP Snippet 1:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
$val = $data['some_field'];
$options = $form->get('existing_field_name_to_replace')->getConfig()->getOptions();
$options['attr']['your-attr'] = $val;
$form->add('existing_field_name_to_replace', 'type', $options);
};
PHP Snippet 2:
<div {% if form.DATA_FIELD.vars.value == 'foo' %}disabled{% endif %}>
PHP Snippet 3:
public function doSomethingAction(Request $request) {
// ......
$edit = $this->areWeEditing(); // or whatever data youre trying to inject
// options to send to the form
$options = [
'disable_field' => $edit, // <--- put your data in here.
];
$entity = new Something();
$form = $this->createForm( SomethingType::class, $entity, $options );
$form->handleRequest( $request );
if ( $form->isValid() ) {
//... do something
}
// .....
}
PHP Snippet 4:
class SomethingType extends AbstractType {
public function buildForm( FormBuilderInterface $builder, array $options ) {
$builder->add( 'name', TextType::class, [
'label' => 'Name',
'required' => true,
'disabled' => $options[ 'disable_field' ], // <---- use the $option here.
] )->add( 'email', EmailType::class, [
'required' => true,
] );
}
public function configureOptions( OptionsResolver $resolver ) {
$resolver->setDefaults( [
'data_class' => Something::class, // whatever entity class youre mapping to
'disabled_field' => false, // you need to set this to tell symfony its an allowed option. Its name needs to match the incoming array key. If the array key isnt present, this default will be used instead.
] );
}
}