MorphToSelect with one type
I'm building multiple forms that all write to the same
subject
morph on the same model. But each form has it's own specific type that needs to be set as the subject
. What I'm looking for is a way to set the morph type by default and hide the select field for type, as it's already known.
Is this possible?13 Replies
use a normal Select on subject_id instead of a MorphToSelect?
and then before you save fill in the subject_type as it's known
when I do that I get an error that the selected option is not correct
what error
A validation error that the subject doesn't exists
what validation rule are you using
none
please send the code
Of the whole form? It's quite a complex one
The field is this:
When I use it as above, it sometimes submits the form, but the relationship is not set in the form and sometimes gives an validation error that the given location does not exists.
What I think that's happening is that it tries to load the related relation, but because of it being an morph, it thinks the relations is with itself somehow? Because, when i remove the third argument from
->relationship()
it shows other models that are of the same type as the one I'm making in the form.replace relationship() with your own options() array
like this?
Yes, this seems to work!
This is the complete code now:
It works great tothether with the hidden input
i wouldnt use a hidden input
you should add this type on the backend before the data is saved
otherwise it is insecure
Good point