Благодаря @ Cerad .Ответ на самом деле События формы
В типе формы (для меня App\Form\InvoicesType
) добавьте вызов метода в конец построителя:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$plus_thirty_days = new \DateTime('+28 days');
$builder
->add('client', EntityType::class, array(
'class' => Clients::class,
'choice_label' => 'name',
'disabled' => false,
) )
// the event that will handle the conditional field
->addEventListener(
FormEvents::PRE_SET_DATA,
array($this, 'onPreSetData')
);;
}
а затем в том же классе создайте открытый метод с именем, совпадающим со строкой в массиве (onPreSetData
для этого примера):
public function onPreSetData(FormEvent $event)
{
// get the form
$form = $event->getForm();
// get the data if 'reviewing' the information
/**
* @var Invoices
*/
$data = $event->getData();
// disable field if it has been populated with a client already
if ( $data->getClient() instanceof Clients )
$form->add('client', EntityType::class, array(
'class' => Clients::class,
'choice_label' => 'name',
'disabled' => true,
) );
}
Здесь вы можете обновить поле, чтобы оно было любым допустимым FormType и укажите все допустимые параметры, как для обычного элемента формы в From Builder, и он заменит предыдущий, оставив его в той же исходной позиции в форме.