symfony隐藏字段,但对它们进行验证

I have a strange issue with one of my entities.

my Vehicle entity has the following db table fields:

  • orders (PK)
  • licenseplate (PK)
  • make
  • model
  • variant

On the form the only field visible is the licenseplate.

After the field there is a button where the visitor fetches the make/model/variant data from a ajax call to a AjaxController that adds the data to the db. (there is a $session 'orderId' that sets the 'orders' field).

I need to be absolutely certain that the visitor has fetched the make/model/variant and that the visitor cannot change the make/model/variant data.

How can I check on form submit that the vehicle table is filled out? Can I do a Doctrine check and set the form to 'invalid'?

Thank you for your time.

Regards, Lars Hansen

One approach would be:

  1. Remove make, model and variant from your form. If you don't want the user potentially changing them and you're saving them elsewhere, there's no point having them.
  2. Add a hidden, non-mapped field, something like 'dataFetched' to the form.
  3. When successfully fetching/saving the car data via ajax, set this field checked/true
  4. Via javascript, don't allow form submission until the dataFetched field is checked.
  5. In a PRE_SUBMIT listener, check that dataFetched is true and if not, invalidate the form (because a JS check is not sufficient on its own). See CsrfValidationListener in the symfony standard package for a listener example. (I'm using 2.3 but I assume it's still there in newer versions).
  6. Optionally If you wanted to keep make/model/variant in the form. In the listener you could also check that make/model/variant corresponds to what you would expect it to (in case the user changes it) & invalidate the form at that point.