symfony3 - Is it possible to use symfony without FormBuilder?
天蓬老师2017-05-16 16:43:53
0
4
447
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?
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
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!
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.
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:
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
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!
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.
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:
Your Entity:
Map your FORM to entities:
Certify an Entity just like using Form:
Reference: http://symfony.com/doc/current/components/serializer.html#deserializing-an-object