-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add a data_help method in Form #26332
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
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 missing tests
|
||
{% block form_help -%} | ||
{% if form.vars.help is not empty %} | ||
<small class="form-text text-muted">{{ form.vars.help|raw }}</small> |
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.
displaying it raw is a bad idea IMO. It opens the door to XSS.
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.
Mmm, is it really a problem ? Because it's set in the FormType by the dev, not by a user. Then, if we want use html in the help text without raw, we can't. I don't really know
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.
Well, requiring to perform HTML escaping when setting the option is insecure IMO. It requires all devs to be aware that this options requires escaping when setting it. And it might totally be set based on some user input (coming from a previous configuration elsewhere) in some cases.
Btw, I would add translation support for the help text instead of requiring to translate it in the form type when setting the option.
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.
Ok, I remove it from Symfony, if someone want to change it, just to extend the Template and add the raw.
By translation you mean use |trans
in Twig ?
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.
yes, as done for labels or other things
{# Help #} | ||
|
||
{% block form_help -%} | ||
{% if form.vars.help is not empty %} |
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.
just help
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.
Thx for the shortcut, I edit it
Yes, I fix existing tests, and wite new (I've never do it, it take a little times, sorry...) |
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.
Great. Thank you for contributing this.
|
||
{% block form_help -%} | ||
{% if help is not empty %} | ||
<small class="form-text text-muted">{{ help|trans }}</small> |
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.
Let's make sure it respects the translation domain.
{{ translation_domain is same as(false) ? help : help|trans({}, translation_domain) }}
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.
Okay, I edit it. (I don't really know what is it, but I trust you ;-))
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.
It checks if translation_domain
is false, it it is we just print help
. If it is not false we use translation with that domain.
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.
I was thinking about TranslationDomain, I don't know what it is, I'd watch.
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.
Have a look here: http://symfony.com/doc/current/components/translation.html#using-message-domains
It is basically a way to categorize translation messages.
Make sure you add this fix every time you print help
. (ie in the form_div_layout.html.twig)
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.
Thanks, and it's done :-) (it's interresting to do PR, it permit to learn lot of things:-))
Just a question, how can I execute tests ? Because when I do:
It return to me alle is ok, but Skip a lot of tests, but in TravisCi, it doesn't skip test and there is a lot of error, I can't try and push everytime to see what say Travis :-( I've try to do something like travis do:
But it does less tests, Skip all, and thrw an exception:
Thanks a lot, and sorry, it's the first tim I'm looking into tests... (I know, it's wrong... :p) |
I usually do Here is a page that describes this in details: https://symfony.com/doc/current/contributing/code/tests.html |
Btw, I see that it is the "deps low" test that fails on Travis. That is okey for this PR since the changes on the different components depends on each other. |
Oki, but I thought it would be nice to add tests for the new elements, right? Test the |
That would be excellent. |
I'm pinging some of our biggest Symfony Form experts so they can double check these changes: @HeahDude, @vudaltsov, @yceruto Thanks! |
Thank you :-) |
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.
Great PR, I like it.
Since we support Foundation framework, we should probably add form_help
there too.
Excellent. This looks real good. There is one thing I'm missing. You need to add an <label for="inputPassword5">Password</label>
<input type="password" id="inputPassword5" class="form-control" aria-describedby="passwordHelpBlock">
<small id="passwordHelpBlock" class="form-text text-muted">
Your password must be 8-20 characters long, contain letters and numbers, and must not contain spaces, special characters, or emoji.
</small> |
@Nyholm I fix it tomorrow :-) |
Good. We also need the the aria-describedby on the . After that I’m very happy. |
@Nyholm This is done, just a little reflexion to avoid add the aria-describedBy, when there is not helpBlock. And some difficulties in the FrameworkBundle views, I don't know wwhat is its usage, but I don't like it :p |
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.
Good job. I did a much more thorough review now.
Could you also make sure fabbot is happy?
|
||
{% block form_help -%} | ||
{% if help is not empty %} | ||
<small id="{{ id }}HelpBlock" class="form-text text-muted">{{ translation_domain is same as(false) ? help : help|trans({}, translation_domain) }}</small> |
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.
id
generated by symfony is snake_case. I suggest changing this to {{ id }}_help
@@ -297,7 +305,8 @@ | |||
<div> | |||
{{- form_label(form) -}} | |||
{{- form_errors(form) -}} | |||
{{- form_widget(form) -}} | |||
{{- form_widget(form, { 'helpBlockDisplayed': true }) -}} |
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.
Couldn't we check if help is not empty
here?
That would make the logic in widget_attributes
simpler
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.
The problem is when a user just display form_widget
for exemple, the form_help
is not call, then we don't have to display the aria-describedBy, because the block doesn't exists.
That's why I do the test later, else, user say if he declare the form_help to do the link between help block and input.
In the row, I do it automatically, because I'm sure the help block is displayed.
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.
I see. Could we try something like:
{{- form_widget(form, { 'helpBlockDisplayed': (help is not empty) }) -}}
or that may be a syntax error.
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.
It's the same problem, we can't do it automatically, the help may be set, but don't displayed if the user do:
{{ form_label(form.name) }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
He doesn't call form_label
, then help is not displayed, but set in the form.
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.
Yeah, But it that a problem?
If the user do:
{{ form_label(form.name) }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
Then no form help will be rendered and no aria-describedBy
will be used.
If the user do:
{{ form_label(form.name) }}
{{ form_errors(form.name) }}
{{ form_widget(form.name) }}
{{ form_help(form.name) }}
Then form help will be rendered but no aria-describedBy
will be used.
We should only make sure that the logic is clean and when the user do:
{{ form_row(form.name) }}
Then form help will be rendered AND aria-describedBy
will be used.
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.
Yes, we can do that, I've do it to permit the user to add the aria-describedBy
when don't use form_row
. Then, I'm not really sure it's the best way to do, the method must work for all.
@@ -384,6 +393,7 @@ | |||
id="{{ id }}" name="{{ full_name }}" | |||
{%- if disabled %} disabled="disabled"{% endif -%} | |||
{%- if required %} required="required"{% endif -%} | |||
{%- if helpBlockDisplayed is defined and helpBlockDisplayed and help is not empty %} aria-describedby="{{ id }}HelpBlock"{% endif -%} |
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.
Replace and helpBlockDisplayed and
with and helpBlockDisplayed same as(true) and
@@ -0,0 +1,3 @@ | |||
<?php if (!empty($help)): ?> | |||
<p id="<?php echo $view->escape($id) ?>HelpBlock" class="help-text"><?php echo $view->escape(false !== $translation_domain ? $view['translator']->trans($help, array(), $translation_domain) : $help) ?></p> |
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.
_help
instead of HelpBlock
@@ -1,3 +1,4 @@ | |||
id="<?php echo $view->escape($id) ?>" name="<?php echo $view->escape($full_name) ?>"<?php if ($disabled): ?> disabled="disabled"<?php endif ?> | |||
<?php if ($required): ?> required="required"<?php endif ?> | |||
<?php echo $attr ? ' '.$view['form']->block($form, 'attributes') : '' ?> | |||
<?php if (isset($helpBlockDisplayed) && $helpBlockDisplayed && !empty($help)): ?> aria-describedby="<?php echo $view->escape($id) ?>HelpBlock"<?php endif ?> |
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.
&& $helpBlockDisplayed &&
=> && true === $helpBlockDisplayed &&
@@ -177,10 +178,12 @@ public function configureOptions(OptionsResolver $resolver) | |||
'attr' => array(), | |||
'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.', | |||
'upload_max_size_message' => $uploadMaxSizeMessage, // internal | |||
'help' => '', |
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.
Should the default be an empty string or null? Spontaneously I would say null
, but Im sure there is a reason you chose the empty string.
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.
I've not test with null... But In the line bellow, we must define the accepted type that is string. That's why I've put an empty string. But Maybe it works well with null, I can try it. Tests will scream if it's not good.
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.
I confirm, if default is null, it trhown error because it expect a string value.
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.
Okey, Could we make it expect a string value and null? Or maybe false
would make more sense.
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.
I can try if I can give him an array of possibilities, but I don't know if it works, I don't know this part of the code.
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.
It's okay :-)
$resolver->setAllowedTypes('help', ['string', 'NULL']);
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.
Great
|
||
$this->assertMatchesXpath($html, | ||
'/span | ||
[@id="nameHelpBlock"] |
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.
Update id
|
||
$this->assertMatchesXpath($html, | ||
'/p | ||
[@id="nameHelpBlock"] |
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.
Update id
@Nyholm I've fixed some of your suggestions :-) Mmm, "Could you also make sure fabbot is happy?", how I do that ? :p |
Oh yes, I've see that, but the correction, isn't the way used in all the other files... That's why I've not fix it and keep the way used in existing files. |
No, you can trust the fabbot. It does only correct/review the files you've edited in this PR. |
For exemple it say to fix:
By:
I've not edited other lines, just add one. That's why I've keep the syntax without the It's the same everywhere, it want to add |
I guess fabbot has updated its rules since those other lines were added. I think we should follow the recommendations by fabbot in this case. |
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.
Could you also run the following to fix the license in the file headers:
curl https://fabbot.io/patch/symfony/symfony/26332/80c66414450cbf0148d4fac6288c83a8b72172ae/license.diff | patch -p0
@@ -92,6 +92,7 @@ | |||
<div class="form-group{% if (not compound or force_error|default(false)) and not valid %} has-error{% endif %}"> | |||
{{- form_label(form) -}} | |||
{{- form_widget(form) -}} |
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.
We should make sure aria-describedby is enabled here
@@ -218,6 +218,7 @@ | |||
<{{ element|default('div') }} class="form-group"> | |||
{{- form_label(form) -}} | |||
{{- form_widget(form) -}} |
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.
We should make sure aria-describedby is enabled here
@@ -271,6 +271,7 @@ | |||
<div class="large-12 columns{% if (not compound or force_error|default(false)) and not valid %} error{% endif %}"> | |||
{{ form_label(form) }} | |||
{{ form_widget(form) }} |
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.
We should make sure aria-describedby is enabled here
It bugs.... The diff is empty, the diff file doesn't exists and I've already apply the patch to add the licence header. |
Thank you @mpiot. |
This PR was merged into the 4.1-dev branch. Discussion ---------- Add a data_help method in Form | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26331 | License | MIT | Doc PR | symfony/symfony-docs#9361 Add a form_help method in twig to display a help message in form. A `help` keyword is added to all FormType to define the message. Commits ------- 585ca28 Add return type hint 859ee03 Revert: remove comment line from twig templates d723756 Fix some mistakes c74e0dc Use spaceless balises in Twig templates 8b937ff Try without try/catch 32bf1f6 Test the renderHelp method in all Tests about help to skip them if necessary. 437b77e Skip renderHelp test as skipped if not override d84be70 Update composer files 075fcfd [FrameworkBundle] Add widgetAtt to formTable/form_row f1d13a8 Fix Fabpot.io 69ded67 Added form_help on horizontal design and removed special variable fd53bc5 Enable aria-described in row for all Templates 98065d3 fabpot.io fix edb95f8 Use array long syntax aada72c Set help option on nul as default f948147 Rename help id (snake_case) 77fa317 Fix Test 30deaa9 PSR fix bf4d08c Add aria-describedBy on input 1f3a15e Rename id 058489d Add an id to the help 6ea7a20 Remove vars option from form_help ba798df FrameworkBundle Tests 4f2581d Use array long syntax f15bc79 Fix coding standards c934e49 Add test without help set 8094804 Add Tests 067c681 Template for table, Foundation and Bootstrap 3 d3e3e49 Fix: check translation domain 2c2c045 Adapt existant tests 831693a Add trans filter e311838 Remove raw filter for help 8b97c1b Use a shortcut to acces help var in Twig template 1b89f9d Add a template fot div_layout c8914f5 Add a data_help method in Form
This PR was squashed before being merged into the 4.1 branch (closes #9361). Discussion ---------- Add help option to FormType Add documentation about the new feature symfony/symfony#26332 Commits ------- 5bdf708 Add help option to FormType
Maybe this should be added to UPGRADE-4.1.md as well? Because when you have your own Form Type Extension to handle |
Would you like to open this PR? |
This PR was merged into the 4.1 branch. Discussion ---------- [Form] mention "help" field option in UPGRADE file | Q | A | ------------- | --- | Branch? | 4.1 <!-- see below --> | Bug fix? | no | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT When you have your own Form Extension to handle help option (e.g. similar to the one mentioned in the [blog post](https://symfony.com/blog/new-in-symfony-4-1-form-field-help)), it starts to render twice in the form after upgrading to 4.1 (where #26332 was added) <!-- Write a short README entry for your feature/bugfix here (replace this comment block.) This will help people understand your PR and can be used as a start of the Doc PR. Additionally: - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. --> Commits ------- 744362a update UPGRADE-4.1 for feature #26332 Form field help option
* 4.1: (22 commits) [HttpKernel] Fix restoring trusted proxies in tests Update UPGRADE-4.0.md [Messenger] Fix suggested enqueue adapter package bumped Symfony version to 4.1.1 updated VERSION for 4.1.0 updated CHANGELOG for 4.1.0 Insert correct parameter_bag service in AbstractController Revert "feature #26702 Mark ExceptionInterfaces throwable (ostrolucky)" CODEOWNERS: some more rules removed unneeded comments in tests removed unneeded comments in tests Change PHPDoc in ResponseHeaderBag::getCookies() to help IDEs [HttpKernel] fix registering IDE links update UPGRADE-4.1 for feature #26332 Form field help option [HttpKernel] Set first trusted proxy as REMOTE_ADDR in InlineFragmentRenderer. [Process] Consider \"executable\" suffixes first on Windows Triggering RememberMe's loginFail() when token cannot be created bumped Symfony version to 4.1.0 updated VERSION for 4.1.0-BETA3 updated CHANGELOG for 4.1.0-BETA3 ...
Do you think it's possible to have this feature available also in Symfony 3.4? |
@loru88 We never add new features in patch versions, but only do that in minor releases. You can read more about our release process at http://symfony.com/doc/current/contributing/community/releases.html. |
Add a form_help method in twig to display a help message in form. A
help
keyword is added to all FormType to define the message.