tl;dr:
- I need a custom validation constraint to run on an unmapped form field
- I need the ID of the object the form is manipulating to eliminate it from consideration doing my validation constraint
- Attaching the validation to the form itself or the unmapped field doesn't give me enough context to run my validation query
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?