Usage
The HTML and JavaScript tabs build the identical Form Builder setup shown above,
declaratively via attributes or imperatively via the JS API. A FieldRole
marks a field for special handling elsewhere in your app once the definition is saved. The
Price field above already has the Price Field role assigned, restricted to NumericBox
fields via inputType, so wherever this saved definition gets processed later,
that field can be picked out by its role rather than by name, regardless of what the
person building the form happened to call it.
<cui-form id="form"
ui-display-mode="2"
ui-fixed-layout="false"
ui-field-roles='[
{ "id": "Price", "label": "Price Field", "inputType": 6 }
]'
ui-field-sets='[
{ "type": "FieldSet", "label": "Product", "fields": [
{ "type": "Field", "name": "productName", "label": "Product Name" },
{ "type": "Field", "name": "price", "label": "Price", "inputType": 6, "role": "Price" },
{ "type": "Field", "name": "inStock", "label": "In Stock", "inputType": 2 }
] }
]'></cui-form>
const form = $UI.createComponent(componyx.UI.Form, {
id: 'form',
displayMode: componyx.UI.Form.DisplayModeOption.BUILD,
fixedLayout: false,
fieldRoles: [
new componyx.UI.Form.FieldRole({ id: 'Price', label: 'Price Field', inputType: componyx.UI.Form.InputTypeOption.NUMERICBOX })
],
fieldSets: [
new componyx.UI.Form.FieldSet({
label: 'Product',
fields: [
new componyx.UI.Form.Field({ name: 'productName', label: 'Product Name' }),
new componyx.UI.Form.Field({
name: 'price',
label: 'Price',
inputType: componyx.UI.Form.InputTypeOption.NUMERICBOX,
role: 'Price'
}),
new componyx.UI.Form.Field({
name: 'inStock',
label: 'In Stock',
inputType: componyx.UI.Form.InputTypeOption.CHECKBOX
})
]
})
],
containerElement: document.body
});
form.show();
A built form binds to the Form's own values object instead of a model of your own, so there's
no model with rules to validate against on the server. For .NET backends, Componyx.UI includes
FormValidator, which checks the submitted values against the saved definition, using
the same required and validation settings as the client, repeatable field sets included:
FormValidationResult result = FormValidator.Validate(definitionJson, valuesJson);
Load the definition from your own storage, never from the request, so a user can't remove the validation rules.
Fields of the Editor type submit HTML. As with any HTML that comes from users sanitize it server-side
before storing it. This way it's safe wherever it ends up, like in emails, PDFs or other applications.
For .NET HtmlSanitizer is a common choice.