1

I want to build form to perform multiples request. Here is an example of what i want to do enter image description here

In this case SQL request should be something like this :

SELECT * FROM DB WHERE city = city1 or city = city2 or pet = pet2 or pet = pet3 or food = food1

I tried to do it with symfony with no success because "symfony except string not array"

   class CoreSearchType extends AbstractType
    {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('city' , ChoiceType::class, [
            'choices' =>array('France' => $this->getChoicesCities()),
                'label' => false,'required' => true,'placeholder'=>'Choose','multiple' => true])

But i dont know what how to do it. If someone can help me finding the way to do it ... Please Notice that i use symfony 4

edit : I want to be able to choose two cities (or more), for exemple : city1 and city2 (all the city1 sample + the city2 sample, and not a variable type-hinted city1 and city2)

for exemple if : city1 = paris, tokyo, rio city2= amiens, madrid, london

the answer of the request will be : paris,tokyo,rio,amiens,madrid,london

Thanks !

5
  • 1
    What does $this->getChoicesCities() return exactly? Commented Nov 5, 2019 at 19:39
  • its private method who call the entiy where cities are stock Commented Nov 5, 2019 at 19:46
  • 1
    Can you share the code of this method? Commented Nov 5, 2019 at 19:56
  • with pleasure private function getChoicesCities() { $mizecore = Core::ARRON; $output = []; foreach (Core::ARRON as $key => $value) { $output[$value] = $key; } return $output; } Commented Nov 5, 2019 at 20:12
  • thanks you a lot for your support Commented Nov 5, 2019 at 20:12

1 Answer 1

1

From what I can see in your question you want a form which consists of three choice types. Here is an example on how this could look like.

class CoreSearchType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options) 
    {
        $builder
            ->add('city', ChoiceType::class, [
                'choices' => [
                    'city 1' => 'city1',
                    'city 2' => 'city2',
                    'city 3' => 'city3',
                ],
                'label' => false,
                'placeholder' => 'Choose',
                'multiple' => true,
            ])
            ->add('pet', ChoiceType::class, [
                'choices' => [
                    'pet 1' => 'pet1',
                    'pet 2' => 'pet2',
                    'pet 3' => 'pet3',
                ],
                'label' => false,
                'placeholder' => 'Choose',
                'multiple' => true,
            ])
            ->add('city', ChoiceType::class, [
                'choices' => [
                    'food 1' => 'food1',
                    'food 2' => 'food2',
                    'food 3' => 'food3',
                ],
                'label' => false,
                'placeholder' => 'Choose',
                'multiple' => true,
            ])
        ;
    }
}

Please check the part regarding the choices option in the symfony docs here: https://symfony.com/doc/current/reference/forms/types/choice.html#choices

The choices option is an array, where the array key is the item's label and the array value is the item's value

I think your error might come from this line:

'choices' => array('France' => $this->getChoicesCities())

It might work if you change it to

'choices' => array($this->getChoicesCities())
Sign up to request clarification or add additional context in comments.

4 Comments

Hello ! A huge thanks you for your help. Unfortunatly i still get the same error
Expected argument of type "string", "array" given at property path "city".
edit : I want to be able to choose two cities, for exemple : city1 and city2 (all the city1 sample + the city2 sample, and not a variable type-hinted city1 and city2)
Can you please share the code of your method getChoicesCities() or at least an example for its return value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.