5

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".
4
  • $data = ['Field1' => null, 'Field2' => null]; And make sure you are in dev mode so you see the error messages. And of course ensure $form is being passed to your twig template and the template is working as wanted. Commented Mar 28, 2017 at 14:51
  • The error message I have is : Neither the property "Field1" nor one of the methods "Field1()", "getField1()"/"isField1()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" Commented Mar 28, 2017 at 15:00
  • Hence the $data = ... suggestion. Commented Mar 28, 2017 at 15:27
  • try this symfony.com/doc/current/forms.html#building-the-form symfony.com/doc/current/form/without_class.html check this method too -- 'form' => $form->createView(), Commented Mar 29, 2017 at 3:44

1 Answer 1

5

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.

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.