When searching via the main search_form, if a user simply clicks the "Search" button with nothing in the keywords textbox, the page refreshes with a "Please enter some keywords" message. However, if the user does the same from the search_block_form, they are simply sent to an empty main search_form with no message. Shouldn't both of these (identical) actions result in the same "Please enter some keywords" message being displayed?

The following patch is a very simple port of the search_form_submit() code to search_box_form_submit(). It doesn't use a "processed_keys" value which seems like an error, but in looking through the code I couldn't figure out exactly how that functioned. :-/

--- /nfs/home/D/dwong/search.module	2009-07-07 15:35:50.000000000 -0400
+++ search.module	2009-07-13 23:45:56.620410000 -0400
@@ -1081,6 +1081,12 @@ function search_box(&$form_state, $form_
  * Process a block search form submission.
  */
 function search_box_form_submit($form, &$form_state) {
+  $keys = $form_state['values']['search_block_form'];
+  if ($keys == '') {
+    form_set_error('keys', t('Please enter some keywords.'));
+    // Fall through to the drupal_goto() call.
+  }
+
   $form_id = $form['form_id']['#value'];
   $form_state['redirect'] = 'search/node/'. trim($form_state['values'][$form_id]);
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhodgdon’s picture

Version: 6.13 » 7.x-dev

This is still a problem in Drupal 7, and should be fixed there first, then ported to Drupal 6.

jhodgdon’s picture

Category: feature » bug

And I think it's a bug not a feature request.

lelutin’s picture

I've rerolled dwong127's modification upon HEAD and it seems to be working for me. Sending the patch so that it can be tested by testbot.

lelutin’s picture

Status: Active » Needs review
FileSize
607 bytes

oops. resending correctly this time

I can't confirm anything about that "processed_keys" thing that dwong127 mentioned, please someone advice.

Status: Needs review » Needs work
Issue tags: -D7csmtl

The last submitted patch, 0001-Empty-search-from-block-doesn-t-display-error.diff, failed testing.

lelutin’s picture

Status: Needs work » Needs review
Issue tags: +D7csmtl
jhodgdon’s picture

Issue tags: -D7csmtl +Needs tests
FileSize
602 bytes

This patch needed a small modification. The comment says it falls through to drupal_goto(), but there is no drupal_goto() call (it is using form redirect).

It also probably needs a test, assuming the test bot thinks it's OK.

douggreen’s picture

FileSize
831 bytes

RTBC, but slightly streamlined patch

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

OK, RTBC assuming the test bot agrees.

webchick’s picture

Hm. Is there a reason we don't just set the field to required?

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs work

Duh. I'll look into that.

jhodgdon’s picture

Assigned: Unassigned » jhodgdon

I'm looking into this one.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
31.51 KB

Well. I have an answer as to why. I set the field to #required => TRUE for both the form in the search block and the form on the search page.

From the search block, I thought the error message was confusing. For one thing, it was in the main body of the page, not very near the search box (see screen shot). For another thing, since the search keywords field has no title, the message says "field is required" instead of a more helpful "please enter some keywords". And there was also a validation message complaining about a missing #title from the default form validation (see screen shot).

So I guess it could be moved into a custom validation function instead of using #required, but I don't really see why this would improve the code much over the current code?

Tentatively suggesting we commit the last patch instead...

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

I guess it was at RTBC before webchick asked that last question.

nadavoid’s picture

FileSize
1.53 KB

Here's a re-rolled patch with a test added. The added test is:

    // Test an empty search via the block form, from the front page.
    $terms = array('search_block_form' => '');
    $this->drupalPost('node', $terms, t('Search'));
    $this->assertText('Please enter some keywords');
dwightaspinwall’s picture

Reviewed #15 and tested by community. Ready to be committed.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Let's also maybe get a comment to the effect of jhodgdon's in #13. I forsee us not being the only people to wonder about this.

Also, can we add one more assertion to that test to check and see if you're redirected properly to the search page after submitting the block form? Otherwise, looks good! :)

nadavoid’s picture

Thanks for the feedback webchick. I will make those changes and submit a new patch. I will need a day or so since I'm travelling home from drupalcon at the moment.

nadavoid’s picture

FileSize
2.27 KB

I added some comments reflecting #13, and added two new assertions to confirm correct redirection. One assertion is for when the search block form has something in it, and one for when it's submitted empty. Please let me know if this patch needs any other attention. This is my first core patch, so I want to be sure this is good and proper, and that I get started off on the right foot. Thanks!

nadavoid’s picture

Status: Needs work » Needs review

updating status to "needs review"

hefox’s picture

just visually looked at it but "because the search keywords field has not title." not => no or not => does not have

nadavoid’s picture

FileSize
2.27 KB

Fixed a silly grammar mistake. Thanks for spotting that hefox.

Status: Needs review » Needs work

The last submitted patch, 518512.patch, failed testing.

jhodgdon’s picture

Those test failures look real...

nadavoid’s picture

Assigned: jhodgdon » nadavoid

Yep, it's actually an error in the two tests I added. They assume that Clean URLs are enabled. This evening I'll look for other examples of confirming correct URL redirection, and get my tests corrected. If you know of any I should look at for reference, please point them out to me. Thanks!

Assigning to me because I want to finish it up. Thank you for your patience!

jhodgdon’s picture

I searched through the code base for getUrl(), and it looks like quite a few tests are using it. For instance, in module/aggregator/aggregator.test, they compare getUrl() to url('whatever'), rather than using getAbsoluteURL('whatever'), so you might try that in your test?

nadavoid’s picture

FileSize
2.6 KB

Thanks for the suggestion. Using the core url() function is exactly what I needed to do, I believe. I'll let the testing bot help determine that. Let's see if I get a green light on this one. Thanks again for the help!

nadavoid’s picture

Status: Needs work » Needs review

updating status to "needs review"

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
2.32 KB

You left two commented-out code lines in like this:

+    // $this->assertEqual($this->getUrl(), $this->getAbsoluteUrl('search/node/' . $terms['search_block_form']));

Those need to be removed.

I've confirmed that the patch works (fixes the problem), and the test bot is also happy. The tests look reasonable to me.

So I just removed those two commented code lines from your version, rerolled, and it should be ready to go now.

nadavoid’s picture

Doh! The commented-out code. I'll be sure to make an extra pass visually next time I submit a patch to make sure that type of thing is removed.

marcingy’s picture

Issue tags: -Needs tests

#29: 518512-fixcomments.patch queued for re-testing.

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

The last submitted patch, 518512-fixcomments.patch, failed testing.

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
2.31 KB

The reason that this test is failing is that the redirects have changed:

+    // Test an empty search via the block form, from the front page.
+    $terms = array('search_block_form' => '');
+    $this->drupalPost('node', $terms, t('Search'));
+    $this->assertText('Please enter some keywords');
+
+    // Confirm that the user is redirected to the search page, when form is submitted empty.
+    $this->assertEqual(
+      $this->getUrl(),
+      url('search/node/', array('absolute' => TRUE)),
+      t('Redirected to correct url.')
+    );

A no-term search ends up at URL 'search' rather than 'search/node' now, so this last assertEqual is failing (it needs to be changed so it checks url('search', ...) instead. See #245103: Search page tabs not highlighting for more information on this change.

Status: Needs review » Needs work

The last submitted patch, 518512-fixtest.patch, failed testing.

jhodgdon’s picture

Well, guess that didn't quite do it...

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
2.31 KB

Ah. The redirect url is url('search') not url('search/'). This should do it.

jhodgdon’s picture

Issue tags: -Needs tests

#36: 518512-fixurl.patch queued for re-testing.

jhodgdon’s picture

Issue tags: +Needs tests

#36: 518512-fixurl.patch queued for re-testing.

jhodgdon’s picture

Issue tags: -Needs tests +Quick fix

Adding tag. This is really a very small change - three lines of code, several of comments, and some additional tests. Could we possibly get it in?

jhodgdon’s picture

Issue tags: -Quick fix

#36: 518512-fixurl.patch queued for re-testing.

Status: Needs review » Needs work
Issue tags: +Quick fix

The last submitted patch, 518512-fixurl.patch, failed testing.

jhodgdon’s picture

Hmmm. "Redirected to correct URL" in the search block test is failing. Need to investigate...

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
2.29 KB

A committed patch from another issue changed the blank page URL to search/node rather than just search, which is why that test was failing.

Also the above patch would not apply.

So here's a redo.

Status: Needs review » Needs work

The last submitted patch, 518512-43.patch, failed testing.

jhodgdon’s picture

Huh, that's strange. That patch passed that test on my test box. Will need to investigate (possibly by disabling clean URLs to make sure that's not the problem again).

jhodgdon’s picture

Status: Needs work » Needs review
FileSize
2.29 KB

Argh. I see it: it's redirecting to search/node/ not search/node

This should do the trick.

jhodgdon’s picture

Issue tags: -Quick fix

#46: 518512-46.patch queued for re-testing.

jhodgdon’s picture

Issue tags: +Quick fix

#46: 518512-46.patch queued for re-testing.

pwolanin’s picture

Status: Needs review » Reviewed & tested by the community

This makes sense for the simple search block to behave the same way when there are no keys.

webchick’s picture

Version: 7.x-dev » 6.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

While this patch does touch strings, it's re-using an existing one so shouldn't have any bearing on string freeze.

Committed to HEAD. Thanks!

Marking for backport to D6.

Albert Volkman’s picture

Status: Patch (to be ported) » Needs review
FileSize
969 bytes

D6 backport.

Albert Volkman’s picture

Patch name fail. Reuploading.

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

This patch seems simple and straightforward enough that it could be committed to 6.x.

pwolanin’s picture

Why is this in the submit function and not validate?

jhodgdon’s picture

pwolanin: The reason is because the validate with form_set_error would put the message in a weird spot, just as it would if the #required was used, as described in comments above.

pwolanin’s picture

Is this really the correct behavior? Some modules might want to execute a search with no keywords as long as other "conditions" are present

pwolanin’s picture

pwolanin’s picture

jhodgdon’s picture

The only thing this patch does is make it so that a search from the Search block works the same as a search from the Search page.

The last submitted patch, 51: search_from_block_with_no_keywords-518512-51-d6.patch, failed testing.

jhodgdon’s picture

Version: 6.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Fixed

Talked with Gabor (the Drupal 6 branch maintainer) and D6 issues are really not being committed unless they're really essential -- we really don't have a test system for Drupal 6 and it's too dangerous. So... putting this back to D7 / fixed, even though this is a simple three-line patch.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.