Reputation: 44
I was trying to build a form contains a button, which should have an attribute with baseId (eg. <button baseId="1">history</button>
).
The problem is that baseId value should be get from entity for each row. I was trying as below:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('bedsoreBaseId', ButtonType::class, ['attr' => [
'baseId' => function($entity) {
return $entity->getBaseId();
}
], 'label' => 'history']);
}
However, this results in an error:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Closure could not be converted to string").
What is the best way to access this property and use it as attribute value?
Upvotes: 0
Views: 416
Reputation: 5679
You can set it directly in template
{{ form_row(form.bedsoreBaseId, {'attr':{'baseId':form.vars.data.baseId }}) }}
Or add this element to form on PRE_SET_DATA
event
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
$form->add('bedsoreBaseId', ButtonType::class, [
'attr' => ['baseId' => $data->getBaseId()],
'label' => 'history'
]);
});
}
Upvotes: 1