angular-schema-form icon indicating copy to clipboard operation
angular-schema-form copied to clipboard

postProcess Examples

Open dkengaroo opened this issue 12 years ago • 5 comments

Hiya,

I was wondering if you could provide me some more examples with the postProcess function. For example: I used "*" in the form. However, now I want to hide some properties. schemaFormProvider.postProcess(function(form){ //what do I do with form here return form; })

Other than reverse() what are some 1 line examples that I could do in the postProcess?

Thanks!

dkengaroo avatar Aug 21 '14 22:08 dkengaroo

Hi @dkengaroo,

Schema form basically works like this, the schema is parsed and a "default" form definition is created, i.e. a list with objects for each field. Then it's merged with the user supplied form definition, in the case of ["*"] this means it takes the default form definition as it is.

The merged form definition is what is used to loop over and render the form, and before it does it runs the postProcess function on it. So basically you can alter the entire form with it, add fields, remove fields, change a fields type, translate validation messages etc. We use it to change some things we can't change in the backend that servers the schema.

Does that make it any clearer?

davidlgj avatar Aug 25 '14 07:08 davidlgj

Hey,

Thanks! I understand how it works now. So to do things like add or remove, should I push a new object into form? I think I'm a bit confused with syntax.

dkengaroo avatar Aug 25 '14 14:08 dkengaroo

This might come a bit late, but what is it you're trying to accomplish? Most tasks of adding and removing can for instance be done with the conditional form type, which is much more easier.

If you use case is that you'd like just the default form + buttons at the bottom then use a form definition that does that , like

[
 "*",
 { "type": "submit", "title": "booya"}
]

What I mean is that it's a big chance postProcess might not be the best tool for the job.

davidlgj avatar Sep 08 '14 06:09 davidlgj

Hiya,

I think the best way would be to use an example. I have a json schema:

{
"type": "object",
"title": "Comment",
"properties": {
  "name":  {
    "title": "Name",
    "type": "string"
  },
  "email":  {
    "title": "Email",
    "type": "string",
    "pattern": "^\\S+@\\S+$",
    "description": "Email will be used for evil."
  },
  "comment": {
    "title": "Comment",
    "type": "string",
    "maxLength": 20,
    "validationMessage": "Don't be greedy!"
  }
},
"required": ["name","email"]
}

and in my form I do the following: [ "*", { "type": "submit", "title": "yay"} ]

Could the postprocess function edit the form so that it does not include "email"? (for my personal use case, I will have quite a bit of properties in the schema. It would be easier to omit some in the form, rather than to only include the ones that I want rendered).

dkengaroo avatar Sep 08 '14 20:09 dkengaroo

Yes, just traverse the form and remove that specific object, whatever postProcess returns is used.

Remember though that it could be a tree structure, tabs, fieldsets, arrays and sections have "tabs" and "items" attributes that themselves are lists of form definitions.

I feel the problem you have, and I've been toying with an idea on how to do "overrides", but for now only defining an entire form definition or using postProcess are your only options.

davidlgj avatar Sep 11 '14 05:09 davidlgj