Forms and validation
Validate values in components, track what the user changed and control the state of a whole form.
On this page
jComponent has no form object. A form is a group of components whose paths share a prefix, such as form.name and form.city. Each component keeps its own state, and global functions read the state of the whole group through a path like form.*.
The state of a field
Every component with a path has these values in its config object:
| Value | CSS class | Meaning |
|---|---|---|
config.invalid | ui-invalid | The validate function rejected the value. |
config.touched | ui-touched | The user changed the field. |
config.modified | ui-modified | The value was changed by the user or with the @change flag. |
config.disabled | ui-disabled | The field is disabled. |
The library adds and removes the CSS classes on the element of the component.
Validating a value
Give the component a validate function. It receives the value and returns true when the value is valid. The library calls it when the component starts and on every change. An <input> with the data-jc-bind attribute is connected to the path of the component:
<script>
COMPONENT('textbox', 'required:false', function(self, config) {
self.validate = function(value) {
return !config.required || !!value;
};
self.make = function() {
self.html('<input type="text" data-jc-bind />');
};
});
var form = { name: '' };
</script>
<ui-component name="textbox" path="form.name" config="required:true"></ui-component>The classes of the element follow the user:
| Moment | Classes |
|---|---|
| The page opens | ui-invalid |
The user types Peter | ui-modified ui-touched |
| The user clears the field | ui-modified ui-touched ui-invalid |
An empty required field is invalid from the start. To avoid showing errors on a fresh form, style only fields the user has touched:
.ui-touched.ui-invalid input { border-color: red; }DEF.validators contains regular expressions for email, phone and url:
self.validate = function(value) {
return DEF.validators.email.test(value || '');
};Checking a form
| Function | Returns true when |
|---|---|
VALID(path) | No component under the path is invalid. |
CAN(path) | No component is invalid and at least one was touched. |
DISABLED(path) | CAN(path) is false. |
CAN() also returns true when there is no component under the path.
Add the flag visible to check only the fields the user can see:
VALID('form.*'); // false, a hidden required field is empty
VALID('form.*', 'visible'); // trueEnabling a save button
Watch the form and update the button. Start with a disabled button, because the components are not ready yet when the page opens:
<ui-component name="textbox" path="form.name" config="required:true"></ui-component>
<ui-component name="textbox" path="form.city"></ui-component>
<button id="save" disabled>Save</button>
<script>
WATCH('form.*', function() {
$('#save').prop('disabled', DISABLED('form.*'));
});
</script>| Moment | VALID | CAN | Button |
|---|---|---|---|
| The page opens | false | false | disabled |
| The user fills in the city only | false | false | disabled |
| The user also fills in the name | true | true | enabled |
Showing errors on submit
CHANGE(path) marks every field as touched and modified, so the invalid ones show their errors. ERRORS(path) returns the components that are touched and invalid:
function save() {
CHANGE('form.*');
if (!VALID('form.*')) {
console.log(ERRORS('form.*').map(com => com.path));
return;
}
console.log(GET('form @reset'));
}With two empty required fields, this prints ['form.name', 'form.city']. Once they are filled in, it prints the model. The @reset flag clears the state of the form while reading it. CHANGE(path, false) removes the touched and modified state.
Reading what changed
MODIFIED(path) returns the paths of the fields that were changed. The @modified flag of GET() returns an object with only those fields:
var form = { name: 'Peter', city: 'Bratislava' };
SET('form.city @change', 'Prague');
MODIFIED('form.*'); // ['form.city']
GET('form @modified'); // { city: 'Prague' }The @change flag tells the library to treat a value from code as if the user had entered it. The other flags work with forms too.
Resetting a form
RESET(path) clears the touched and modified state and validates the fields again. Values stay as they are:
RESET('form.*');
MODIFIED('form.*'); // []Default values
The default attribute of a component holds a JavaScript expression. DEFAULT(path) writes the default values back to the fields:
<ui-component name="textbox" path="filter.search" default="'all'"></ui-component>
<script>
SET('filter.search @change', 'books');
DEFAULT('filter.*');
RESET('filter.*');
// filter.search is 'all' and the field has no state classes
</script>Call RESET() after DEFAULT() so that the CSS classes of the fields are updated.
Forms in a plugin
A plugin is a natural home for a form. exports.modified returns the changed fields and resets the state, and exports.form returns the whole model and resets the state:
<script>
PLUGIN('profile', function(exports) {
exports.set({ name: 'Peter', city: 'Bratislava' });
exports.save = function() {
if (CAN('profile.*'))
console.log(exports.modified);
};
});
</script>
<ui-plugin path="profile">
<ui-component name="textbox" path="?.name" config="required:true"></ui-component>
<ui-component name="textbox" path="?.city"></ui-component>
</ui-plugin>After SET('profile.city @change', 'Prague'), PLUGINS.profile.save() prints { city: 'Prague' }.
Reacting to state changes
The state delegate of a component runs whenever its state may have changed. Use it to show an error message or to change the look of the field:
self.state = function(type, what) {
self.tclass('has-error', config.touched && config.invalid);
};type says who caused the change:
type | Cause |
|---|---|
0 | The component has been initialized. |
1 | Code, for example SET() or RESET(). |
2 | The user, through an input. |
3 | DEFAULT(). |
what says what happened:
what | Change |
|---|---|
1 | Validation. |
2 | CHANGE(). |
3 | Initialization, RESET() or DEFAULT(). |
4 | UPD(). |
5 | A new value. |