Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony3 create form without entity in type file

I have created a form in my controller like this :

$data = array ();
$formBuilder = $this->createFormBuilder ( $data );
$formBuilder->add( 'Field1', NumberType::class, array(
                    'constraints' => array(
                            new NotBlank(),
                    ),
            ) )
            ->add ( 'Field2', NumberType::class, array(
                    'constraints' => array(
                            new NotBlank(),
                    )
...);
$form = $formBuilder->getForm ();

I am trying to put my form creation in a Type file. I did this like this but the form is not created and I can't display form fields in my view.I don't understand why.

#in ControlController
$data = array ();
$formBuilder= $this->createFormBuilder(ControlType::class, $data);
$form = $formBuilder->getForm ();

#in ControlType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add( 'Field1', NumberType::class, array(
                    'constraints' => array(
                            new NotBlank(),
                    ),
            ) )
            ->add ( 'Field2', NumberType::class, array(
                    'constraints' => array(
                            new NotBlank(),
                    )....;
}

Edit 1 : I have tried all things you tell me, but it still doesn't work. My code looks like this now :

#in ControlController
$data = ['Field1' => null, 'Field2' => null];
$formBuilder= $this->createFormBuilder(ControlType::class, $data);
$form = $formBuilder->getForm ();
return $this->render ( 'MeTestBundle:Control:index.html.twig', array (
            'form' => $form->createView () 
    ) );

#in ControlType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add( 'Field1', NumberType::class, array(
                            'mapped' => false,
    ) )
    ->add ( 'Field2', NumberType::class, array(
            'constraints' => array(
                    new NotBlank(),
            ),
            'mapped' => false
    ))
    ->add ( 'save', SubmitType::class, array (
            'label' => 'Control'
    ));
}

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
            'data_class' => Control::class,
    ));
}

But now the error I have is:

The options "Field1", "Field2" do not exist. Defined options are: "action", 
"allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", 
"compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", 
"csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", 
"empty_data", "error_bubbling", "error_mapping", "extra_fields_message", 
"inherit_data", "invalid_message", "invalid_message_parameters", "label", 
"label_attr", "label_format", "mapped", "method", "post_max_size_message", 
"property_path", "required", "translation_domain", "trim", 
"upload_max_size_message", "validation_groups".
like image 608
Etienne Avatar asked Mar 28 '17 14:03

Etienne


1 Answers

Your Controller Code to create the Form seems to be wrong.

The Signature for createFormBuilder in Controller is defined like:

public function createFormBuilder($data = null, array $options = array())

What you want should be:

$data = ['Field1' => null, 'Field2' => null];
$form = $this->createForm(ControlType::class, $data);
return $this->render ( 'MeTestBundle:Control:index.html.twig', array (
        'form' => $form->createView () 
));

Edit: Also you should not set a data_class if you're not gonna use a data object. Just leave configureOptions empty in this case. If you want to use an Entity you should pass an instance of it as $data instead of a simple array.

like image 106
Joe Avatar answered Oct 15 '22 13:10

Joe