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

Preselect values in form #140

Open
markus-fischbacher opened this issue Apr 25, 2019 · 0 comments
Open

Preselect values in form #140

markus-fischbacher opened this issue Apr 25, 2019 · 0 comments

Comments

@markus-fischbacher
Copy link

Hi there,
I'm using select2entity with a AbstractType inherited from ChoiceType.

class ApplicationStatusType extends AbstractType
{
    const APPLIED = 'applied';
    const CHECKED = 'checked';
    const SELECTED = 'selected';
    const NOT_SELECTED = 'not selected';
    const ATTENDED = 'attended';
    const NOT_ATTENDED = 'not attended';

    public static $values = [
        self::APPLIED => self::APPLIED,
        self::CHECKED => self::CHECKED,
        self::SELECTED => self::SELECTED,
        self::NOT_SELECTED => self::NOT_SELECTED,
        self::ATTENDED => self::ATTENDED,
        self::NOT_ATTENDED => self::NOT_ATTENDED,
    ];

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults(
            [
                'choices' => self::$values,
            ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getParent(): ?string
    {
        return ChoiceType::class;
    }
}

The control is configured in my FormType as following:

class ReportParameterSelectorType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $statusConfig = [
            'multiple' => true,
            'class_type' => static::class,
            'class' => ApplicationStatusType::class,
            'remote_route' => 'ajax_autocomplete_status',
            'label' => 'applicationStatus',
            'placeholder' => 'selectStatusPlaceholder',
        ];

        $builder
            ->add('status', Select2EntityType::class, $statusConfig);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            [
                'data_class' => null,
            ]
        );
    }
}

The Ajax call is implemented that way:

    /**
     * @param string $typename
     * @param string $query
     * @param TranslatorInterface $translator
     * @return Response
     */
    private function getAbstractTypeValues(string $typename, ?string $query, TranslatorInterface $translator): Response
    {
        $typeInstance = new $typename();

        $options = [];
        foreach ($typeInstance::$values as $key => $value) {
            $options[] = ['id' => $value, 'text' => $translator->trans($key)];
        }
        sort($options);

        $status['results'] = array_values(array_filter($options, function ($element) use ($query) {
            return !strlen($query) || stristr($element['text'], $query) != false;
        }));

        return new JsonResponse($status);
    }


    /**
     * @Route("/autocomplete_status", name="ajax_autocomplete_status")
     * @param Request $request
     * @param TranslatorInterface $translator
     * @return Response
     */
    public function autocompleteStatusAction(Request $request, TranslatorInterface $translator): Response
    {
        $query = $request->get('q');
        return $this->getAbstractTypeValues(ApplicationStatusType::class, $query, $translator);
    }

The form is opened that way:

        $data = ['status' => null];
        $form = $this->createForm(ReportParameterSelectorType::class, $data);

Is there a way from controller point of view, to preselect values?

I already tried:

$data = ['status' => ['selected', 'applied']];
Results in: Warning: spl_object_hash() expects parameter 1 to be object, string given

Any ideas?

Regards,
Markus

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

1 participant