FOSRestBundle icon indicating copy to clipboard operation
FOSRestBundle copied to clipboard

Form fields to JSON

Open Oviglo opened this issue 7 years ago • 3 comments

Hi, I want to send form fields informations (attributes etc.) allow to generate this form with vue.js When I send form to the view I have only the fields names: {"username":[],"email":[],"actions":{"save":[],"cancel":[]}}

I would like: {"username":{"type":"text", "label": "Username", "attr" : {"maxlength": 100}},...

Can you tell me how ? Thanks

Oviglo avatar Feb 12 '18 20:02 Oviglo

I've created a json schema handler that use this bundle: https://github.com/Limenius/LiformBundle Responses are like:

{
    "title": "form",
    "type": "object",
    "properties": {
        "birthdate": {
            "type": "string",
            "title": "birthdate",
            "widget": "date",
            "propertyOrder": 1
        },
        "email": {
            "type": "string",
            "title": "email",
            "widget": "email",
            "format": "email",
            "propertyOrder": 2
        },
        "firstname": {
            "type": "string",
            "title": "firstname",
            "propertyOrder": 3
        },
        "lastname": {
            "type": "string",
            "title": "Last name",
            "propertyOrder": 4
        }
    },
    "required": [
        "birthdate",
        "email",
        "firstname",
        "lastname"
    ]
}

These are json schema, so you can also use it to validate data client side before send them. See this post for details https://blog.apisyouwonthate.com/the-many-amazing-uses-of-json-schema-client-side-validation-c78a11fbde45

hpatoio avatar Mar 22 '18 08:03 hpatoio

Thank you, I installed this bundle but what is the best pratice to use this automaticaly with FOSRest ? I need to override JsonViewHandler ?

Oviglo avatar Mar 22 '18 09:03 Oviglo

I need to override JsonViewHandler ?

That's an option. Otherwise you can register a ViewResponseListener and handle it there

hpatoio avatar Mar 22 '18 10:03 hpatoio