Skip to content

Commit c550458

Browse files
Arknistaabm
authored andcommitted
Entries: document the ability to define a global normalizer (#33)
1 parent d23a2e2 commit c550458

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

entries/normalizer.xml

+60-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
</signature>
99
<desc>Prepares/transforms the elements value for validation.</desc>
1010
<longdesc>
11-
Note that this method:
12-
<p>Doesn't change the elements' value, it only changes the value used for validation.</p>
13-
<p>Gets the value passed as argument, and "this" within it references the corresponding DOMElement.</p>
14-
<p>Needs to return a String value, otherwise it will throw a TypeError exception.</p>
11+
<p>Transform the value of an element and the result for validation instead of the initial value. The normalizer can be defined global to all elements or local to only one element. With that said, the local normalizer will only run for the element for which it was defined. The global normalizer will run for all validated elements. This normalizer can be then overrided for each element, as needed, by attaching one to it. This way only the local one will run for that element, and the global one will run for others.</p>
12+
<p>Note that this method:</p>
13+
<ul>
14+
<li><p>Doesn't change the elements' value, it only changes the value used for validation.</p></li>
15+
<li><p>Gets the value passed as argument, and "this" within it references the corresponding <code>DOMElement</code>.</p></li>
16+
<li><p>Needs to return a String value, otherwise it will throw a <code>TypeError</code> exception.</p></li>
17+
</ul>
1518
</longdesc>
1619
<example>
1720
<desc>Makes "field" required and use a normalizer to trim its value before validating</desc>
@@ -38,21 +41,67 @@ $( "#myform" ).validate( {
3841
<input type="submit" value="Validate!">
3942
]]></html>
4043
</example>
41-
<example>
42-
<desc>Makes "url" required and use a normalizer to append 'http://', if not present, to the value of the "url" element before validating</desc>
44+
<example>
45+
<desc>Makes "url" required and use a normalizer to append 'http://', if not present, to the value of the "url" element before validating</desc>
46+
<code><![CDATA[
47+
$( "#myform" ).validate( {
48+
rules: {
49+
url_input: {
50+
required: true,
51+
url: true,
52+
normalizer: function( value ) {
53+
var url = value;
54+
55+
// Check if it doesn't start with http:// or https:// or ftp://
56+
if ( url && url.substr( 0, 7 ) !== "http://"
57+
&& url.substr( 0, 8 ) !== "https://"
58+
&& url.substr( 0, 6 ) !== "ftp://" ) {
59+
// then prefix with http://
60+
url = "http://" + url;
61+
}
62+
63+
// Return the new url
64+
return url;
65+
}
66+
}
67+
}
68+
} );
69+
]]></code>
70+
<html><![CDATA[
71+
<label for="url_input">url: </label>
72+
<input class="left" id="url_input" name="url_input">
73+
<br/>
74+
<input type="submit" value="Validate!">
75+
]]></html>
76+
</example>
77+
78+
<example>
79+
<desc>Using a global normalizer in conjunction with a local one</desc>
4380
<code><![CDATA[
4481
$( "#myform" ).validate( {
82+
// This global normalizer will trim the value of all elements
83+
// before validatng them.
84+
normalizer: function( value ) {
85+
return $.trim( value );
86+
},
4587
rules: {
88+
username: {
89+
required: true
90+
},
4691
url_input: {
4792
required: true,
4893
url: true,
94+
95+
// We don't need to trim the value of this element, so we overrided
96+
// the global normalizer in order to append 'http://' to the url value
97+
// if doesn't already.
4998
normalizer: function( value ) {
5099
var url = value;
51100
52101
// Check if it doesn't start with http:// or https:// or ftp://
53102
if ( url && url.substr( 0, 7 ) !== "http://"
54-
&& url.substr( 0, 8 ) !== "https://"
55-
&& url.substr( 0, 6 ) !== "ftp://" ) {
103+
&& url.substr( 0, 8 ) !== "https://"
104+
&& url.substr( 0, 6 ) !== "ftp://" ) {
56105
// then prefix with http://
57106
url = "http://" + url;
58107
}
@@ -65,6 +114,9 @@ $( "#myform" ).validate( {
65114
} );
66115
]]></code>
67116
<html><![CDATA[
117+
<label for="username">Required: </label>
118+
<input class="left" id="username" name="username">
119+
<br/>
68120
<label for="url_input">url: </label>
69121
<input class="left" id="url_input" name="url_input">
70122
<br/>

entries/validate.xml

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@
212212
</desc>
213213
<type name="Object"/>
214214
</property>
215+
<property name="normalizer">
216+
<desc>
217+
Prepares/transforms the elements value for validation.
218+
See <a href="/normalizer/">normalizer docs</a> for more details.
219+
</desc>
220+
<type name="Function"/>
221+
</property>
215222
<property name="onsubmit" default="true">
216223
<desc>
217224
Validate the form on submit. Set to false to use only other events for validation.

0 commit comments

Comments
 (0)