So this issue is on the intersection between Paragraphs module and Inline Entity Form. Adding it here as the error message is returned due to code belonging to Paragraphs module.
I have a following structure: Node -> Paragraph -> Media entity. Paragraphs entity revision field is using Experimental widget. Media entity is using Inline Entity Form - Simple. Media field has cardinality 1, while Paragraph field - unlimited. Steps to reproduce:
- Create new node (or edit existing one).
- Add new Paragraph
- Upload file into Media field
- Collapse yet-unsaved Paragraph, so you don't see fields
- Submit the form
- The website encountered an unexpected error. Please try again later.
Here's the screencast:
![]()
The error:
LogicException: Form errors cannot be set after form validation has finished. in Drupal\Core\Form\FormState->setErrorByName() (line 1057 of /home/dmv92/www/core/lib/Drupal/Core/Form/FormState.php).
And the code that is responsible for this error was added in #2939718: Remove validation of closed paragraphs, fix validation and display of validation message for collapse (ParagraphsWidget:massageFormValues()):
// Assume that the entity is being saved/previewed, in this case,
// validate even the closed paragraphs. If there are validation errors,
// add them on the parent level. Validation errors do not rebuild the
// form so it's not possible to auto-uncollapse the form at this point.
elseif ($form_state->getLimitValidationErrors() === NULL) {
$violations = $paragraphs_entity->validate();
$violations->filterByFieldAccess();
if (count($violations)) {
foreach ($violations as $violation) {
/** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
$form_state->setError($element[$item['_original_delta']], $violation->getMessage());
}
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | 3013171-13.patch | 1.39 KB | mheip |
| #5 | 3013171-5.patch | 883 bytes | zaporylie |
Comments
Comment #2
zaporylieThe backtrace to confirm why I think the code above is to blame here (I highlighted relevant lines):
Comment #3
miro_dietikerThe massageFormValues is triggered twice, first during validation in validateForm and if there were no errors during doSubmitForm.
It seems your validation provides inconsistent results, thus i would expect that you have some code in place that modifies data on validation?
Tested this without IEF and the validation checks consistent values.
From the description above i can't seem to understand what the relation to IEF is nor what speciflc violation error was triggered.
So can't help much here.
Comment #4
zaporylieThank you Miro for taking the time to reply to this bug report.
Please let me know if this is clear now and what additional information you need in order to reproduce the issue.
I believe the core issue here is errors being set (via
$form_state->setError()) in submit part of the form submission. Once commented out media submission via IEF works like a charm.Comment #5
zaporylieSo, as suggested earlier, I think Paragraph validation shouldn't be triggered once the form was already validated. I think that adding a simple check would solve the major issue here.
Comment #6
azinck commentedPatch works well for us.
Comment #7
dennis cohn commentedI'm using Drupal 8.7.0-alpha1 and the error below is still there after applying #5
LogicException: Form errors cannot be set after form validation has finished. in Drupal\Core\Form\FormState->setErrorByName() (line 1057 of /Users/denniscohn/Sites/flean/web/core/lib/Drupal/Core/Form/FormState.php).Comment #8
vood002 commentedI'm using Drupal 8.7.0-beta2 and tried applying this patch to Paragraphs 1.8 and 1.x-dev and in both cases I continued seeing the same error,
LogicException: Form errors cannot be set after form validation has finished. in Drupal\Core\Form\FormState->setErrorByName() (line 1057 of /app/web/core/lib/Drupal/Core/Form/FormState.php).It's obviously a very simple and concise patch....any other thoughts what might be going on here?
Comment #9
miro_dietikerComment #10
miro_dietikerAlso please help to extend our tests to cover this use case.
Comment #11
zaporylie@Dennis Cohn and @vood002 - could you share:
- which fields are you using to trigger this error - is it IEF and Media?
- which field widget for Paragraphs module is in use - Classic or Experimental?
- backtrace when the error occurs? Is it caused by calling `setError` in `massageFormValues`?
Comment #12
niklanHaving same issue since 8.7.0.
I have paragraph type "image_single" which has entity reference field "field_media_image" . I use "Media Library" widget and this error happens when I click "Added media". For paragraph field I use EXPERIMENTAL widget. There is not nested paragraphs, to be sure. Since it looks like this issue #3003150: Media library causes validation errors when it is used in a required field of a nested form, but patch from that issue is not helps with this problem and patch from #3013171-5: LogicException Form errors cannot be set after form validation has finished also not fix the problem.
UPD With "Paragraphs Classic" field widget media select dialog is opens, but when select value, it "saves" without any values and errors.
My error trace is
Comment #13
mheip commentedI was also getting this issue with Drupal 8.7 and paragraphs 1.8. My LogicException triggered in the if statement above:
Changing this to:
In combo with the patch in #5 resolved it for me.
I have attached my patch.
Comment #14
niklanPatch #13 is solved my problem, where #5 can't.
Comment #15
ainarend commentedI can confirm that the patch #13 fixed the bug of media library not opening when using Paragraphs with referenced media field and experimental widget.
Comment #16
ainarend commentedOh and since there is a patch, updating the issue status.
Comment #17
miro_dietikerA quick hint: While we have seen multiple runs of validations eating time (and like here resulting in errors), we also have seen cases where nested structures skip validation completely because somehow it was partially skipped but on some level marked as validated (maybe also collapsing related). Some operations (for instance the new convert patch) can produce temporary illegal situations within the UI. We need to make sure that items are guaranteed to be validated before saving.
We definitively need to check the quality of our validation tests combined with nesting, collapsing and maybe more of the complex operations.
Comment #18
ainarend commentedTurns out that the patch from #13 somehow brakes JSON:API responses when using included paragraphs fields in the jsonapi request. Didn't have time to debug why.So it did fix the media library not working, but caused a regression for jsonapi.Unrelated bug with config.
And it seems that miro_dietiker also hints that there's more work to be done with this issue.
Comment #19
FNGR commented#13 worked for me too.
Comment #20
br0kenThe root problem here is that the method is called at least twice and after the first execution the form is already validated. Suppressing the validation doesn't seem to be a good fix (but as a quick fix it works perfectly fine).
Comment #21
carma03 commented+1. Confirmed, #13 worked for me on Drupal 8.7.1 and PHP 7.3. Thanks!
Comment #22
dani3lr0se commentedRTBC +1. The patch in #13 worked for me as well. Patch applied cleanly and solved my issue. Using core 8.7.5 and module 8.x-1.8. Thanks!
Comment #23
mbovan commentedI tested #13 with a large number of paragraphs (Paragraphs Performance module in combination with latest Drupal core, latest development branch of Paragraphs and ERR modules) where I get on average ~20% (~0.5s difference) faster save times with the patch compared to results without the patch.
Comment #24
berdirI think the issues in #17 aren't really related to this MR and aren't blocking it.
The bugs are non-trivial to test for, and at least the one in media_library is also being fixed in core, so wouldn't help us much then to test I think.
This does have considerable performance improvements, passes our existing tests, we also tested it with our distribution extensively and there were also several reports from others that it is fixing their problems. RTBC.
Comment #26
miro_dietikerYay then, committed. :-) and attributed all, thx!