"Format Symfony Form Fill" form text box with data from database

How can I pre-populate a textbox in symfony with data from a database. I have a field in my host table named hostFee and when I create a form I want this data to pre-populate this text field.

I am creating a form for new BookingSpecsType () ...

Here is my form builder.

$builder->add('hostFee', 'text', array(
        'required'=>false,
        'error_bubbling'=>true,
        'label'=>'Do you charge a hosting fee?',
        'data' => '??????? (How do I fill this text field dynamically with the Host table hostFee column data) ?????',
        'attr'=>array(
            'placeholder'=>'If yes, enter dollar amount $0.00',
            'class'=>'form-control'
        )
    ));

      

Thank.

+3


source to share


2 answers


the documentation contains many examples.

When you use a $this->createForm

Controller in your action, the second parameter allows you to remove the form using the object.



For example:

public function editAction()
{
        $user = $this->getDoctrine()->getRepository('User')->find(1); // YOUR OBJECT RETRIEVED FROM THE DB FOR EXAMPLE
        $form = $this->createForm(new EditType(), $user, array(
            'action' => $this->generateUrl('account_edit'),
        ));

        return $this->render(
            'AcmeAccountBundle:Account:edit.html.twig',
            array('form' => $form->createView())
        );
}

      

+1


source


You don't need to manually define the data. If you just run the form from a hydrated entity, then all data is initialized in all fields.



0


source







All Articles