Closed (fixed)
Project:
Paragraphs
Version:
7.x-1.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
27 Aug 2015 at 11:11 UTC
Updated:
18 Jan 2020 at 06:06 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
vaccinemedia commentedChanging the title of the issue to something which makes more sense
Comment #3
vaccinemedia 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 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
rjarteta 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
rjarteta commentedComment #7
danepowell commentedPatch in #6 throws a notice because $value_count can be undefined. This fixes it.
Comment #8
rjarteta commentedAdding the use case of nested Paragraphs
Comment #9
thihathit commentedPatch works but needs to update module.
Still happening in 7.x-1.0-rc4
Comment #10
jeroen.b 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 commentedokay, thanks. ^^
Comment #12
recrit 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 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 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 commented@jeroen.b np ... fixed code first, then I'll add tests.
Main changes from patch #12:
Comment #16
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
xamountI 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
xamountActually 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
xamountTidying up patch as my previous patch filed failed testing
Comment #23
das-peter 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 commentedI put #23 patch into my project and it works - an editor can`t create pages without paragraphs anymore.
Comment #25
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 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 commentedSame here. #23 worked perfectly. Since we are 4 people with good results, I'll set the issue as RTBC
Comment #28
capysara commentedI'm finding this same issue on D8.
Comment #30
jstollerPatch committed to dev.
Comment #32
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