My app allows pieces of content to be saved to the database whilst status = draft
. The form is made up of some fields that are required (e.g title) and then a bunch of other fields in a sub form that saves into a JSON array on the entity, alongside title.
I have validation constraints on the sub form fields, but don't want to enforce them if status = draft
. In addition, I want to run the validation rules on form load so that the user knows what fields are required when the record moves to published, as this displays form errors but you can still save the form.
I've partly achieved this by, in my edit action, cloning the form and faking a submit on it. This generates the form errors. I can then render the cloned version with errors, but process and save the non-cloned version (to which I run $form->clearErrors(true) to effectively ignore validation when the status is draft)
It works but I wondered if there's a better way. I tried with FormEvents but couldn't achieve the validation on load.