Reputation: 315
I can't get Symfony constraints to work.
I have this simple form inside an action:
/**
* @Route("reservation/{hash}/invoice", name="reservation-invoice")
* @param Request $request
* @param TranslatorInterface $translator
* @param Reservation $reservation
* @return JsonResponse|Response
*/
public function invoiceAction(Request $request, TranslatorInterface $translator, Reservation $reservation)
{
if (empty($reservation->getInvoice())) {
$invoice = new Invoice();
$invoice->setReservation($reservation);
} else {
$invoice = $reservation->getInvoice();
}
$form = $this->createFormBuilder($invoice)
->add(
'name',
TextType::class,
array(
'label' => $translator->trans('invoice.name'),
'required' => true,
'attr' => array(
'placeholder' => $translator->trans('invoice.name_placeholder'),
'class' => 'form-control',
),
'constraints' => array(
new Length(array('min' => 3, 'max' => 50))
)
)
)
->add(
'company',
TextType::class,
array(
'label' => $translator->trans('invoice.company'),
'required' => true,
'attr' => array(
'placeholder' => $translator->trans('invoice.company_placeholder'),
'class' => 'form-control',
),
'constraints' => array(
new Length(array('min' => 3, 'max' => 50))
)
)
)
->add(
'address',
TextType::class,
array(
'label' => $translator->trans('invoice.address1'),
'required' => true,
'attr' => array(
'placeholder' => $translator->trans('invoice.address1_placeholder'),
'class' => 'form-control',
),
'constraints' => array(
new Length(array('min' => 3, 'max' => 100))
)
)
)
->add(
'address2',
TextType::class,
array(
'label' => $translator->trans('invoice.address2'),
'required' => true,
'attr' => array(
'placeholder' => $translator->trans('invoice.address2_placeholder'),
'class' => 'form-control',
),
'constraints' => array(
new Length(array('min' => 3, 'max' => 100))
)
)
)
->add(
'nip',
TextType::class,
array(
'label' => $translator->trans('invoice.nip'),
'required' => true,
'attr' => array(
'placeholder' => $translator->trans('invoice.nip_placeholder'),
'class' => 'form-control',
),
'constraints' => array(
new Length(array('min' => 3, 'max' => 10))
)
)
)
->setAction($this->generateUrl('reservation-invoice', array('hash' => $reservation->getHash())))
->setMethod('POST')
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$invoice = $form->getData();
$this->db()->persist($invoice);
$this->db()->flush();
$this->addFlash('success', $translator->trans('invoice.data_changed'));
return new JsonResponse(array(
'redirect' => $this->generateUrl('reservation', array('hash' => $reservation->getHash()),
UrlGeneratorInterface::ABSOLUTE_URL)
));
}
return $this->response(
array(
'form' => $form->createView()
)
);
}
In config I have:
framework:
validation:
enabled: true
And form display is really simple:
{{ form_start(form) }}
<div class="form-group">
{{ form_label(form.name) }}
{{ form_widget(form.name) }}
{{ form_errors(form.name) }}
</div>
<div class="form-group">
{{ form_label(form.company) }}
{{ form_widget(form.company) }}
{{ form_errors(form.company) }}
</div>
<div class="form-group">
{{ form_label(form.address) }}
{{ form_widget(form.address) }}
{{ form_errors(form.address) }}
</div>
<div class="form-group">
{{ form_label(form.address2) }}
{{ form_widget(form.address2) }}
{{ form_errors(form.address2) }}
</div>
<div class="form-group">
{{ form_label(form.nip) }}
{{ form_widget(form.nip) }}
{{ form_errors(form.nip) }}
</div>
{{ form_end(form) }}
When I submit the form with empty inputs, the $form->isValid()
returns true and I get database error as these fields cannot be empty.
I suspect there is something I missed, but according to Symfony docs that's all you have to do.
EDIT:
I managed to do assertions in Entity class annotations, like:
/**
* @var string
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
* @Assert\Length(min=3, max=50)
*/
private $name;
What seems strange to me is that leaving controller intact, 'required' => true
doesn't matter at all. When I post empty field, I get 1 error, but when I post 1 letter I get 2 errors: one from contraints array and one from annotation.
Upvotes: 0
Views: 170
Reputation: 315
To ensure that field is not null I finally used Assert\NotBlank:
/**
* @var string
* @ORM\Column(type="string", length=50)
* @Assert\NotBlank()
* @Assert\Length(min=3, max=50)
*/
private $name;
Upvotes: 1