0

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.

2
  • try with {{ form_widget(form.name, {'attr': {'class': 'form-control', 'onkeyup': 'myFunction()'} }) }} Commented Aug 10, 2017 at 15:40
  • Thank you very much Commented Aug 10, 2017 at 18:23

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.