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
Comments
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. |
For example if I have a list of choices in database, or on a different server, and I want to implement cache, etc |
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 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]);
}
} |
I do not use Form component in my project, only Validation |
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 |
@rpkamp yes, right now my solution is close to that. Btw, this issue about improve DX in this case. |
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? |
@xabbuh I think this can be closed. As we can do the same with custom validator/constraint. |
I would like to get choice list as result of call some service method. Looks like it is impossible for now.
The text was updated successfully, but these errors were encountered: