0

I have a entity with some data prefilled, and I want to display them on the form : here state would be ACTIVE and customerId = 1;

// Form file (ben.file.create)
protected function buildForm()
{
    $this->formBuilder->add('name', 'text', ['label'=>'Nom du patient'])
        ->add('customerId', 'integer')
        ->add('state', 'choice', ['choices'=>['ACTIVE'=>'ACTIVE',
            'DONE'=>'DONE', 'CANCELLED'=>'CANCELLED']]);

}


// in the controller
public function index()
{
    $form = $this->createForm("ben-file-create");

    $file = new BenFile();
    $file->setCustomerId(1);
    $file->setState('ACTIVE');

    $form->getForm()->submit($file); // <--- Here the glue problem
    return $this->render('create-file', array());
}

It looks like submit is not the right bind function. I would expect that the form is pre-filled accordingly, and after the POST request, I have an updated BenFile entity.

1
  • Also you can set those values at the BenFile constructor, so the form will be rendered with active state, supposing his state would be always active at first, probably CustomerId is the value of the logged user? Commented Mar 6, 2019 at 10:29

1 Answer 1

1

You can do easily in createForm method :

// in the controller
public function index(Request $request)
{
    $file = new BenFile();
    $file->setCustomerId(1);
    $file->setState('ACTIVE');

    $form = $this->createForm("ben-file-create", $file);

    // handle submit
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // flush entity 
        $fileNew = $form->getData();

        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($fileNew );
        $entityManager->flush();
    }

    return $this->render('create-file', array());
}
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.