Skip to content
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

[Validator] Choice constrait callback service call #26340

Closed
Strate opened this issue Feb 28, 2018 · 8 comments
Closed

[Validator] Choice constrait callback service call #26340

Strate opened this issue Feb 28, 2018 · 8 comments

Comments

@Strate
Copy link
Contributor

Strate commented Feb 28, 2018

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? no
Symfony version x.y.z

I would like to get choice list as result of call some service method. Looks like it is impossible for now.

@linaori
Copy link
Contributor

linaori commented Feb 28, 2018

Do you have an example of how you'd like to use this feature? I know that it's already possible, but is a bit of extra work.

@Strate
Copy link
Contributor Author

Strate commented Mar 1, 2018

For example if I have a list of choices in database, or on a different server, and I want to implement cache, etc

@rpkamp
Copy link
Contributor

rpkamp commented Mar 3, 2018

The way I do this is by registering the form type as a service, pass it the service that has the choices I need, call that service in the buildForm method, and pass the choices.

Something like this:

<?php

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface

class TaskType extends AbstractType
{
    private $service;

    public function __construct(MyService $service)
    {
        $this->service = $service;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $choices = $this->service->getSomeChoices();

        $builder->add('foo', ChoiceType::class, ['choices' => $choices]);
    }
}

@Strate
Copy link
Contributor Author

Strate commented Mar 9, 2018

I do not use Form component in my project, only Validation

@rpkamp
Copy link
Contributor

rpkamp commented Mar 9, 2018

Well in that case it's probably easiest to create a custom constraint with a custom validator. It's quite easy and gives you all the flexibility to cache and whatever you might need.

See https://symfony.com/doc/current/validation/custom_constraint.html

@Strate
Copy link
Contributor Author

Strate commented Mar 9, 2018

@rpkamp yes, right now my solution is close to that. Btw, this issue about improve DX in this case.

@xabbuh
Copy link
Member

xabbuh commented Sep 24, 2018

How do you see the feature being used? Would you somehow configure the constraint to let it know where to find the choices? Or do you see it as something that would be done only when the data is being validated by the validator?

@Simperfit
Copy link
Contributor

@xabbuh I think this can be closed. As we can do the same with custom validator/constraint.

@xabbuh xabbuh closed this as completed Jul 13, 2019
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

5 participants