I have a user entity, with a field isSubscribed.
I want to show the status of this (boolean) as a checked or unchecked checkbox in a form.
Normally I would get this value in my formtype from a query but that seems overkill.
I want to do something like this:
$builder
->add('isSubscribed', CheckboxType::class, [
'label' => 'Subscribe to weekly update',
'attr' => array('checked' => function(User $user){
return $user->getIsSubscribed();
}),
'required' => false,
])
;
But I get an error:
Closure could not be converted to string
Here's my formtype:
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AccountFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('isSubscribed', CheckboxType::class, [
'label' => 'Subscribe to weekly update',
'required' => false,
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
In my User entity:
/**
* @ORM\Column(type="boolean")
*/
private $isSubscribed;