By default, the value of field will be taken by using jQuery's val() method.
In some case, you might want to adjust the value before performing validations. For example, the numeric validator doesn't allow to use a comma (,) for thousand separator.
Fortunately, it's now possible to hook the value of field via the transformer option. The following piece of code is an usage of this option:
The transformer option only adjusts the field value and uses the modified value for validating. It does NOT send the modified value to the server when submitting the form
The next sections introduce some examples demonstrating how to apply this option for popular validators.
Using with the numeric validator
By default, the numeric validator doesn't accept the comma.
In the form below, the Price field now accepts value using comma for thousand separator, such as 12,570.634
The phone validator supports phone number in various countries. Despite the fact that it try to support many possible formats of a phone number, it can't cover all or special one which you want it to be a valid phone number.
For instance, a number containing the spaces such as XXX XXX XXXX (where X presents a digit from 0-9) is treated as invalid US phone number.
By using the transformer option, we can turn this kind of number into a valid one by removing all spaces.
WYSIWYG stands for What You See Is What You Get. A WYSIWYG editor provides a visual way to edit the content of input which mostly is a textarea element. TinyMCE, CKEditor, Summernote are some popular one.
Since these editors usually generate additional HTML tags, the raw content of input might be different with the value returned by the editor. The notEmpty, stringLength validators maybe don't work correctly with the field using a WYSIWYG editor.
The following form uses the transformer option to get raw text of the editor before doing validations.