-
-
Notifications
You must be signed in to change notification settings - Fork 364
[Live] Re-extracting form data after submit #254
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
Conversation
So if I'll have hundred live actions I'll need to write same number submit/reset statements? :) |
Yup! The same as if you has a hundred controller actions 😉 BUT, I'm re-thinking this approach - I think it's flawed, as trying to "submit the form" then "change the data the form was created from" then... re-recreate the form with the new mix of data... just doesn't work, as the final "new" form wouldn't be "submitted", so it wouldn't have correct validation errors. If we re-submit the 2nd form, then we overwrite the new values we just changed in our underlying object. I think it's a dead approach. I think the easier flow is to, in your actions, manipulate |
Yeah, at least I currently get them 😉 |
From a quick glimpse I can tell that this does not collide nor solve the problem addressed in my PR, which focuses on handling different kind of checkbox field types within forms 👍 |
7658fa3
to
b4d20b6
Compare
$form->submit($this->formValues); | ||
// re-extract the "view" values in case the submitted data | ||
// changed the underlying data or structure of the form | ||
$this->formValues = $this->extractFormValues($this->getForm()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the key. It's super simple, it just took me a long time to understand what's going on. The flow is this:
A) A custom action is called
B) In the custom action, the user changes $this->formValues
. For example, to add a new array to initialize a new collection type.
C) We re-extract the formValues
from the form.
Normally, if the user sends formValues
and we then use those to submit the form, the formValues
that should be sent back to the frontend would be the same: that is STILL the "formValues" data. However, if your form changes as a result of submitted data (e.g. CollectionType
which adds more fields if you add a new item to a collection type array), then we need to re-extract the formValues
so that they match the new reality.
} | ||
|
||
return $this->formValues; | ||
$this->formValues = $this->extractFormValues($this->getForm()); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything above this was just cleaning up the flow, but not significant
Other than a small TODO in the documentation, this is ready to go. This is much simpler now & mirrors what @norkunas was doing in his project, except now with the fix to
So now the answer to that is "no": your actions modify |
0c6c713
to
da9fc63
Compare
src/LiveComponent/tests/Functional/Form/ComponentWithFormTest.php
Outdated
Show resolved
Hide resolved
* If you override, call $this->initializeFormValues() manually | ||
* and also $this->formView = $form if you are passing in a Form. | ||
* | ||
* TODO: Refactor to PostMount when available. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PostMount
is now available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeeeea it is 😎
This is now ready. The form process is now much smoother, working with |
|
||
public static function doesFormContainAnyErrors(FormView $formView): bool | ||
{ | ||
if ($formView->vars['errors'] ?? null && \count($formView->vars['errors']) > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure - but your suggestion is safer 👍
e3a9104
to
c8b3a28
Compare
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Twig] add `ExposeInTemplate` attribute | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | n/a | License | MIT #232 made all of a component's public properties available directly (without the `this.`) in it's template. This adds an `ExposeInTemplate` attribute to make non-public properties available in this way. Example: ```php use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; #[AsTwigComponent('with_exposed_variables')] final class WithExposedVariables { #[ExposeInTemplate] private string $prop1; // available as `{{ prop1 }}` #[ExposeInTemplate('customProp')] private string $prop2; // available as `{{ customProp }}` #[ExposeInTemplate(name: 'ico', getter: 'fetchIcon')] private string $icon = 'ico-warning'; // available as `{{ ico }}` in the template using `fetchIcon()` as the getter /** * Required to access $prop1 */ public function getProp1(): string { return $this->prop1; } /** * Required to access $prop1 */ public function getProp2(): string { return $this->prop2; } /** * Required to access $this->icon */ public function fetchIcon(): string { return $this->icon; } } ``` **TODO:** - [x] use to expose `form` in `ComponentWithFormTrait` (requires #254) Commits ------- e6c53fd [Twig] add `ExposeInTemplate` attribute
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Twig] add `ExposeInTemplate` attribute | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Tickets | n/a | License | MIT #232 made all of a component's public properties available directly (without the `this.`) in it's template. This adds an `ExposeInTemplate` attribute to make non-public properties available in this way. Example: ```php use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate; #[AsTwigComponent('with_exposed_variables')] final class WithExposedVariables { #[ExposeInTemplate] private string $prop1; // available as `{{ prop1 }}` #[ExposeInTemplate('customProp')] private string $prop2; // available as `{{ customProp }}` #[ExposeInTemplate(name: 'ico', getter: 'fetchIcon')] private string $icon = 'ico-warning'; // available as `{{ ico }}` in the template using `fetchIcon()` as the getter /** * Required to access $prop1 */ public function getProp1(): string { return $this->prop1; } /** * Required to access $prop1 */ public function getProp2(): string { return $this->prop2; } /** * Required to access $this->icon */ public function fetchIcon(): string { return $this->icon; } } ``` **TODO:** - [x] use to expose `form` in `ComponentWithFormTrait` (requires symfony/ux#254) Commits ------- e6c53fd [Twig] add `ExposeInTemplate` attribute
This fixes 3 bugs at once:
This recalculates
formValues
after submit, in case the submitted data changes the form's data or underlying structure (e.g.CollectionType
)If a field is suddenly missing entirely from a form (hint: think removing an embedded CollectionType form), then previously
validatedFields
did not "forget" that field. This caused edge-case bug when removing an embedded form that was validated, then adding a new form a moment later (which would be empty, but now validated).When passing a
FormView
into your component that was already validated, on the first re-render, the validation would be lost and only modified fields would be validated.Ping @Lustmored and @norkunas. This mostly addresses @norkunas's problems with how
getFormValues()
works, but this was also causing trouble for @Lustmored, potentially for different reasons. So, this may not solve the problems addressed in #221, but it's certainly related.Cheers!