Skip to content

[TwigComponent] Update DOC preMount validation data code #1845

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

Merged
merged 1 commit into from
May 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/TwigComponent/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -468,17 +468,33 @@ component use a ``PreMount`` hook::
{
// validate data
$resolver = new OptionsResolver();
$resolver->setIgnoreUndefined(true);

$resolver->setDefaults(['type' => 'success']);
$resolver->setAllowedValues('type', ['success', 'danger']);
$resolver->setRequired('message');
$resolver->setAllowedTypes('message', 'string');

return $resolver->resolve($data);
return $resolver->resolve($data) + $data;
}

// ...
}

.. note::

In its default configuration, the OptionsResolver treats all props.
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.
To avoid this, use the `ignoreUndefined()` method with `true`. See `ignore not defined options`_ for more info.

$resolver->setIgnoreUndefined(true);

The major drawback of this configuration is that the OptionsResolver will remove every non-defined option when resolving data.
To maintain props that have not been defined within the OptionsResolver, combine the data from the hook with the resolved data.

return $resolver->resolve($data) + $data;


The data returned from ``preMount()`` will be used as the props for mounting.

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