Support from Acquia helps fund testing for Drupal Acquia logo

Comments

vaccinemedia created an issue. See original summary.

vaccinemedia’s picture

Title: Submitting nodes with no paragraph items on a required paragraph field is possible » Add validation for required paragraph fields

Changing the title of the issue to something which makes more sense

vaccinemedia’s picture

Here is a patch as a starter for 10. It adds a validation function which first checks to see if the field is required and if so fails validation and throws up a required field error if:

1) There is no paragraph items or
2) The "actual count" of items is zero which is what occurs when paragraph items are being removed before submitting the node

It's only rough and needs more work. It is for example possible to click on the delete button on the paragraph item and submit the node without actually confirming the removal of the paragraph item. This allows you to submit a node with zero paragraph items even if the paragraph field is required.

jeroen.b’s picture

Status: Active » Needs work

Maybe it's better to calculate the actual items ourself by looping through the items and check their internal properties (removed/confirmed_removed).

panche’s picture

Hi, yes, the real_items_count is not a variable to trust (as I just found out). I will try to give it a shot as well.

panche’s picture

Status: Needs work » Needs review
FileSize
1.25 KB
Dane Powell’s picture

Patch in #6 throws a notice because $value_count can be undefined. This fixes it.

panche’s picture

Adding the use case of nested Paragraphs

thihathit’s picture

Title: Add validation for required paragraph fields » Needs to be update module
Version: 7.x-1.x-dev » 7.x-1.0-rc4
Category: Bug report » Support request
Priority: Normal » Major
Status: Needs review » Needs work

Patch works but needs to update module.
Still happening in 7.x-1.0-rc4

jeroen.b’s picture

Title: Needs to be update module » Add validation for required paragraph fields
Version: 7.x-1.0-rc4 » 7.x-1.x-dev
Category: Support request » Bug report
Priority: Major » Normal
Status: Needs work » Needs review

@thiha.c, that here are buttons/select/input fields does not mean you need to touch them, this might be a useful read.

This is not a major problem.
The title should still be "Add validation for required paragraph fields", as that is what needs to happen.
The version should be the current dev branch, as it's not broken for a specific version.
There is a patch for it so the status should be "Needs review".

If there is a consensus on that the patch works, please change the status to "Reviewed & tested by the community".

thihathit’s picture

okay, thanks. ^^

recrit’s picture

I ran into an issue with Patch #8 for nested structures such as paragraphs within paragraphs. The attached updated patch uses the element's #parents to extract the proper values array within the form state.

gadaniels72’s picture

Status: Needs review » Reviewed & tested by the community

Tested patch #14 and it fixed the required problem. I tested by adding a single required paragraph field to the content type and confirming that the required validation worked. i then added additional paragraph bundles to allow me to nest paragraph items down 5 levels and verified that in each instance, the paragraph items still validated as required.

jeroen.b’s picture

Status: Reviewed & tested by the community » Needs work

Thanks @recrit. However, the given solution seems kind of weird.
Isn't this something core should take core of using hook_field_is_empty? (maybe there's some issue there)

Plus I'm currently trying to get the quality of the code up to par with the D8 version.
That mostly just means we need to add many tests to make sure we won't break anything.
Are you able to create a unit test for this? (and provide 2 patches: 1 with the fixed code + test, 1 with the test only. that way we can see the test actually confirms it is fixed now).

recrit’s picture

@jeroen.b np ... fixed code first, then I'll add tests.

Main changes from patch #12:

  1. Updated validation to use _field_filter_items() which invokes hook_field_is_empty() for the field type module.
  2. Updated paragraphs_field_is_empty() to call paragraphs_item_is_empty() on the $item['entity']. This allows processing the fields within the paragraph to determine if it is empty.
drup16’s picture

I can confirm that #15 patch did work for me and it is now validating in all scenarios even when i do not press "Add more Element".

We are using Paragraph 7.x-1.0-rc4

xamount’s picture

I can confirm that #15 patch worked for me too. Thanks so much for that!

I'm using Drupal 7.56 and Paragraphs 7.x-1.0-rc5

xamount’s picture

Actually I retract my previous comment.

The patch does not cater for the case whereby you add content to a paragraph item via hook_entity_view_alter()

It seems paragraphs_item_is_empty($item['entity']) only checks if the fields are empty.

In my case, the paragraph entity has no fields but the content comes from a view via hook_entity_view_alter which I implemented as follow:

/**
 * Implements hook_entity_view_alter().
 */
function my_module_entity_view_alter(&$build, $type) {
  if ($type == 'paragraphs_item') {

    switch ($build['#bundle']) {
      case 'my_courses' :

      $view = views_get_view('my_view_courses');
      $view->execute_display('page_1');
      $build['dropdown_view']['#markup'] = $view->preview('page_1');

      break;
    }
  }
}

I attached the updated patch which works for me and the interdiff.

The last submitted patch, 18: paragraphs-2558517-18.patch, failed testing. View results

The last submitted patch, 18: paragraphs-2558517-18.patch, failed testing. View results

xamount’s picture

Tidying up patch as my previous patch filed failed testing

Status: Needs review » Needs work

The last submitted patch, 21: paragraphs-2558517-18.patch, failed testing. View results

das-peter’s picture

Status: Needs work » Needs review
FileSize
3.38 KB

Just tried to apply patch in #21 but failed.
And I came across this:

+++ b/paragraphs.module
+++ b/paragraphs.module
@@ -852,7 +852,7 @@ function paragraphs_field_is_empty($item, $field) {

@@ -852,7 +852,7 @@ function paragraphs_field_is_empty($item, $field) {
   elseif (isset($item['entity'])) {
-    return FALSE;
+    return paragraphs_item_is_empty($item['entity']);
   }
   return TRUE;

+++ b/paragraphs.module
@@ -845,15 +845,22 @@ function paragraphs_field_item_to_ids($items) {
 function paragraphs_field_is_empty($item, $field) {
...
-  elseif (isset($item['entity'])) {
+  elseif (isset($item['entity']) &&  !empty($instances)) {
     return paragraphs_item_is_empty($item['entity']);
   }
+  elseif (isset($item['entity']) &&  empty($instances)) {
+    //because of hook_entity_view_alter, the content comes from a view but not stored in a field
+    return FALSE;
+  }
   return TRUE;
 }

I'm not sure how this patch seems to patch the same location twice :O

Manually re-rolled now to the version I think it was intended to be. Fixed some smaller nitpicks:

  • Unnecessary check empty($instance),
  • Double spaces
  • Comments longer than 80 char limit.

Let's see of the bot can apply now.

akerbel.dev’s picture

I put #23 patch into my project and it works - an editor can`t create pages without paragraphs anymore.

sharkbaitdc’s picture

I applied #23 patch and it worked for me. Also works on nested paragraphs. Thank you so much!

Does anyone know if this patch will be included in the module?

lily.yan’s picture

After I applied #23 patch, when I add a node with a paragraph field which is set as required, without adding any paragraph items, the validation error message is given and the node cannot be created. Thanks

unstatu’s picture

Status: Needs review » Reviewed & tested by the community

Same here. #23 worked perfectly. Since we are 4 people with good results, I'll set the issue as RTBC

capysara’s picture

I'm finding this same issue on D8.

  • jstoller committed f7272cf on 7.x-1.x authored by das-peter
    Issue #2558517 by xamount, recrit, panche, Dane Powell, vaccinemedia,...
jstoller’s picture

Status: Reviewed & tested by the community » Fixed

Patch committed to dev.

Status: Fixed » Closed (fixed)

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

delacosta456’s picture

hi
i tried the patch and it correctly .
But in case the field has to be conditionally displayed with the https://www.drupal.org/project/conditional_fields Module, the validation will not occur