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

[UX] Live component - form select required not initialized #1332

Closed
BriceFab opened this issue Dec 13, 2023 · 10 comments · Fixed by #2403
Closed

[UX] Live component - form select required not initialized #1332

BriceFab opened this issue Dec 13, 2023 · 10 comments · Fixed by #2403
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed

Comments

@BriceFab
Copy link

Symfony version(s) affected

6.3.10

Description

Class: Symfony\UX\LiveComponent\ComponentWithFormTrait::extractFormValues

Doesn't extract required values for select / entity type field:

image
image

The default value should be '1'.

This is the FormType:

            ->add('project', EntityType::class, [
                'class' => Project::class,
                'label' => 'label.project',
                'required' => true,
                'multiple' => false,
                'choice_label' => fn ($project) => $project->getName(),
            ])

How to reproduce

Create a form type with ux live component and required entity type field.
https://symfony.com/bundles/ux-live-component/current/index.html#forms

Possible Solution

If select / entity type and required, extract the default value

Additional Context

No response

@BriceFab BriceFab added the Bug Bug Fix label Dec 13, 2023
@carsonbot carsonbot added the Status: Needs Review Needs to be reviewed label Dec 13, 2023
@xabbuh xabbuh transferred this issue from symfony/symfony Dec 13, 2023
@carsonbot
Copy link

Hey, thanks for your report!
There has not been a lot of activity here for a while. Is this bug still relevant? Have you managed to find a workaround?

@carsonbot
Copy link

Hello? This issue is about to be closed if nobody replies.

@carsonbot
Copy link

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

@dsoriano
Copy link
Contributor

Hello, I have exactly the same problem !

@carsonbot carsonbot removed the Stalled label Sep 18, 2024
@GarbiSebastian
Copy link

Same problem, cannot submit live form with default values for select and entity types

@smnandre
Copy link
Member

How would you handle this ?

@dsoriano
Copy link
Contributor

@smnandre the solution proposed by @BriceFab seems to be a goode idea, The values should be extracted and defined in ComponentWithFormTrait::extractFormValues.

Actually to make my form works, I had to do a dirty workaround in the form:

    public function finishView(FormView $view, FormInterface $form, array $options): void
    {
        $this->initChoiceValue($view, 'notation');
    }

    private function initChoiceValue(FormView $view, string $key): void
    {
        if ($view[$key]->vars['value'] === "" && array_key_exists('choices', $view[$key]->vars) && count($view[$key]->vars['choices']) > 0) {
            $view[$key]->vars['value'] = key($view[$key]->vars['choices']);
        }
    }

@smnandre
Copy link
Member

Would one of you people be ready to open a PR ?

(We can iterate on "details" things, but this could be a good base :))

@dsoriano
Copy link
Contributor

I can do it but not right away, I'm overwhelmed in the short term and I already have another PR to create and submit to you following another bug found. If someone else can do it that's great, otherwise I'll take care of it as soon as possible

@smnandre
Copy link
Member

Something like this ?

// ...
        if ($child->vars['required'] && $values[$name] === null) {
        
            // Select the first element if available (can be an empty placeholder) 
            if (0 < count($choices = $child->vars['choices'] ?? [])) {
                $values[$name] = reset($choices);
                continue;
            } 
              
            throw new UnprocessableEntityHttpException(sprintf('The required field "%s" is missing and has no default value.', $name));
        }
        

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug Fix Status: Needs Review Needs to be reviewed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants