简体   繁体   中英

error in creating a Form without the FormBuilder symfony2

I'm trying to create a form using the following code:

protected function getSearchForm(){
    return $this->getFormFactory()->create('number', null, array('label' => 'code',
        'required' => true,
    'constraints' => new NotBlank(),
        'compound' => true,
    ))
        ->add('submit', 'submit');
}

But I get this error, which originates from ->add('submit', 'submit'); :

Expected argument of type "object, array or empty", "string" given

However I can create the same form using the following syntax (technically it's not the same, it has a root form of type form added.

protected function getSearchForm(){
    return $this->getFormFactory()->createBuilder()->add('code','number', array('label' => 'code',
        'required' => true,
    'constraints' => new NotBlank(),
    ))
        ->add('submit', 'submit')
        ->getForm();
}

What is the Problem with the first approach?

In Symfony, forms are represented by objects and these objects are built by using a form factory. Building a form factory is simple: This factory can already be used to create basic forms, but it is lacking support for very important features:

  1. Request Handling: Support for request handling and file uploads;
  2. CSRF Protection: Support for protection against Cross-Site-Request-Forgery (CSRF) attacks;
  3. Templating: Integration with a templating layer that allows you to reuse HTML fragments when rendering a form;
  4. Translation: Support for translating error messages, field labels and other strings; Validation: Integration with a validation library to generate error messages for submitted data.

The Symfony Form component relies on other libraries to solve these problems. Most of the time you will use Twig and the Symfony HttpFoundation, Translation and Validator components, but you can replace any of these with a different library of your choice.

If you're using the Symfony framework, then the form factory is available automatically as a service called form.factory. Also, the default base controller class has a createFormBuilder() method, which is a shortcut to fetch the form factory and call createBuilder on it.

Now that the form has been created, the next step is to render it. This is done by passing a special form "view" object to your template (notice the $form->createView() in the controller above) and using a set of form helper functions:

Read more at : http://symfony.com/doc/current/components/form/introduction.html

Conclusion : You must call createBuilder() to fetch formFactory and you must call createView() to render it to template.

I will make your code easy for you to understand :

protected function getSearchForm(){
    $form = $this->getFormFactory()->create('number', null, array('label' => 'code',
        'required' => true,
    'constraints' => new NotBlank(),
        'compound' => true,
    ));

    $form->add('submit', 'submit');
    return $form;
}

As you can see in your readable example above, you are adding a field into formFactory before fetched. And you should fetch it first before can adding a field.

protected function getSearchForm(){
    $form = $this->getFormFactory()->createBuilder()->add('code','number', array('label' => 'code',
        'required' => true,
    'constraints' => new NotBlank(),
    ));

    $form->add('submit', 'submit')
        ->getForm();

    return $form;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM