Problem/Motivation

When no source objects are selected in the sources managment area over the tmgmt module the message showing that no sources were selected is not showing up.

To recreate:

1.- go to /admin/tmgmt/sources

2.- unselect any sources previously selected.

3.- click on request translation and nothing will show up.

Proposed resolution

Check the form source code and fix it.

Remaining tasks

create a patch

User interface changes

API changes

Data model changes

Original report by [username]

CommentFileSizeAuthor
#44 interdiff-2532716-42-44.txt555 bytesjuanse254
#44 Error_message_not_showing-2532716-44.patch3.75 KBjuanse254
#42 interdiff-2532716-39-42.txt499 bytesjuanse254
#42 Error_message_not_showing-2532716-42.patch4.01 KBjuanse254
#39 interdiff-2532716-38-39.txt769 bytesjuanse254
#39 Error_message_not_showing-2532716-39.patch4.06 KBjuanse254
#37 interdiff-2532716-36-38.txt2.81 KBjuanse254
#37 Error_message_not_showing-2532716-38.patch4.07 KBjuanse254
#36 interdiff-2532716-34-36.txt4.47 KBjuanse254
#36 Error_message_not_showing-2532716-36.patch6.84 KBjuanse254
#34 interdiff-2532716-31-34.txt775 bytesjuanse254
#34 Error_message_not_showing-2532716-34.patch4.13 KBjuanse254
#31 interdiff-2532716-29-31.txt4.02 KBjuanse254
#31 Error_message_not_showing-2532716-31.patch4.03 KBjuanse254
#29 interdiff-2532716-27-29.txt3.11 KBjuanse254
#29 Error_message_not_showing-2532716-29.patch3.73 KBjuanse254
#27 interdiff-2532716-23-27.txt0 bytesjuanse254
#27 Error_message_not_showing-2532716-27.patch2.24 KBjuanse254
#23 interdiff-2532716-20-23.txt816 bytesjuanse254
#23 Error_message_not_showing-2532716-2e.patch2.24 KBjuanse254
#20 interdiff-2532716.txt1.71 KBjuanse254
#20 Error_message_not_showing-2532716-20.patch2.33 KBjuanse254
#18 interdiff-2532716-16-18.txt1.07 KBjuanse254
#18 Error_message_not_showing-253276-18.patch1.73 KBjuanse254
#16 Interdiff-2532716-14-16.txt694 bytesjuanse254
#16 Error_message_not_showing-253276-16.patch1.73 KBjuanse254
#14 interdiff-2532716-11-14.txt483 bytesjuanse254
#14 Error_message_not_showing-253276-14.patch1.73 KBjuanse254
#11 interdiff-2532716-9-11.txt551 bytesjuanse254
#11 Error_message_not_showing-253276-11.patch1.71 KBjuanse254
#9 interdiff-2532716-7-9.txt1.52 KBjuanse254
#9 Error_message_not_showing-253276-9.patch1.7 KBjuanse254
#7 interdiff-253276-2-7.txt1.65 KBjuanse254
#7 Error_message_not_showing-253276-7.patch1.46 KBjuanse254
#2 Error_message_not_showing-253276-2.patch1.51 KBjuanse254
#2 Screen Shot 2015-07-14 at 09.41.00.png81.81 KBjuanse254

Comments

juanse254’s picture

juanse254’s picture

Uncommented line and fixed relating to issue #2529610: Validation errors shown repeatingly also test added.

juanse254’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: Error_message_not_showing-253276-2.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: Error_message_not_showing-253276-2.patch, failed testing.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new1.46 KB
new1.65 KB

Fixed failing test.

Status: Needs review » Needs work

The last submitted patch, 7: Error_message_not_showing-253276-7.patch, failed testing.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new1.7 KB
new1.52 KB

Fixed commented method which was not working. Added Test as well

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
+    $items = &$form['items']['#value'];
+    $issearch = FALSE;
+    if ($_POST['op'] == t('Search')) {
+      $issearch = TRUE;
+    }
+    if (empty($items) && !$issearch) {

Why do you need to get the value from $form? And you shouldn't use $_POST, again, use $form_state->getValue().

juanse254’s picture

I had to use $form instead of $form_state because $form doesnt add the value to the array if it has not changed while $form_state fills the array with the options and if one is chosen sets its value to 1 else the value is set to 0. Therefore i would have to check that all elements of the $form_state->getValue('items') are set to zero in order to know if no values are chosen.

Added fix for Post.

juanse254’s picture

Status: Needs work » Needs review
berdir’s picture

Status: Needs review » Needs work
+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
+    $items = $form['items']['#value'];

Did you try array_filter() on getValue() ?

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB
new483 bytes

Okay, that worked :).

sasanikolic’s picture

Status: Needs review » Needs work
+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
+    $issearch = FALSE;
...
+      $issearch = TRUE;

Based on the coding standards, I would separate these two words in the variable with an underscore. It is much easier to read that way.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB
new694 bytes

Fixed with comment 15.

sasanikolic’s picture

Status: Needs review » Needs work
+++ b/modules/demo/src/Tests/TMGMTDemoTest.php
@@ -50,6 +50,9 @@ class TMGMTDemoTest extends TMGMTTestBase {
+    $this->assertUniqueText(t("You didn't select any source objects"));

+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
+      $form_state->setError($form, t("You didn't select any source objects"));

Sorry, you're missing a dot at the end of the sentence. :)

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new1.73 KB
new1.07 KB

New patch, please try to review it all at once so i dont have to create tons of patches on syntax changes.

mbovan’s picture

Status: Needs review » Needs work
  1. +++ b/modules/demo/src/Tests/TMGMTDemoTest.php
    @@ -50,6 +50,9 @@ class TMGMTDemoTest extends TMGMTTestBase {
    +    $this->assertUniqueText(t("You didn't select any source objects."));
    
    +++ b/src/Form/SourceOverviewForm.php
    @@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
    +      $form_state->setError($form, t("You didn't select any source objects."));
    

    Comment #10 from #2511822: False past broken info on status report :)

  2. +++ b/src/Form/SourceOverviewForm.php
    @@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
    +    if ($form_state->getValue('op') == t('Search')) {
    

    I guess you can use $form['search_wrapper']['search_submit']['#value'] == $form_state->getTriggeringElement()['#value'] so we don't need to translate new Search again.

    Btw, there is a Cancel button which displays "You didn't select any source objects" when triggered. Is this Cancel about translation or filters? If it is like Search, then I think we should do the same for it.

  3. +++ b/src/Form/SourceOverviewForm.php
    @@ -153,10 +153,15 @@ class SourceOverviewForm extends FormBase {
    +    if (empty($items) && !$is_search) {
    

    You can use !$items instead of empty($items).

juanse254’s picture

Fixed 2, 3 and also added coverage for the cancel button which is not always present. i left 1 because i have a single quote inside of the string so maybe using double quote will be better in this case.

juanse254’s picture

Status: Needs work » Needs review
LKS90’s picture

Status: Needs review » Needs work
+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,21 @@ class SourceOverviewForm extends FormBase {
+    if (array_key_exists('search_cancel', $form['search_wrapper'])) {

Is always true?

Remove that, also looks nicer than two nested if statements.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new2.24 KB
new816 bytes

This is actually an issue since in /admin/tmgmt/sources/locale/default the cancel button is not shown. Im uploading the patch and creating a followup since the test have to be fixed, after that the patch will be positive.

issue #2535190: LocaleSourceUITest testing wrong path in value

Status: Needs review » Needs work

The last submitted patch, 23: Error_message_not_showing-2532716-2e.patch, failed testing.

mbovan’s picture

+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,19 @@ class SourceOverviewForm extends FormBase {
+    if ($form['search_wrapper']['search_cancel']['#value'] == $form_state->getTriggeringElement()['#value']) {
+      $is_cancel = TRUE;
+    }

I think you have to check if $form['search_wrapper']['search_cancel'] exists before calling the child element.

juanse254’s picture

In the issue posted in comment 23 it states that the search_cancel must always be present. Therefore until the patch gets commited, its pointless to check if the search_cancel exists since it must always exist.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new2.24 KB
new0 bytes

Rebased the patch for applying.

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Form/SourceOverviewForm.php
@@ -153,10 +153,19 @@ class SourceOverviewForm extends FormBase {
+    $items = array_filter($form_state->getValue('items'));
+    $is_search = FALSE;
+    $is_cancel = FALSE;
+    if ($form['search_wrapper']['search_submit']['#value'] == $form_state->getTriggeringElement()['#value']) {
+      $is_search = TRUE;
+    }
+    if ($form['search_wrapper']['search_cancel']['#value'] == $form_state->getTriggeringElement()['#value']) {
+      $is_cancel = TRUE;
+    }
+    if (!$items && !$is_search && !$is_cancel) {
+      $form_state->setError($form, t("You didn't select any source objects."));
+    }
+    $plugin = $form_state->get('plugin');
     $item_type = $form_state->get('item_type');

Not really happy about this.

We fixed locale now but this still means we have hardcoded assumptions that those form elements exist.. there might be a source that doesn't even have a search.

We could make it conditional I guess, but what about turning this around...

We could move it to overviewFormValidate() in the plugin Ui base class.

Or, what if we add a validateItemsSelected() method to the baes class and explicitly add it to the buttons that actually want it? Combined with unifying that part into the base class, that would make it standardized but still overridable.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new3.73 KB
new3.11 KB

Okay, added to the overviewformValidate() and added to sourceoverview as well. Test included.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/modules/demo/src/Tests/TMGMTDemoTest.php
    @@ -50,6 +50,15 @@ class TMGMTDemoTest extends TMGMTTestBase {
         $this->assertText(t('2 jobs need to be checked out.'));
    +    // Check to see if no items are selected and the error message pops up.
    +    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Request translation'));
    +    $this->assertUniqueText(t("You didn't select any source objects."));
    +    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Search'));
    +    $this->assertNoText(t("You didn't select any source objects."));
    +    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Cancel'));
    +    $this->assertNoText(t("You didn't select any source objects."));
    +    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Add to cart'));
    +    $this->assertUniqueText(t("No job items were selected."));
    

    How is that we have two different messages here?

  2. +++ b/src/Form/SourceOverviewForm.php
    @@ -135,6 +135,7 @@ class SourceOverviewForm extends FormBase {
         $form['actions']['submit'] = array(
           '#type' => 'submit',
    +      '#validate' => array('::validateItemsSelected'),
           '#value' => t('Request translation'),
         );
    

    The same validate needs to be added to the add to cart form.

  3. +++ b/src/Form/SourceOverviewForm.php
    @@ -212,5 +210,23 @@ class SourceOverviewForm extends FormBase {
    +    $items = array_filter($form_state->getValue('items'));
    +    $is_search = FALSE;
    +    $is_cancel = FALSE;
    +    if ($form['search_wrapper']['search_submit']['#value'] == $form_state->getTriggeringElement()['#value']) {
    +      $is_search = TRUE;
    +    }
    +    if ($form['search_wrapper']['search_cancel']['#value'] == $form_state->getTriggeringElement()['#value']) {
    +      $is_cancel = TRUE;
    +    }
    +    if (!$items && !$is_search && !$is_cancel) {
    +      $form_state->setError($form, t("You didn't select any source objects."));
    +    }
    +  }
    

    Still not cleaned up.

  4. +++ b/src/SourcePluginUiBase.php
    @@ -60,6 +60,10 @@ class SourcePluginUiBase extends PluginBase implements SourcePluginUiInterface {
         // Nothing to do here by default.
    +    $items = array_filter($form_state->getValue('items'));
    +    if (!$items && ($form_state->getTriggeringElement('#value') == t('Request translation') || ($form_state->getTriggeringElement('#value') == t('Cancel')))) {
    +      $form_state->setError($form, t("You didn't select any source objects."));
    +    }
    

    This is not needed?

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new4.03 KB
new4.02 KB

Comments taken into account.

mbovan’s picture

  1. +++ b/src/Form/SourceOverviewForm.php
    @@ -153,10 +155,7 @@ class SourceOverviewForm extends FormBase {
    -    /*if (!$form_state->getValue('values')) {
    -      $form_state->setErrorByName('items', t("You didn't select any source objects"));
    -    }*/
    -    $plugin =  $form_state->get('plugin');
    +    $plugin = $form_state->get('plugin');
         $item_type = $form_state->get('item_type');
         // Execute the validation method on the source plugin controller.
         $source_ui = $this->sourceManager->createUIInstance($plugin);
         $source_ui->overviewFormValidate($form, $form_state, $item_type);
    

    I would rewrite this as:

        $source_ui = $this->sourceManager->createUIInstance($form_state->get('plugin'));
        $source_ui->overviewFormValidate($form, $form_state, $form_state->get('item_type'));
    

    Same for submitForm()

  2. +++ b/src/Form/SourceOverviewForm.php
    @@ -212,5 +211,15 @@ class SourceOverviewForm extends FormBase {
    +  /**
    +   * {@inheritdoc}
    +   */
    +  public function validateItemsSelected(array $form, FormStateInterface $form_state) {
    

    Method documentation seems wrong?

  3. +++ b/src/Form/SourceOverviewForm.php
    @@ -212,5 +211,15 @@ class SourceOverviewForm extends FormBase {
    +  public function validateItemsSelected(array $form, FormStateInterface $form_state) {
    +    $items = array_filter($form_state->getValue('items'));
    +    if (!$items) {
    +      $form_state->setError($form, t("You didn't select any source items."));
    +    }
    +  }
    
    +++ b/tmgmt.module
    @@ -1066,7 +1066,7 @@ function tmgmt_source_add_to_cart_submit(array $form, FormStateInterface $form_s
     function tmgmt_cart_source_overview_validate(array $form, FormStateInterface $form_state) {
       $items = array_filter($form_state->getValue('items'));
       if (empty($items)) {
    -    $form_state->setError($form, t('No job items were selected.'));
    +    $form_state->setError($form, t("You didn't select any source items."));
       }
     }
    

    Are the both methods (with same code) needed? If yes, can we call tmgmt_cart_source_overview_validate() from validateItemsSelected() and avoid code duplication?

mbovan’s picture

Status: Needs review » Needs work
juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new4.13 KB
new775 bytes

1.- Has nothing to do with the issue.
2.- Applied.
3.- Applied.

LKS90’s picture

Status: Needs review » Needs work
+++ b/modules/demo/src/Tests/TMGMTDemoTest.php
@@ -50,6 +50,15 @@ class TMGMTDemoTest extends TMGMTTestBase {
+    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Search'));
...
+    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Cancel'));
...
+    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Add to cart'));

$this->drupalPostForm(NULL, [], t('Search')); is shorter and consistent regarding the array syntax. Leave the path in the first drupalPostForm, replace the rest with NULL (since we already are on that page), switch the array()'s to [].

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new6.84 KB
new4.47 KB

Done.

juanse254’s picture

Forgot to rebase, my bad.

LKS90’s picture

Status: Needs review » Needs work
+++ b/modules/demo/src/Tests/TMGMTDemoTest.php
@@ -50,6 +50,15 @@ class TMGMTDemoTest extends TMGMTTestBase {
+    $this->drupalPostForm('admin/tmgmt/sources', array(), t('Request translation'));

another array()

The interdiff is something, but not an interdiff :P.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new4.06 KB
new769 bytes

My bad, fixed that and added right interdiff.

LKS90’s picture

Status: Needs review » Reviewed & tested by the community

Looks good now.

berdir’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/src/Form/SourceOverviewForm.php
    @@ -130,11 +130,13 @@ class SourceOverviewForm extends FormBase {
           '#title' => t('Operations'),
           '#open' => TRUE,
    -      '#attributes' => array('class' => array('tmgmt-source-operations-wrapper'))
    +      '#attributes' => array('class' => array('tmgmt-source-operations-wrapper')),
    +      '#validate' => array('::validateItemsSelected'),
    

    A #validate on a details element doesn't do anything?

  2. +++ b/src/Form/SourceOverviewForm.php
    @@ -212,5 +211,17 @@ class SourceOverviewForm extends FormBase {
    +   */
    +  public function validateItemsSelected(array $form, FormStateInterface $form_state) {
    +    tmgmt_cart_source_overview_validate($form, $form_state);
    +  }
    

    This is a bit weird but OK, can live with that.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new4.01 KB
new499 bytes

Done :).

mbovan’s picture

Status: Needs review » Needs work
+++ b/src/Form/SourceOverviewForm.php
@@ -130,11 +130,12 @@ class SourceOverviewForm extends FormBase {
       '#open' => TRUE,
-      '#attributes' => array('class' => array('tmgmt-source-operations-wrapper'))
+      '#attributes' => array('class' => array('tmgmt-source-operations-wrapper')),
     );

I guess then this change is not needed.

juanse254’s picture

Status: Needs work » Needs review
StatusFileSize
new3.75 KB
new555 bytes

Done

mbovan’s picture

Seems fine.

LKS90’s picture

Status: Needs review » Reviewed & tested by the community

Looking good now.

berdir’s picture

Status: Reviewed & tested by the community » Fixed

Ok, committed, thanks.

  • Berdir committed 344a473 on 8.x-1.x authored by juanse254
    Issue #2532716 by juanse254: Error message not showing when no source...

Status: Fixed » Closed (fixed)

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