bsm
bsm

Reputation: 81

sending a value to an input using Symfony

I am trying to use javascript inside Symfony and then I got a problem. The problem is located at my file twig.html. At the begining I had this code:

  {{ form_widget(form.name, {'attr': {'class': 'form-control'} }) }}

Then I change it to this in order to use dynamic validation(that was the same code generated in browser, I just add onkeyup action)

  <input type="text" id="person_name"
  onkeyup="myFunction()" name="person[name]" required="required"
   class="form-control" />

Then I was happy because my validation rules work, but I discovered that when I want to update to form. The field name is empty (but in the server it's good). So I would like to get this field. In my function updateAction I dump variable person before handling the form and then name was containing the good element. So the problem is there is a difference between form_widget and the good I just did. I would like to do a thing like this to get my field name:

   <input type="text" id="person_name"
  onkeyup="myFunction()" name="person[name]" required="required"
   class="form-control" value="if(form.name is not empty) form.name"/>

Thank you.

Upvotes: 0

Views: 138

Answers (1)

user8400509
user8400509

Reputation:

Are you passing the entity or document to your createForm function? Something like that should work:

controller

$em = $this->getDoctrine()->getManager();
$your_entity = $em->getRepository(**your_entity_path**)->findOneBy([**parameters**]);
$edit = 1;
if(!isset($your_entity)) {
    $your_entity = new Your_entity();
    $edit = 0;
}
$form = $this->createForm(**your_entity_type_path**, $your_entity);
$editForm->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    if(!$edit)
        $em->persist($your_entity);
    $em->flush();

    //other code
}

I hope this is not too messy

Upvotes: 1

Related Questions