B
B
BonBon Slick2018-07-30 12:48:17
symfony
BonBon Slick, 2018-07-30 12:48:17

If you use a form component, where is it better to specify constraints?

There is a variant of the task of constraints, validation rules in the entity itself, or when creating a form.

/**
     * @var string
     *
     * @ORM\Column(type="string", length=255)
     *
     * @Assert\NotBlank()
     * @Assert\Type(
     *     type="string",
     * )
     */
    private $firstName;

или

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', TextType::class, [
                'required' => true,
                constraints' => array(new NotBlank()....))
            ])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2018-07-30
@BonBonSlick

I chose the first option using annotations. If you choose the second one, the buildForm function can turn into an analogue of "code noodles". The second minus of the second approach is arrays within arrays.
In general, as I understand it, it's a matter of taste.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question