I'm creating a form with Symfony, its works perfectly but I have an issue with my validation, I made a field required and not blank but the error message isn't displayed when I submit the form with the input empty I don't know why
there is some part of code :
FormType :
use Symfony\Component\Validator\Constraints\NotBlank;
class AnnonceurType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
<some fields>
->add('phone', TextType::class, [
'required' => true,
'constraints' =>[
new NotBlank()
]
])
}
}
template of form :
{{ form_start(form) }}
<some fields>
<div class="form-group">
{{ form_label(form.phone, 'Phone number') }} *
{{ form_widget(form.phone, { 'attr': {'class': 'form-control'}}) }}
</div>
<div class="float-right">
<button type="submit" class="btn btn-primary">
{{ 'submit'|trans() }}
</button>
</div>
<div class="form-error">
{{ form_errors(form) }}
</div>
{{ form_end(form) }}
Someone know what's wrong with my code ? thanks you
update: solved, it was because of the required, I added a novalidate and now its ok
My Answer:
Glad to hear that you were able to solve the issue! Adding the `novalidate` attribute can sometimes bypass the browser's built-in validation and allow Symfony's validation to take over. If you have any more questions or need further assistance, feel free to ask.
Rate this post
4 of 5 based on 3818 votesComments