Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
When adding a node which has a paragraph field which has been set as required it is possible to submit the node without adding any paragraph items.
Comment | File | Size | Author |
---|---|---|---|
#23 | paragraphs-is_required_check_fails-2558517-23.patch | 3.38 KB | das-peter |
| |||
#21 | paragraphs-2558517-18.patch | 3.97 KB | xamount |
#18 | interdiff-2558517-15-18-do-not-test.diff | 1.16 KB | xamount |
#18 | paragraphs-2558517-18.patch | 4.4 KB | xamount |
#15 | paragraphs-2558517-15.patch | 2.95 KB | recrit |
|
Comments
Comment #2
vaccinemedia CreditAttribution: vaccinemedia at CTI Digital commentedChanging the title of the issue to something which makes more sense
Comment #3
vaccinemedia CreditAttribution: vaccinemedia at CTI Digital commentedHere 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.
Comment #4
jeroen.b CreditAttribution: jeroen.b at .VDMi/ commentedMaybe it's better to calculate the actual items ourself by looping through the items and check their internal properties (removed/confirmed_removed).
Comment #5
panche CreditAttribution: panche commentedHi, 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.
Comment #6
panche CreditAttribution: panche commentedComment #7
Dane Powell CreditAttribution: Dane Powell at Acquia commentedPatch in #6 throws a notice because $value_count can be undefined. This fixes it.
Comment #8
panche CreditAttribution: panche commentedAdding the use case of nested Paragraphs
Comment #9
thihathit CreditAttribution: thihathit commentedPatch works but needs to update module.
Still happening in 7.x-1.0-rc4
Comment #10
jeroen.b CreditAttribution: jeroen.b at .VDMi/ commented@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".
Comment #11
thihathit CreditAttribution: thihathit commentedokay, thanks. ^^
Comment #12
recrit CreditAttribution: recrit at Phase2 commentedI 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.
Comment #13
gadaniels72 CreditAttribution: gadaniels72 as a volunteer commentedTested 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.
Comment #14
jeroen.b CreditAttribution: jeroen.b at .VDMi/ commentedThanks @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).
Comment #15
recrit CreditAttribution: recrit commented@jeroen.b np ... fixed code first, then I'll add tests.
Main changes from patch #12:
Comment #16
drup16 CreditAttribution: drup16 commentedI 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
Comment #17
xamount CreditAttribution: xamount as a volunteer commentedI 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
Comment #18
xamount CreditAttribution: xamount as a volunteer commentedActually 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:
I attached the updated patch which works for me and the interdiff.
Comment #21
xamount CreditAttribution: xamount as a volunteer commentedTidying up patch as my previous patch filed failed testing
Comment #23
das-peter CreditAttribution: das-peter at Cando commentedJust tried to apply patch in #21 but failed.
And I came across this:
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:
empty($instance)
,Let's see of the bot can apply now.
Comment #24
akerbel.dev CreditAttribution: akerbel.dev commentedI put #23 patch into my project and it works - an editor can`t create pages without paragraphs anymore.
Comment #25
sharkbaitdc CreditAttribution: sharkbaitdc commentedI 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?
Comment #26
lily.yan CreditAttribution: lily.yan commentedAfter 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
Comment #27
unstatu CreditAttribution: unstatu commentedSame here. #23 worked perfectly. Since we are 4 people with good results, I'll set the issue as RTBC
Comment #28
capysara CreditAttribution: capysara at Bounteous commentedI'm finding this same issue on D8.
Comment #30
jstollerPatch committed to dev.
Comment #32
delacosta456 CreditAttribution: delacosta456 commentedhi
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