Problem/Motivation
An entity reference field with the Complex widget is returning a vague error ("This value should not be null.") without indicating what field or value is incorrect. Sometimes nothing is incorrect, and it's the fact that inline_entity_form isn't adding values to the main entity before it's validated.
In some cases this is happening when all the fields are filled in, such as on clicking the "preview" button when adding a new node.
Steps to reproduce:
1) go to https://simplytest.me/ and select "Inline Entity Form" as the project
2) Once site launches, add an entity reference field on the article content type to reference the page content type
3) Edit the form display to use the Complex Widget widget.

4) Create a test page node (/node/add/page)
5) Go to create an article node (/node/add/article), fill in all the fields (including the reference to the page node you created
6) Click Preview
7) Observe error.
Expected: You should see a preview of your node with the image displaying
Actual: An error message "This value should not be null." and the form reloads. I traced it back to validation on the image field (see stack trace below).
Proposed resolution
I'm not sure of the best approach yet. I have a patch, but it needs review.
Next Steps
- test coverage to demonstrate the error
- needs review
- needs test coverage for the fix
Comments
Comment #2
oknateComment #3
oknateMade some changes after seeing it fail tests.
Comment #4
oknateComment #5
oknateComment #6
darvanenCan you provide steps to reproduce for review?
Comment #7
bojanz commentedFeels hacky to add entity_browser specific code. A better fix is probably needed, along with more instructions for reproducing.
Comment #8
oknateI'm experiencing the vague "This value should not be null." error again.
This time with this set up:
node with field_image (entity reference to media entity of type bundle), using the complex widget. On the node/add/mynodetype form, clicking preview triggers the error, even though the field is filled in.
Comment #9
oknateComment #10
oknateComment #11
oknateNote: As a quick fix / hack for this issue with node preview this is one idea, just prevent validation errors.
Another quick fix / hack is to not make the field required.
Comment #12
oknateHere's a patch that takes care of the vague message in the context of node preview, and fixes the node preview of inline entity form elements, at least with the one context I tested. Will need to see if it breaks anything else.
It also needs test coverage to demonstrate the error and the fix.
Comment #13
oknateComment #14
oknateComment #15
oknateComment #16
oknateComment #17
oknateComment #18
oknateComment #19
oknateComment #20
oknateComment #21
oknateComment #22
oknateComment #23
oknateThe patch needs work I'm seeing this error on another content type:
This line is where the bug is:
foreach ($form_state->get('inline_entity_form') as &$widget_state) {Comment #24
oknateFixing bug in #23
Comment #25
larvymortera commentedThis patch makes sure that IEFs submit callbacks are called when clicking "preview".
Comment #26
bojanz commentedThis is a dangerous change. It means that clicking Preview will save all of the inline entities. So if you click Cancel, or navigate away from the page, your data model will be left in an inconsistent state.
Comment #27
guypaddock commentedIMHO It's difficult to get an accurate node preview without committing the inline entities.
The only solution I can think of that would produce an accurate preview while also preventing persistence of the nested entities would be a database transaction that gets created during validation and rolled back after the preview is rendered, but I doubt there's a clean injection point for something like that and it would likely cause issues for the form cache.
Comment #28
guypaddock commentedI take it back... with the patch from #24 applied:
However, it would be good for there to be a test included for this issue since this is not the first time this error has appeared in this module. See:
Comment #29
albertosilvaI've found a really simple way to make complex widgets work, even with nested entities (inside paragraphs, like my use case). During
InlineEntityFormComplex.php::extractFormValues(), which is called twice for each widget, that function is returning nothing the second time it is executed. Making it to be completely executed when the preview button is clicked seems to fix each and every one of the problems with IEF and previews:It seems to me that this is a too much simple solution to be true, but it simply works, at least for my use case, with nested and non-nested complex widgets.
I would like some feedback from other developers, because maybe this is the solution we where looking for to solve all problems between IEF and previews.
Comment #30
albertosilvaComment #31
geek-merlinComment #32
guypaddock commented@alberto_silva: Won't that lead to the IEF changes getting committed on Preview? With the patch from #29, what happens if you make a change in IEF, Preview, then cancel? My understanding of the prior approach was that we did not want to actually _commit_ or save the IEF unless the real submit button that saved changes was clicked.
Also, your approach hard-codes this to just work for a English-language button named 'Preview', which means that it won't work as a fix for other languages AND it may not work if there are other preview-style buttons in sites that aren't actually titled 'Preview'.
My preference is for the patch in #24 (which now needs a re-roll) because it looks more reliable. Either way, we need a test to cover this issue that ideally confirms that previewing does not save changes and does not display an error.
Comment #33
guypaddock commentedAttached is a re-roll of #24 against 8.x-1.x. Still needs tests.
Comment #34
khaldoon_masud commentedI spent two days trying to figure this out, and below did the trick for me:
$form['actions']['preview']['#limit_validation_errors'] = [];Seems like inline_entity_form doesn't like if any other element is using AJAX on the form.
Comment #35
attheshow commentedPatch #33 is working well for us.
Comment #36
a.sotirov commented@GuyPaddock I just added an additional
t()function, because if the button "op" value for the preview button is translated the code will not be executed.Now with the latest patch it should works on the translated button value.
Comment #37
david.muffley commentedRerolled for rc-12. The previous patch included several unrelated code style changes that were covered by #3135178: Fix Coding Standards .
Comment #38
quadrexdevAttaching a new patch with a changed check in inline_entity_form_node_preview_builder.
Comparing the "op" title didn't work for me cuz there could be a project-specific case with a changed title of the button. That will lead to a failed check
So, added a check by triggered element ID. Not sure this is the best way but it works for any button title (Preview/Live preview/etc).