I'm integrating the reCaptcha to my website with symfony forms. I've been created a custom form type in which I include a HiddenType input.
class ReCaptchaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('token', HiddenType::class, [
'constraints' => [
new NotBlank([
'message' => 'Please, verify the captcha.',
]),
new ReCaptchaConstraint([
'action' => $options['action'],
'projectDir' => $this->projectDir
])
],
]);
}
}
I use javascript to get the token and then set it to the HiddenType input, in order I can get the token value and validate it. So, as you can see I use two constraints, the NoBlank and a custom one ReCaptchaConstraint.
The HiddenType input has a blank value by default. So if the captcha is not verified the value remains blank, and the constraint triggers the invalidation. This behavior works as expected, and the form is not sent. And in the symfony debug tool bar, I got the respective message.
But I want to show this message to the user as a form error, I Have the following template
{% block re_captcha_errors %}
{% for child in form.children %}
{{ form_errors(child) }}
{% endfor %}
{% endblock %}
With this, it is supposed to print the error of the children, currently the HiddenType. and print them as the ReCaptchaType errors. but it don't print anything. Actually if I hardcode something in the template, it is printed, but the children errors are not.
I don't know if I'm doing it in the right way. Is this supposed to work? How can I show the error messages in my template?
this is my form template, where is use the ReCaptchaType
...
<tr>
<td class="f-err" colspan="2">
{{ form_errors(registrationForm.recaptcha) }}
</td>
</tr>
<tr>
<td class="f-wig" colspan="2">
{{ form_widget(registrationForm.recaptcha) }}
</td>
</tr>
....