It would be helpful if you could provide the whole code of your CreditCardType class as well as the complete POST data and the validation errors from your form.
Anyway this are the things I found to be the reason for form submit failures:
- Submit incomplete data: if you have a field on your CerditCardType form that is absent on your POST request, make sure it has the option 'required' set to false.
- Form name: make sure all your fields are wrapped by a property named after your form name (the name you provide on your CreditCardType::getName() method). In your case I assume that is "credit_card".
- Only post the fields defined in your CreditCardType class. If you post a parameter which doesn't match any field of your form you will get a validation error like "This form shouldn't have any extra fields" or something like that.
Anyway if you provide more information I'd be glad to help more.
UPDATE:
Ok, I think your problem is you are posting your data as a JSON string and the handleRequest()
method is not getting any data because Symfony doesn't know it should be decoded from the request's body (it expects the data to be sent either as $_GET or $_POST parameters). So you have to do it on your own.
Instead of using the handleRequest()
method you have to use the submit()
method with the data you obtained by decoding the request content ($request->getContent()
):
$request = $this->getRequest();
$cc = new CreditCard();
$form = $this->createForm(new CreditCardType(), $cc);
// Decode de JSON input
$data = json_decode($request->getContent(), true);
// Post the data to the form
$form->submit($data);
if ($form->isValid()) {
//...
}
Hope it helps now!
BTW if your are working on an RESTful implementation you should really consider using the FOSRestBundle bundle. It would handle for you all the formats conversion logic as well as routing, entities de/serialization, etc.