Home›Examples›Valid Library: Custom Validation, Secondary Validation, and Examples of Events & Methods
Valid Library: Custom Validation, Secondary Validation, and Examples of Events & Methods
Category: ExamplesThe Valid validation module performs value validation on form fields. It requires the form field to have a 'name' attribute and is best used within a 'form' node. When submitting the form, 'validate' supports secondary validation. It also supports asynchronous Ajax validation. Built-in validations include value type checks, value strength checks, string inclusion checks, numerical range checks, and quantity checks. Additionally, it supports custom validation methods.
Custom Validation Types
When the built-in validation types don't meet requirements, users can extend their own validation types using the extend
parameter. Basic syntax:
new orca.Valid('#ID',{
extend:{
'mytype01':(param)=>{
return {passed:true};
},
},
type:'mytype01'
});
Important notes:
- The name defined in extend is what you use in type
- Extended types should be functions that return a standard object with at least a
passed
property - The
param
parameter in custom validation contains:- name: Field name being validated
- value: Field value being validated
- label: Field alias (defaults to name if not defined via options.label)
- type: Validation type name
- ins: Valid instance, providing access to internal variables/methods
- data: Reference value set in type (user-defined value/type)
- When using custom validation types in type, values can be omitted
Secondary Validation
Sometimes form values aren't entered via keyboard, so blur
, change
and input
events won't trigger. Also, if form controls have validation, the form shouldn't submit if any validation fails.
For button
, a
or oc-btn
form submission, manually validate using validTools.validForm
before submitting.
Works with async validation too.
hide/show Methods
Validation containers persist after creation unless removed. Use hide()
to hide and show()
to display.
Event Listeners
There are 4 event listeners available: trigger
, change
, hide
, and show
:
Destruction
Use destroy()
to remove an instance (only init()
remains usable). Use init()
to restore.