tl;dr:
I have an unmapped field on my Person
entity form that I need to run a validation on. I've followed this great article on how to do this, but my use case is slightly different and not entirely covered by the article.
I am making my own Unique Constraint that needs to run a custom query to determine the uniqueness. To run the query, I need access to the field value that was submitted, as well as the original Person
object (so I can get it's ID if it's an update operation). Without also having the that Person
object I won't be able to eliminate it from consideration during the uniqueness query.
If I apply the validator on the PersonType
class like so:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults(array(
'data_class' => 'CS\AcmeBundle\Entity\Person',
'constraints' => array(
new MyUniqueValidator(),
)
))
;
}
Then the validator gets passed the entire Person
object to perform the validation on. This doesn't help me, because the submitted form data is not persisted to the Person
object (it's an unmapped field that I handle after $form->isValid()
is called in the controller).
If I apply the validator to the unmapped field directly instead:
$builder
->add('myUnmappedField', 'text', array(
'mapped' => false,
'constraints' => array(
new MyUniqueValidator(),
)
),
))
Then the object I get passed to the validator is just the standalone form text, and nothing else. I don't have the ID Person
object (if it was an update operation) to perform by uniqueness query.
Hopefully I've explained this properly. Do I have any options to do this sort of validation gracefully?
You say you have unmapped field. Would it help, if you make it mapped to the Person entity? Just make a new property in the Person class with getter and setter methods, but not to the ORM, since you don't want it persisted.
If you do not want to polute your Person class, you can also make another composite class, which will hold your currently unmapped field and a Person object (you will then make it mapped). Ofcourse you will then set data_class to match the new object's namespace.
Both above solutions should work with the upper code you have there. Please let me know it it helped.
Thanks. I went with adding another property on my Person entity and mapping the form field to that. This works well.
Glad I could help;) And also in the future, always remember, a form can only have one underlying object. If you need more than one object or anything like that, make another composite object and it can always contain more objects / properties :)
sure, this works, but this can't be the accepted answer to the question that was asked... the problem isn't fixed by that, it's just by-passed
This is an acceptable work-around, but I agree with @AlexK - this isn't really a solution or answer to the question.