I have some fields exposed in filter criteria, and my exposed form style is input required.
So as expected, when i visit the view path i see only the exposed form and no results from the view, until i submit (or apply the filters). What i don't want is to be able to submit an empty form and see all the results from my view, so i check the fields i need to have a value as required but they automatically receive an .error class (so they get a red margin) before i apply the filters (or submit). I would like to have this validation after the form is submitted not before, since, in my understanding, the whole meaning of having input required is to wait for the filters to be applied (and maybe do some validation if something is marked as required) then show results.
This also happens when the form is exposed in a block.
What i wanted is to have my filters exposed in a block and loaded in a region on my home page, then submit the form and see the results in my view. It works as expected with this one little drawback, the required fields in the exposed form receive the .error class before the form is submitted even though the validation should be done after submission
Comment | File | Size | Author |
---|---|---|---|
#26 | 7.x-3.13-views-plugin_exposed_form_check_input-1877678-26.patch | 1.58 KB | IRuslan |
| |||
#11 | 1877678_views_plugin_exposed_form_check_input_2.patch | 2.37 KB | MrHaroldA |
#7 | 1877678_views_plugin_exposed_form_check_input.patch | 815 bytes | MrHaroldA |
Comments
Comment #1
rockaholiciam CreditAttribution: rockaholiciam commentedfacing the same problem and not sure if to simply override it using js or dive deeper into the module (which might end up consuming hell lot of time)
Comment #2
rockaholiciam CreditAttribution: rockaholiciam commentedfacing the same problem and not sure if to simply override it using js or dive deeper into the module (which might end up consuming hell lot of time)
Comment #3
drupnewb CreditAttribution: drupnewb commentedWell the only hint i have is in
sites/all/modules/views/plugins/views_plugin_exposed_form.inc line 151 in function render_exposed_form
its using this function to build the form, $form = drupal_build_form('views_exposed_form', $form_state);
and drupal_build_form does the processing, validation and submission all in one go (from here
http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_build... )
On the other hand, when you build forms in a module you can just use drupal_get_form, and provide the function that builds the render array to it. Then have a _validate function and it runs as expected, you get the form, and on submission it's validated (also shows validation error messages if it doesn't pass). Example here
http://codekarate.com/daily-dose-of-drupal/drupal-7-module-development-p...
Comment #4
ericclaeren CreditAttribution: ericclaeren commentedHi, have the same issue, don't know how to fix this, but this definitely needs some work, because this behavior is incorrect.
Comment #5
MrHaroldA CreditAttribution: MrHaroldA commentedBug confirmed in views-7.x-3.5+37-dev on two separate websites using required exposed textfields and the "exposed form style" set to either "input required" or "basic".
Here's a simple view to test it:
Comment #6
MrHaroldA CreditAttribution: MrHaroldA commentedThe error class was introduced in Drupal 7.17. I managed to trace it to this patch in form.inc.
Here's the (reopened) issue: #1785436: Submission of #required elements without #title and empty value does not display any error
Comment #7
MrHaroldA CreditAttribution: MrHaroldA commented(semi-crosspost with #1785436: Submission of #required elements without #title and empty value does not display any error #22)
Hmmm... Further investigation points to this piece of code in
views/plugins/views_plugin_exposed_form.inc->render_exposed_form()
In this case
'always_process' => TRUE
triggers form validation, which < Drupal 7.17 did throw an error(!), but without the error class. I now believe the patch from this issue reveiled the error in the views exposed filter plugin instead of causing it ...The attached patch only processes the form if there actually is input. This seems to be a really straight-forward fix for a long time error which became visible with the fix in Drupal 7.17, but it also changes the way you have to set default values for exposed filters.
Before the patch:
after the patch:
You now can omit the
if ()
because'always_process'
is triggered when there's input available from $_GET (or session, etc) and will override whatever you set as default value with the input.Bumping it to 'major' since a lot of websites will encounter this bug/misbehaviour when updating to Drupal 7.17+.
Comment #8
MrHaroldA CreditAttribution: MrHaroldA commentedNote to self: do not only check if you've actually attached the patch, also check if you set the status to 'needs review' ;)
Comment #10
MrHaroldA CreditAttribution: MrHaroldA commentedNot only did my patch fail testing, it's also not good enough.
I only check if there's input, not if the input matches the exposed filters in this view. This means that all forms are processed when there's input and the error class returns for all exposed filters on that page.
This also explains this function in
views_plugin_exposed_form_input_required.inc
A revisited patch will probably move that function into
views_plugin_exposed_form.inc
and use it instead of$exposed_input = $this->view->get_exposed_input();
Comment #11
MrHaroldA CreditAttribution: MrHaroldA commentedHere's the revised patch which moves
get_exposed_input()
into theviews_plugin_exposed_form
class. I had to get rid of the static cache because it had to be cached for each identifier.Really hoping someone looks into this ...
Comment #13
MrHaroldA CreditAttribution: MrHaroldA commentedChanging the title to reflect the problem. Still hoping someone looks into this.
Comment #14
MrHaroldA CreditAttribution: MrHaroldA commented#11: 1877678_views_plugin_exposed_form_check_input_2.patch queued for re-testing.
Comment #16
nodecode CreditAttribution: nodecode commentedAny news on this?
I see that the other related issue #1785436: Submission of #required elements without #title and empty value does not display any error is still sort of unresolved and the errors related to the patch in #11 seem mostly (if not all) unrelated to the work MrHaroldA has done. I would be happy to test but not if the patch cannot be applied.
Comment #17
nodecode CreditAttribution: nodecode commentedI tested the patch in #11 against 7.x-3.7, which applied cleanly, and it works!
The .error class is now gone from the filter, however, for those using "required" exposed filters read on....
This patch solves the above issue but reveals another issue #1169092: "required" flag on exposed filters ignored. Since I'm using views with Search API module, my exposed filters are "required" because I don't want results showing when the user hasn't even hit the search button (exposed filter button) yet. But due to the issue I mentioned, ALL results are showing when I visit the view page.
Here's how to use this patch successfully if you are using "required" exposed filters...
1. There is an untested patch https://drupal.org/node/1169092#comment-4932422 which may solve the problem.
2. While "required" filters are still broken in Views you can use the following workaround http://www.agentic.ca/blog/suppressing-views-exposed-filter-output-until...
Comment #18
almc CreditAttribution: almc commentedThis issue is definitely worth fixing and inclusion in the core code, looks like it has been open for almost one year.
Thanks for the link on the workaround, will try to use it meanwhile ...
Comment #20
nodecode CreditAttribution: nodecode commentedDoes anyone know how to debug the Simpletest failures that are holding up this patch? I'd do the leg work if I knew where to start.
Comment #21
anrikun CreditAttribution: anrikun commentedI've tested patch #11 and it works even when using "required" for me.
Comment #22
MrHaroldA CreditAttribution: MrHaroldA commentedLet's try to get this into D8 first; I believe that's the prefered way of changing things in Views these days ...
Thanks for bumping it! ;)
Comment #23
anrikun CreditAttribution: anrikun commentedActually #17 seems right: patch #11 fixes some views but breaks others. I'll try to figure out what's going wrong.
Comment #24
anrikun CreditAttribution: anrikun commentedHere's my workaround for a simple search filter when you don't want results to be displayed until user has actually submitted keywords:
- Use "required input" as exposed form plugin
- Do not check "required" for the keywords exposed filter
- Do not check "remember" either
- Add this code in a custom module:
Comment #25
LendudeMoving this back to the 7.x-3.x queue since this has already been fixed in D8 (well it's working at least).
Comment #26
IRuslan CreditAttribution: IRuslan as a volunteer and at DrupalJedi commentedFor me, the last patch does not work.
Also, I think better to use polymorphism for exposed_filter_applied() instead of moving it to parent class at all.
See my version of the patch.
Comment #28
IRuslan CreditAttribution: IRuslan as a volunteer and at DrupalJedi commentedComment #29
d.novikov CreditAttribution: d.novikov as a volunteer commentedHere is my workaround for 8.x-2.x (inspired by #24):
Comment #30
sagesolutions CreditAttribution: sagesolutions commented@d.novikov Thanks for the fix for drupal 8.2. It worked for me too.
Comment #31
Calar CreditAttribution: Calar commented@d.novikov Thanks for the workaround! #29 Working in Drupal 8.3.2.
Comment #32
glycid CreditAttribution: glycid commentedIn D8 you can furthermore simply use
hook_views_post_execute()
, (in D7 maybe too)in head of your MYMODULE.module file:
use Drupal\views\ViewExecutable;
then:
Comment #33
MustangGB CreditAttribution: MustangGB commentedIf this wants fixing in D8 first then it should be in that queue.
Comment #37
stiras CreditAttribution: stiras commentedHello, I am using D8 and have the same problem. I see that there are two solutions provided, but I am not sure how to implement them. Could someone explain a little more detailed how to add the proposed custom module? Thanks
Comment #38
stiras CreditAttribution: stiras commentedAnyone? Please :)I have finally found a solution that seems to work just fine. I used the patch of user dawehner, suggested at: https://www.drupal.org/project/views/issues/1169092#comment-4932422
It should be noted that the suggested code has already been included in D8 (in /core/modules/views/src/Plugin/views/filter/FilterPluginBase.php), but in a little different way. I had to "reverse" the code back to the original suggestion of dawehner (switching required with optional, etc.) and it worked for me.
I hope that's a permissible way to do it and that it would help others :)
Comment #39
stiras CreditAttribution: stiras commentedComment #40
daffie CreditAttribution: daffie commented@stiras: If the patch that you link to works for you, then could you port that patch to drupal version 8.
Marking the issue back to active, because there is no patch for Drupal version 8.
Comment #43
sagesolutions CreditAttribution: sagesolutions commentedhere I am, looking at this again 4 years later...
I've updated the validation function for Drupal 9.1
Comment #48
LendudeClosing as a duplicate of #2568889: Views exposed text filter set to required shows an empty error and form error on page load since that has seen more activity.