Skip to content

Commit 416753f

Browse files
committed
minor #1845 [TwigComponent] Update DOC preMount validation data code (cavasinf)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [TwigComponent] Update DOC preMount validation data code | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Issues | Fix #1704 <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT Fixes the missing doc part as discussed in #1704 Commits ------- 6c14a78 [TwigComponent] Update DOC preMount validation data code
2 parents 4d609f2 + 6c14a78 commit 416753f

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/TwigComponent/doc/index.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,17 +468,33 @@ component use a ``PreMount`` hook::
468468
{
469469
// validate data
470470
$resolver = new OptionsResolver();
471+
$resolver->setIgnoreUndefined(true);
472+
471473
$resolver->setDefaults(['type' => 'success']);
472474
$resolver->setAllowedValues('type', ['success', 'danger']);
473475
$resolver->setRequired('message');
474476
$resolver->setAllowedTypes('message', 'string');
475477

476-
return $resolver->resolve($data);
478+
return $resolver->resolve($data) + $data;
477479
}
478480

479481
// ...
480482
}
481483

484+
.. note::
485+
486+
In its default configuration, the OptionsResolver treats all props.
487+
However, if more props are passed than the options defined in the OptionsResolver, an error will be prompted, indicating that one or more options do not exist.
488+
To avoid this, use the `ignoreUndefined()` method with `true`. See `ignore not defined options`_ for more info.
489+
490+
$resolver->setIgnoreUndefined(true);
491+
492+
The major drawback of this configuration is that the OptionsResolver will remove every non-defined option when resolving data.
493+
To maintain props that have not been defined within the OptionsResolver, combine the data from the hook with the resolved data.
494+
495+
return $resolver->resolve($data) + $data;
496+
497+
482498
The data returned from ``preMount()`` will be used as the props for mounting.
483499

484500
.. note::
@@ -1654,3 +1670,4 @@ https://symfony.com/doc/current/contributing/code/bc.html
16541670
.. _`CVA (Class Variant Authority)`: https://cva.style/docs/getting-started/variants
16551671
.. _`shadcn/ui`: https://ui.shadcn.com
16561672
.. _`tales-from-a-dev/twig-tailwind-extra`: https://github.com/tales-from-a-dev/twig-tailwind-extra
1673+
.. _`ignore not defined options`: https://symfony.com/doc/current/components/options_resolver.html#ignore-not-defined-options

0 commit comments

Comments
 (0)