Creating complex forms
ngx-form-object
exposes two methods for modifying form creation. Both can be used for creating complex relationship structures.
create{propertyName}FormObject
should be used to define a FormObject
by which FormStore
or ExtendedFormArray
filled with FormStore
instances will be created.
build{propertyName}
should be used for building other form structures, for example ExtendedFormControl
instead of ExtendedFormArray
for HasMany
relationship.
#
Creating complex relationship structuresUse create{propertyName}FormObject
for creating nested forms. For example, two related models could be defined as follows:
BelongsTo
relationship#
Example 1: Create complex form structure for By default, userForm.get('address')
will be created as ExtendedFormControl
with Address
model set as a value.
In order to have a nested form and to be able to edit street
property, userForm.get('address')
should be a FormGroup
(or FormStore
) containing ExtendedFromControl
for street
form field.
To achive that, a corresponding create method has to be implemented in UserFormObject
. This method must have a name formatted like create{propertyName}FormObject
and return a FormObject
instance. It receives a model and form object options as its arguments.
This results in userForm.get('address')
being a FormStore
created out of the AddressFormObject
. The created form store contains street
property as ExtendedFormControl
.
HasMany
relationship#
Example 2: Create complex form structure for A similar method can be defined for HasMany
relationships. Example:
Create method is then implemented in UserFormObject
:
For each Car
model, FormStore
will be created with CarFormObjects
returned by this method. This will result in userForm.get('cars')
being an ExtendedFormArray
populated with this FormStores
.
note
create<FieldName>FormObject
methods don't have to return specific FormObjects
(e.g. CarFormObject
). They can return the more generic FormObject
if that is the level of control you need.
#
Creating custom relationship formsUse build{propertyName}
for creating custom relationship forms. This metod should return an ExtendedFormControl
, ExtendedFormArray
or a FormStore
instance. It receives property value as its argument.
For example, use buildCars
to create cars form field:
This will result in userForm.get('cars')
being an ExtendedFormArray
populated with forms created in the service.
note
Depending on a use case, car
forms may be FormGroup
, FormStore<Car>
or even a simple FormControl
. For creation of FormArray
containing FormStore
instances rather use create method override.
The build
method is also useful for defining type of a form field. For example, create ExtendedFormControl
instead of default ExtendedFromArray
for HasMany
relationships.
This will result in userForm.get('cars')
being an ExtendedFormControl
with value set to array of Car
models.