8000 [Form] Document "class constraints" for data class-less forms · Issue #18969 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[Form] Document "class constraints" for data class-less forms #18969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PrOF-kk opened this issue Oct 2, 2023 · 5 comments
Closed

[Form] Document "class constraints" for data class-less forms #18969

PrOF-kk opened this issue Oct 2, 2023 · 5 comments
Labels
Form hasPR A Pull Request has already been submitted for this issue.

Comments

@PrOF-kk
Copy link
Contributor
PrOF-kk commented Oct 2, 2023

Symfony supports using a Form without using a Data Class.
In that page it mentions how to validate individual fields using a constraint, however it's also possible to add constraints to the root form:

  • In the controller ->
$form = $this
    ->createFormBuilder(null, [
        'constraints' => [
            new Expression([
                'expression' => 'value["firstName"] == "john"',
            ])
        ]
    ])
    ->add('firstName')
    ->add('lastName')
    ->getForm();

(src)

  • In the FormType using configureOptions ->
public function configureOptions(OptionsResolver $resolver)
{
    $collectionConstraint = new Collection(array(
        'CardNumber' => array(
            new Assert\CardScheme(array(
                'schemes' => array('VISA', 'MASTERCARD', 'DISCOVER'),
                'message' => 'The credit card number you entered is invalid.'))
        ),
        'SecurityCode' => array(
            new Length(array('min' => 3, 'max' => 3))
        )
    ));

    $resolver->setDefaults(array(
        'data_class' => null,
        'constraints' => $collectionConstraint
    ));
}

(src)

(Note: Callback constraints are also allowed and have access to the whole form)

@carsonbot carsonbot added the Form label Oct 2, 2023
@OskarStark
Copy link
Contributor

Interesting, I think this could be a great addition to the docs. Open for a PR against 5.4 branch?

cc @xabbuh

@xabbuh
Copy link
Member
xabbuh commented Oct 14, 2023

I think it would be a good idea to expand the examples around this a bit.

@PrOF-kk
Copy link
Contributor Author
PrOF-kk commented Oct 27, 2023

Here's the 2nd example brought into more modern PHP.

function configureOptions(OptionsResolver $resolver): void {
    $collectionConstraint = new Collection([
            'CardNumber' => [
                    new CardScheme([
                            'schemes' => ['VISA', 'MASTERCARD', 'DISCOVER'],
                            'message' => 'The credit card number you entered is invalid.'
                    ])
            ],
            'SecurityCode' => [
                    new Length(exactly: 3)
            ]
    ]);

    $resolver->setDefaults([
            'data_class' => null,
            'constraints' => $collectionConstraint
    ]);
}

Should I create a complete example formtype or two for this?

@OskarStark
Copy link
Contributor

Friendly ping @alexandre-daubois if you want to help 🤓

@alexandre-daubois
Copy link
Member

I gave it a try!

@xabbuh xabbuh added the hasPR A Pull Request has already been submitted for this issue. label Oct 28, 2023
javiereguiluz added a commit that referenced this issue Jan 9, 2025
…int to validate array-like data (xabbuh)

This PR was merged into the 6.4 branch.

Discussion
----------

[Form] wrap multiple constraints in a Collection constraint to validate array-like data

fixes symfony/symfony#59332, replaces #20487

see also #18969 (comment)

Commits
-------

6e71ed0 wrap multiple constraints in a Collection constraint to validate array-like data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Form hasPR A Pull Request has already been submitted for this issue.
Projects
None yet
Development

No branches or pull requests

5 participants
0