When I tried to add new snippet and previously check some settings for page, but then disable them, the modules tries to validate values and shows error message.

How to reproduce:

1. Open add snippet form page.
2. Check "Enable snippet page".
3. Change "Access" value to "Role".
4. Uncheck "Enable snippet page".
5. Save it.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Niklan created an issue. See original summary.

Niklan’s picture

diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php
index f9b825b..a327d58 100644
--- a/src/Form/GeneralForm.php
+++ b/src/Form/GeneralForm.php
@@ -360,7 +360,7 @@ class GeneralForm extends EntityForm {
     $role = array_values(array_filter($access['role']));
     $form_state->setValue(['page', 'access', 'role'], $role);
 
-    if ($access['type'] == 'role' && count($role) == 0) {
+    if ($page['status'] && $access['type'] == 'role' && count($role) == 0) {
       $form_state->setError($form['page']['access']['role'], $this->t('You must select at least one role if access type is "Role".'));
     }
Chi’s picture

Title: "You must select at least one role if access type is "Role" shows up when page is not enabled. » Do not validate access settings when snippet page is not active
Status: Active » Needs work

Same problem with access permission. Let's fix it as well.

Niklan’s picture

Status: Needs work » Needs review
FileSize
1.63 KB

Update the patch.

diff --git a/src/Form/GeneralForm.php b/src/Form/GeneralForm.php
index f9b825b..9a25ef5 100644
--- a/src/Form/GeneralForm.php
+++ b/src/Form/GeneralForm.php
@@ -346,24 +346,21 @@ class GeneralForm extends EntityForm {
       }
       // Automatically remove '/' and trailing whitespaces from the path.
       $page['path'] = trim($page['path'], ' /');
-
       $form_state->setValue('page', $page);
-    }
 
-    $access = $page['access'];
-
-    if ($access['type'] == 'permission' && !$access['permission']) {
-      $form_state->setError($form['access']['permission'], $this->t('You must select a permission if access type is "Permission".'));
-    }
+      $access = $page['access'];
+      if ($access['type'] == 'permission' && !$access['permission']) {
+        $form_state->setError($form['page']['access']['permission'], $this->t('You must select a permission if access type is "Permission".'));
+      }
 
-    // Make role more export friendly.
-    $role = array_values(array_filter($access['role']));
-    $form_state->setValue(['page', 'access', 'role'], $role);
+      // Make role more export friendly.
+      $role = array_values(array_filter($access['role']));
+      $form_state->setValue(['page', 'access', 'role'], $role);
 
-    if ($access['type'] == 'role' && count($role) == 0) {
-      $form_state->setError($form['page']['access']['role'], $this->t('You must select at least one role if access type is "Role".'));
+      if ($access['type'] == 'role' && count($role) == 0) {
+        $form_state->setError($form['page']['access']['role'], $this->t('You must select at least one role if access type is "Role".'));
+      }
     }
-
   }
 
   /**

  • Niklan authored 7307ba6 on 8.x-1.x
    Issue #2939091 by Niklan: Do not validate access settings when snippet...
Chi’s picture

Status: Needs review » Fixed

Thanks.

Status: Fixed » Closed (fixed)

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