Form XHR Model is a class which interacts with the server API holding the data.
Form XHR Model implements Form Model Interface.
Parameters:
Type | Name | Description |
---|---|---|
String | settings.api | Required. API address to interact with |
Validator | settings.validator | Optional. Validator instance. By default there is used a new Validator instance. |
Function | settings.xhr | Optional. XHR interface. By default there is used a built-in xhr function, but you can override it here. |
Fetch values of the specified fields from the server.
It sends GET
request to the URI = settings.api taken in the constructor.
In successful case(response status == 200) returns server response, otherwise throws an error.
Parameters:
Type | Name | Description |
---|---|---|
String [ ] | fields | Optional. Form fields to get values of. By default there is returned values of all the fields. |
Returns: Promise which resolves with Object with form data. It has the following structure: {field1dName: 'value1', ..., fieldNName: 'valueN'}
.
Submit form data(changes).
It sends POST
request with header {'Content-type': 'application/json'}
and body = JSON.stringify(changes)
to the URI = settings.api taken in the constructor.
Validation is supposed to be performed at the server
(but you can also do explicit local validation calling formModel.isValidRecord(record) before form submitting).
If the server response is successful(Status 200) and it has field error
, the response will be thrown.
In case of unexpected error(e.g. server response status !== 200) an Error instance will be thrown.
If the server response is successful(Status 200) and it doesn’t have field error
,
so serverResponse.data
will be resolved.
Parameters:
Type | Name | Description | |
---|---|---|---|
Object | changes | Required. Changes(or new values) in the form data. Expected structure: {field1Name: 'value1', ..., fieldNName: 'valueN'} |
Return fields(Array of string values) that need to be sent additionally to validate fields specified in passed parameters. This method is required for creating group validators(read details here).
Parameters:
Type | Name | Description |
---|---|---|
String [ ] | fields | Required. Array of fields to get their validation-dependant fields |
Validate specified record by means of the validator passed in the constructor, or the default one. An unexpected error(e.g. if there are async validation rules and the Internet doesn’t work, or server responds with status !== 200) will be thrown.
Parameters:
Type | Name | Description |
---|---|---|
Object | record | Required. Record to validate. Expected structure: {field1Name: 'value1', ..., fieldNName: 'valueN'} |
Returns: Promise which resolves with ValidationErrors