I am trying to test a form in symfony 2.3 that has a select input... along with a file upload (enctype multipart/form-data)
The select input is as follows...
- It is a required field.
- Has 3 options [1, 2, 3]
with the DomCrawler i select the form
$form = $crawler->selectButton->('Update')->form()
then try to set the value of the select with
$form['select'] = null
or
$form['select'] = ssjksjkajsdfj
There is an internal validation system in the DomCrawler that returns the following error if i do that.
InvalidArgumentException: Input "select" cannot take "ssjksjkajsdfj" as a value (possible values: 1, 2, 3).
In symfony 2.4 and up there is a magic method in DomCrawler/Form called disableValidation()
and that works very well. Unfotunately because of some dependencies requiring 2.3 I cannot upgrade
i also tried to use the $client->Request()
method directly
$post_data = '------WebKitFormBoundaryi7fAxO2jrDFTmxef
Content-Disposition: form-data; name="select"
ssjksjkajsdfj
------WebKitFormBoundaryi7fAxO2jrDFTmxef--';
$client->request(
'POST',
$form->getUri(),
array(),
array(),
array(
'CONTENT_TYPE' => 'multipart/form-data; boundary=----WebKitFormBoundaryi7fAxO2jrDFTmxef',
),
$post_data
);
But symfony does not seem to know/care about the form and just returns a regular form without any of the error messages, the form handler doesn't validate the form for some reason.
Here's is the updateAction in the controller
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CoyoteAdBundle:Ad')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Ad entity.');
}
$old_entity = $entity;
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$this->archiveImage($old_entity);
$entity->upload();
$em->flush();
$this->addSuccessFlash('Ad has been updated successfully!');
return $this->redirect($this->generateUrl('ad_show', array('id' => $id)));
}
return $this->render('CoyoteAdBundle:Ad:edit.html.twig', array(
'entity' => $entity,
'form' => $editForm->createView(),
));
}
Here i found an exact question... Select impossible value in select inputs with Symfony DomCrawler
but the answer is not what i am looking for. I want to test and make sure that the server will return a 200 along with a message letting the user know what they are tying to do is not possible.
PS: I can achieve desired results with Postman in chrome.