I use Symfony 6 with EasyAdmin, I've been trying for several hours to understand how form types work in Symfony, and more specifically how to set up a custom form field, but I can't find a simple, comprehensive example. Perhaps one of you could help me?
My need is simple:
I have a "signatureClient" field for which I want to generate a <canvas id="signatureClient" columns="300" height="100"></canvas> tag,
for the purpose of creating a digital signature.
I created a SignatureField class.
<?php
namespace App\EasyAdmin;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
use App\Form\CanvasType;
class SignatureField implements FieldInterface
{
use FieldTrait;
public static function new(string $propertyName, ?string $label = null)
{
return (new self())
->setProperty($propertyName)
->setLabel($label)
->setFormType(CanvasType::class)
;
}
}
In my controller,
SignatureField::new('signatureClient')
Then a CanvasType class, for which i've chosen the textarea parent, and the prefix "canvas":
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
class CanvasType extends AbstractType
{
public function getParent(): ?string
{
return TextareaType::class;
}
public function getBlockPrefix(): string
{
return 'canvas';
}
}
My custom_types.html.twig
{# templates/admin/form/custom_types.html.twig #}
{% form_theme form 'form.html.twig' %}
{% block _InterventionTechnique_signatureClient_widget %}
<canvas id="{{ field.name}}" width="300" height="100">{{ field.value}}</canvas>
{% endblock %}
The result in my form:
<textarea id="InterventionTechnique_signatureClient" name="InterventionTechnique[signatureClient]" class="form-control"></textarea>
The link with the canvas template is not working...
Could one of you help me?
Thank you very much.