This library contains two sets of code:
- Front end code that consumes JSON 7 Schema and generates a user friendly form that can be used in a web interface
- Back end code containing a custom validator that is used to validate that the values returned match the schema rules.
When using the JSON Schema Form in a project, the front end and backend projects need to install the same version of the JSON Schema Form via node.
See our wiki on how to configure JSON 7 Schema into a visual form.
-
Install Node.js
-
Install project dependencies
npm install
- Run it
cd .\projects\jsf-launcher\
npm run serve
- Open localhost:4200
To run the unit tests locally, run the following command:
npm run test
-
To use the front end functionality of this library, your project must use:
- Angular
- Node.js
-
Install the JSON Schema Form into your project
-
Install icons into your front end Angular project
- Open your project's angular.json file. Find the
"assets"
key. Pick a directory to store these images (ex:./src/assets
). - Copy the directory
./projects/jsf-launcher/src/assets/jsf-images
into your project's assets directory.
- Open your project's angular.json file. Find the
- Import the JSFModule into your project.
@NgModule({
declarations: [],
exports: [],
imports: [
JSFModule
],
providers: []
})
export class ExampleModule { }
- Configure your Angular component to use the JSON Schema Form. Reference the example below as well as a detailed list below of the necessary steps.
- Create a JSFConfig object to control the commonly used confguration items:
- If this is an edit case
- If sections are collapsible
- If there are dividers between sections
- Inject the JSFDataItemService into your constructor and use the
getFormDataItems()
method to transform the JSON 7 schema and corresponding values into an array of FormDataItems. FormDataItems are the data model that the JSF understands and uses to generate the angular forms. - Create a Submit button in your component. Listen to the
disableSubmit
event emitted from the JSON Schema Form and disable your submit button. - Create a ViewChild property in your component to reference your JSFComponent. Use this property to get the submitted form values by calling
this.jsfComponent.getFormValues();
- [Optional] Listen to the
formHeightChange
event emitted from the JSON Schema Form.
- Create a JSFConfig object to control the commonly used confguration items:
@Component({
selector: 'example',
templateUrl: 'example.component.html',
styleUrls: ['./example.component.scss']
})
export class ExampleComponent {
@ViewChild(JSFComponent, { static: false }) jsfComponent: JSFComponent;
config = new JSFConfig(false, false, true);
schemaData: JSFSchemaData;
isSubmitDisabled = true;
constructor(private jsfDataItemService: JSFDataItemService) {
// grab schema and values from some service
this.schemaData = new JSFSchemaData(schema, values);
}
// this event allows you to enable/disable the submit button in the parent container
onDisableSubmit(disableSubmit: boolean): void {
this.isSubmitDisabled = disableSubmit;
}
// this event allows you to modify parent container height to match the height of the form
onFormHeightChange(formHeight: number): void { }
getJSFFormValues(): void {
const jsonData = this.jsfComponent.getFormValues();
}
}
- Import the JSFComponent into your template. See example below.
<jsf-component
[schemaData]="schemaData"
[config]="config"
(disableSubmit)="onDisableSubmit($event)"
(formHeightChange)="onFormHeightChange($event)">
</jsf-component>
-
To use the back end functionality of this library, it is recommended your project use:
-
Install JSON Schema Form Validation
-
You will then have access to the following validation classes:
- RequiredSchemaValueValidationService
- SecuredSchemaValueValidationService
- SchemaHelperService
For the versions available, see the tags on this repository.