In my angular project I need to create some forms dynamically. I did some googling for this and found a module angular-schema-form. It seems this is exactly what I want, but the problem is I do not understand how to keep schema and form. What is the relation between schema and form?
I read the documentation from form types but it is not very helpful to me. I don't want to use this module like HIT and TRY. I first want to understand the concept: what is the relation between schema and form?
To try to address the basic question:
What is the relation between schema and form?
To put things into simple terms, perhaps it is easiest to think of it like this. To create the form, you have three pieces of JSON:
The JSON containing the actual data being presented through your form. This is the model
.
The JSON schema
. This describes the structure of the model
. To give an extremely simple example of the relationship between model
and schema
, imagine your actual data (your model
) describes a person in terms of their sex and age, such as {sex: female, age: 32}
. The role of the schema
is to describe the structure of the model, so in this case it would describe that there should be a sex
property of string type whose values can only be male
or female
, and that there is an age
property, and so on. To be able to present a form that is populated using your model
data, it simply has to have a schema
to construct the form in the first place.
The JSON form
is something that I think of as being optional. You must have it, but it can simply contain "*"
, which tells angular-schema-form
to just build the form automatically based on the schema. In this case, the library will build the form based entirely on the schema and using default settings as described in the documentation. But what you can do, if you want to, is use the form
object to describe how you want your form to be structured.
Here's a very crude and very simple example of the flexibility the form
object gives you:
Taking the person
object example above, if you don't make use of the form
object (i.e. it just contains "*"
) then angular-schema-form
will build you a form based on the schema. It would therefore have input fields for sex
and age
. Those fields may, or may not, be in the format, or order, or have other properties that you want. You may then optionally use the form
object to instruct angular-schema-form
how you want it to construct the form. For instance, you could use the form
to make it so that the form only contains a field for age
but not sex
.
So the model
is your data, and the schema
describes the structure of that data. In simplistic terms, the form
provides a further layer of mapping and configuration to your form, overriding the default form that would be created based on the schema
alone.
Does that help?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With