- Issue created by @krystalcode
- Status changed to Needs review
2 days ago 10:27am 11 August 2025 - First commit to issue fork.
- Merge request !151Issue #3525918 by krystalcode: Form validation should return the entity β (Merged) created by alexpott
- π¬π§United Kingdom alexpott πͺπΊπ
I've made this an MR to make the maintainers live easier. I've reviewed the code and I agree with @krystalcode's assessment - see \Drupal\Core\Entity\ContentEntityFormInterface::validateForm which documents:
/** ... * @return \Drupal\Core\Entity\ContentEntityInterface * The built entity. */ public function validateForm(array &$form, FormStateInterface $form_state);
- First commit to issue fork.
- π¨πSwitzerland berdir Switzerland
See π Remove return value from EntityFormInterface::save Needs work . Those core issues are mostly about submit and save, but it's pretty much the same for validate. It's all pretty weird, we override a method that exists on the parent FormInterface and change how it behaves. Unlike submit/save, validate doesn't set the entity, so you can't access it otherwise, it's a hack for that. It would never have been possible if return types would have existed when we built Drupal 8.
There are at least a dozen implementations in core that aren't doing this either.
And it's related to an even older and trickier issue: π In submit use the entity built during the validation instead of building it again - for performance and consistency ensuring we are saving the entity that has been validated Needs work
I'd love to actually improve things at the source instead of implementing somewhat bogus changes just to make phpstan happy. But I'll merge this for now so we can get pipelines greener, once drupal.org actually lets me do that.
-
berdir β
committed 411b02f3 on 8.x-1.x authored by
alexpott β
Issue #3525918 by alexpott, krystalcode: Form validation should return...
-
berdir β
committed 411b02f3 on 8.x-1.x authored by
alexpott β
- π΅πͺPeru krystalcode
I'd love to actually improve things at the source instead of implementing somewhat bogus changes just to make phpstan happy
I agree about improving this at source. I did not come across this change just from PHPStan. I wanted to extend form validation by extending the class rather than via a hook, and I wanted to have access to the built entity with the attached values. I finally ended up writing a validation constraint so I didn't use it, but I still think it's proper for the form to behave as expected by its parent.