Skip to content

Latest commit

 

History

History
45 lines (38 loc) · 1.73 KB

File metadata and controls

45 lines (38 loc) · 1.73 KB

3.19 Repeated fields

A RepeatedType field holds one value that Symfony spreads over two inputs, so the constraints of the property it is bound to describe neither of them on their own. The bundle moves them down to the first of the two children, which is where Symfony reports the violations of a repeated field as well: its validator extension defaults the error_mapping option of the type to ['.' => $options['first_name']].

class User
{
    #[Assert\Length(min: 6)]
    private string $password;
}
$builder->add('password', RepeatedType::class, [
    'type' => PasswordType::class,
    'first_options' => ['label' => 'Password'],
    'second_options' => ['label' => 'Repeat password'],
]);

Nothing has to be configured. Length is validated against the first input, and a violation is rendered next to it, once. Constraints given in first_options are kept and validated together with the moved ones.

The two children are still required to be equal: that check belongs to the ValueToDuplicatesTransformer of the repeated field, which reports the invalid_message of the field - The values do not match. by default - on the same first input.

A few details are worth knowing:

  • The constraints are moved, not copied. Validating a single input of the pair, as validation on a custom event does, validates the first one and leaves the second one to the equality check.
  • A custom first_name is honoured; the constraints follow the child that Symfony maps the errors to.
  • If the first child is not part of the model - it is a HiddenType, or its validation is disabled through first_options - the constraints stay on the repeated field itself and are validated against the value both inputs resolve to.