8000 [Form][Validator] Validation groups need improvement · Issue #2496 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

[Form][Validator] Validation groups need improvement #2496

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
canni opened this issue Oct 27, 2011 · 1 comment
Closed

[Form][Validator] Validation groups need improvement #2496

canni opened this issue Oct 27, 2011 · 1 comment

Comments

@canni
Copy link
Contributor
canni commented Oct 27, 2011

Consider following example:

I have entity Client witch have field type, that field can hold 2 values (const TYPE_PERSON or TYPE_COMPANY), also I have 2 validation groups person and company, because ie. different fields are required based on client type.

Now standard way of creating new client looks like this:

public action clientCreateAction()
{
    $client = new Entity\Client();
    $form = $this->createForm(new ClientType(), $client);

    $form->bindRequest($this->getRequest());

    if($form->isValid())
        // persis object
}

In this scenario, because validation is called by the $form->bindRequest() I'm not able to set correct validation groups without using $data = $this->getRequest()->request->get($form->getName()); $type = $data['type']; and injecting this value into ClientType

That process of determining validation groups based on any value should natural and easy with form/validation framework.

My suggest solution is to enable developer to use closure as value of $options['validation_groups'] eg:

class ClientType extends AbstarctType
{
// ...
    public function getDefaultOptions(array $options)
    {
        return array(
            'validation_groups' => function($dataFromRequest, $objectTainedToType){
                // return array of validation groups that should be used to validate given data / object pair
            },
        );
    }
// ...
}
@schmittjoh
Copy link
Contributor

I agree, I've reported this in #1151.

Closing this, and referencing your ticket there.

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

No branches or pull requests

2 participants
0