Created
November 25, 2016 10:50
-
-
Save pzaj2/b4d7fafe82236d0c0459974e512e2c0e to your computer and use it in GitHub Desktop.
updated gist for readonly form field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace CustomBundle\Form; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; | |
class ExampleFormType extends AbstractType | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(FormBuilderInterface $builder, array $options) | |
{ | |
$builder | |
->add('unmappedField', CheckboxType::class, [ | |
'attr' => [ | |
'disabled' => 'disabled', | |
'readonly' => 'readonly', | |
], | |
]) | |
; | |
// other fields | |
$builder->get('unmappedField') | |
->addEventListener(FormEvents::SUBMIT, [$this, 'setUnmappedField']); | |
} | |
public function setUnmappedField(FormEvent $event) | |
{ | |
$formField = $event->getForm(); | |
$entity = $formField->getParent()->getData(); | |
if($entity) { | |
$event->setData($entity->getUnmappedField()); | |
} | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function configureOptions(OptionsResolver $resolver) | |
{ | |
$resolver->setDefaults(array( | |
'data_class' => 'CustomBundle\Entity\ExampleEntity', | |
)); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getBlockPrefix() | |
{ | |
return 'custombundle_example'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment