formly-js / angular-formly

JavaScript powered forms for AngularJS
http://docs.angular-formly.com
MIT License
2.23k stars 406 forks source link

Is it possible in the fields JSON to add predefined values for every every fields of the form? #757

Open falinsin opened 6 years ago

falinsin commented 6 years ago

Two simple questions

  1. When I load a form fields form a JSON, is it better practice load the predefined fields values populating the JSON model or There are ways to fill them from the JSON of fields, like some property for example like predefined-value or something like that?

  2. Is there a specific field in the JSON of fields where I can receive extra data from the server? For example, data such as the modification date and the name of the person who made it. In general they are other data that do not have to do directly with the creation of the form. Currently I add a new { .... } in the lis of field and it seem it works.

Thanks in advance

gmanriqueUy commented 6 years ago

Hi.

  1. There's a property called defaultValue that is maybe what you're looking for

  2. Can you elaborate more on this? Not understanding what's your use case.

falinsin commented 6 years ago

Thank you very much for your help.

1) your answer is very helpful and made my day

2) For every field I need to collect several extra data i can use in the server side or server can send me in the listfield JSON. For example

{
                    key: "textarea",
                    type: "textarea",
                    className: "col-xs-12",
                    templateOptions: {
                        name_type:"Textarea field",
                        code_type:"textarea",
                        label: "",
                        placeholder: "",
                        required: false
                    }
                }

For example you can see properties name_type and code_type are extra data for my own use.

I insert all these new data inside templateOptions propertie and it is working.

It does not intercept with the generation of the form because it formly ignores them.

My question is :

Is there a specific or more appropriate variable in formly to insert this type of custom variables or is it correct as I am doing? I ask it just in case

Than you very much

gmanriqueUy commented 6 years ago

Ohh, I get it now.

According to docs, you can use either templateOptions (as you're using right now) or data.

From docs:

data (object): This is reserved for the developer. You have our guarantee to be able to use this and not worry about future versions of formly overriding your usage and preventing you from upgrading :-)

Personally, I'd use data because, as you said, it's just extra data is not needed to manipulate the form.

Use templateOptions under your own risk; maybe formly adds a name_type or code_type in future versions and it break your implementation. With data that's not going to happen. :smile:

I hope this helps!