For forms, can I use the front-end designed page for nesting? Or can I only use FormBuilder to create forms? How to verify and process the data posted from a page written by myself?
漂亮男人2017-05-16 16:45:53
I feel it’s better to use FormBuilder to create forms
There is a token to prevent CSRF attacks
It is more convenient to obtain data by combining objects
Validation is more convenient
Maybe when you first get in touch with it, you may feel that FormBuilder is a bit cumbersome, but after you get familiar with it, it seems to be quite easy to use
怪我咯2017-05-16 16:45:53
It’s best to use what he provides. In fact, no matter what kind of layout you want, you can customize it, you can modify the theme of the form, and you can customize some fields. If you make it yourself, verify it yourself!
PHP中文网2017-05-16 16:45:53
To answer the first two questions first, highly complex forms can of course be made using pages designed on the front end. FormBuilder is not necessarily necessary.
The third question, @xxfaxy said that you need to verify it yourself. In my opinion, Symfony has provided the Validation component. If you use Validation, I don’t think it is completely self-verification. Refer to the Symfony official website, scroll to the end of the page, and look at the section Validating Values and Arrays.
I think that although the framework provides a lot of functions, those functions are universal after all. In some special cases, don’t be limited by the framework.
迷茫2017-05-16 16:45:53
Absolutely ok!
In fact, the core of your question is How to map the post data (Array type) to the entity (Entity type) .
Form is an abstraction of Entity and Request objects. That is to say, after the username in your Entity is abstracted by the Form component, the output is similar to <input name="entityName[property]" /> This format, where Property is the property (field name) in your entity. During handleRequest, the value submitted by the front end will be automatically mapped to the property property of the entity. You can understand that Form is to make your Entity interactive .
So what if you don’t use the Form component?
Symfony also provides the Serializer component that can also map Array to Entity:
Your front-end page:
<form>
<input type="text" name="username" />
<input type="text" name="password" />
<button type="submit">Submit</button>
</form>
Your Entity:
<?php
namespace AppBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class User
{
/**
* @Assert\NotBlank
*/
protected $username;
/**
* @Assert\NotBlank
*/
protected $password;
public funciton setUsername($username)
{
$this->username = $username;
return $this;
}
public function getUsername()
{
return $this->username;
}
// ...
}
Map your FORM to entities:
/**
* $params 就是:
* array(
* 'username' => '狗剩',
* 'password' => '123456'
* )
*/
$params = $request->request->all();
$entity = $this->get('serializer')
->denormalize($params , 'AppBundle\Entity\User');
Certify an Entity just like using Form:
$errors = $this->get('validator')
->validate($entity);
Reference: http://symfony.com/doc/current/components/serializer.html#deserializing-an-object