8000 [Form] Expression validation constraint on form without a Data Class · Issue #22403 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Expression validation constraint on form without a Data Class #22403

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
Seb33300 opened this issue Apr 12, 2017 · 4 comments
Closed

[Form] Expression validation constraint on form without a Data Class #22403

Seb33300 opened this issue Apr 12, 2017 · 4 comments

Comments

@Seb33300
Copy link
Contributor

Hello,

When using form without a data class (see: http://symfony.com/doc/current/form/without_class.html), we are not able to use expression to access other fields data.

Only the value var seems to be filled.

If I try something like this.firstName, I am not able to access others fields values.

Is it normal?

@yceruto
Copy link
Member
yceruto commented Apr 12, 2017

Yes, it's the normal behavior, without data_class there is no object instance in context (hence it's null).

However, in this case, you can to add the Expression constraint to the root form and value will contain an array of your fields values.

For example:

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

@Seb33300
Copy 8000 link
Contributor Author
Seb33300 commented Apr 12, 2017

This is what I am doing but I hoped there was another solution.

Maybe something like a data var which give an access to unmapped data (when data_class is not specified or for fields with mapped options set to false).

@yceruto
Copy link
Member
yceruto commented Apr 12, 2017

Maybe we talking about to pass a new variable to the expression context like the current root value from validator context, thus we have complete access to the form instance (in this case), and so, this would be possible (with or without data_class, accessing to unmapped fields too):

$form = $this
    ->createFormBuilder()
    ->add('firstName', null, [
        'constraints' => [
            new Expression([
                'expression' => 'root["firstName"].getData() != root["lastName"].getData()',
            ])
        ]
    ])
    ->add('lastName')
    ->getForm();

@xabbuh
Copy link
Member
xabbuh commented Nov 7, 2018

It doesn't seem that there is much concern about adding something like the root form to the context of the ExpressionLanguage being used when validating a form. Thus, I am going to close here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0