Skip to content

Commit 8ebb668

Browse files
author
Olivier Refalo
committed
removed hidePrompt and validateField, logic moved to hide and validate
1 parent f97752c commit 8ebb668

File tree

9 files changed

+113
-108
lines changed

9 files changed

+113
-108
lines changed

README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Installation
2727
### What's in the archive?
2828

2929
The archive holds, of course, the core library along with translations in different languages.
30-
It also comes with a set of demo pages and a simple ajax server (built in Java).
30+
It also comes with a set of demo pages and a simple ajax server (built in Java and php).
3131

3232
1. Unpack the archive
3333
2. Include the script jquery.validationEngine.closure.js in your page
@@ -178,22 +178,24 @@ $("#formID1").validationEngine('detach');
178178

179179
### validate
180180

181-
Validates the form and displays prompts accordingly.
182-
Returns *true* if the form validates, *false* if it contains errors. Note that if you use an ajax form validator, the actual result will be delivered asynchronously to the function *options.onAjaxFormComplete*.
181+
Validates a form or a list of fields, displays error prompts accordingly.
182+
Returns *true* if the form validates, *false* if it contains errors.
183183

184-
```js
185-
alert( $("#formID1").validationEngine('validate') );
186-
```
184+
When using form validation with ajax, it returns *undefined* , the result is delivered asynchronously via function *options.onAjaxFormComplete*.
187185

188-
### validate one field
186+
```
187+
// form validation
188+
alert( $("#formID1").validationEngine('validate') );
189189
190-
Validates one field and displays the prompt accordingly.
191-
Returns *false* if the input validates, *true* if it contains errors.
190+
// field validation
191+
alert( $("#emailInput").validationEngine('validate') );
192192
193-
```js
194-
alert( $("#formID1").validationEngine('validateField', "#emailInput") );
193+
// multiple field validations
194+
alert( $("#email1 #email2 #email3").validationEngine('validate') );
195195
```
196196

197+
Note: validating a list of forms is not supported.
198+
197199
### showPrompt (promptText, type, promptPosition, showArrow)
198200

199201
Displays a prompt on a given element. Note that the prompt can be displayed on any element by providing an id.
@@ -211,25 +213,25 @@ The method takes four parameters:
211213
</fieldset>
212214
```
213215

214-
### hidePrompt
215-
216-
Closes the prompt linked to the input.
217-
218-
```js
219-
$('#inputID').validationEngine('hidePrompt');
220-
```
221-
222216
### hide
223217

224-
Closes error prompts in the current form (in case you have more than one form on the page).
218+
The hide method can be applied to a form or a list of fields.
219+
It closes/hides error prompts.
225220

226221
```js
222+
// closes all form prompts
227223
$('#formID1').validationEngine('hide');
224+
225+
// closes onle one prompt
226+
$('#email1').validationEngine('hide');
227+
228+
// closes a set of prompts
229+
$('#email1 #email2 #email3').validationEngine('hide');
228230
```
229231

230232
### hideAll
231233

232-
Closes **all** error prompts on the page.
234+
Closes/hides **all** error prompts on the page no matter what form they are attached to.
233235

234236
```js
235237
$('#formID1').validationEngine('hideAll');

TODO

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
#get rid of
3+
4+
$("#formID1").validationEngine('validateField', "#emailInput")
5+
6+
now duplicate with 'hide'
7+
$('#inputID').validationEngine('hidePrompt');
8+
9+
$("#formID1").validationEngine('validateField', "#emailInput") );
10+
11+
#support
12+
13+
$("#emailInput").validationEngine('validate')
14+
15+
$("#f1 #f2 #f3).validationEngine('hide');
16+
17+
18+
19+
update doc
20+
21+
22+
/**
23+
* Closes all error prompts on the page
24+
*/
25+
hidePrompt: function() {
26+
var form = $(this).closest('form');
27+
var options = form.data('jqv');
28+
29+
this.each(function(){
30+
31+
var promptClass = "."+ methods._getClassName($(this).attr("id")) + "formError";
32+
$(promptClass).fadeTo(options.fadeDuration, 0.3, function() {
33+
$(this).parent('.formErrorOuter').remove();
34+
$(this).remove();
35+
});
36+
37+
});
38+
39+
return this;
40+
},

TODO.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.

demos/demoAttr.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<body>
3737
<p>
3838
<a href="#" onclick="alert('is the form valid? '+jQuery('#formID').validationEngine('validate'))">Evaluate form</a>
39-
| <a href="#" onclick="jQuery('#test').validationEngine('validateField', '#sport')">Validate sport1 select</a>
39+
| <a href="#" onclick="jQuery('#sport').validationEngine('validate')">Validate sport1 select</a>
4040
| <a href="#" onclick="jQuery('#sport').validationEngine('hide')">Close favorite sport 1 prompt</a>
4141
| <a href="#" onclick="jQuery('#formID').validationEngine('hide')">Close all prompts on form</a>
4242
| <a href="#" onclick="jQuery('#formID').validationEngine('updatePromptsPosition')">Update all prompts positions</a>

demos/demoAutoHide.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<body>
3737
<p>
3838
<a href="#" onclick="alert('is the form valid? '+jQuery('#formID').validationEngine('validate'))">Evaluate form</a>
39-
| <a href="#" onclick="jQuery('#test').validationEngine('validateField', '#sport')">Validate sport1 select</a>
39+
| <a href="#" onclick="jQuery('#sport').validationEngine('validate')">Validate sport1 select</a>
4040
| <a href="#" onclick="jQuery('#sport').validationEngine('hide')">Close favorite sport 1 prompt</a>
4141
| <a href="#" onclick="jQuery('#formID').validationEngine('hide')">Close all prompts on form</a>
4242
| <a href="#" onclick="jQuery('#formID').validationEngine('updatePromptsPosition')">Update all prompts positions</a>

demos/demoGlobalSettings.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<body>
4040
<p>
4141
<a href="#" onclick="alert('is the form valid? '+jQuery('#formID').validationEngine('validate'))">Evaluate form</a>
42-
| <a href="#" onclick="jQuery('#test').validationEngine('validateField', '#sport')">Validate sport1 select</a>
42+
| <a href="#" onclick="jQuery('#sport').validationEngine('validate')">Validate sport1 select</a>
4343
| <a href="#" onclick="jQuery('#sport').validationEngine('hide')">Close favorite sport 1 prompt</a>
4444
| <a href="#" onclick="jQuery('#formID').validationEngine('hide')">Close all prompts on form</a>
4545
| <a href="#" onclick="jQuery('#test').validationEngine('showPrompt', 'This is an example', 'pass')">Build a prompt on a div</a>

demos/demoRegExp.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
<body>
2222
<p>
2323
<a href="#" onclick="alert('is the form valid? '+jQuery('#formID').validationEngine('validate'))">Evaluate form</a>
24-
| <a href="#" onclick='jQuery("#formID").validationEngine("validateField", "#email");'>Evaluate one field</a>
24+
| <a href="#" onclick='jQuery("#formID").validationEngine("validate", "#email");'>Evaluate one field</a>
2525
| <a href="#" onclick="jQuery('#formID').validationEngine('showPrompt', 'This is an example', 'pass')">Build a prompt on a div</a>
2626
| <a href="#" onclick="jQuery('#formID').validationEngine('hide')">Close all prompts</a>
27-
| <a href="#" onclick="jQuery('#telephone').validationEngine('hidePrompt')">Close Telephone prompt</a>
27+
| <a href="#" onclick="jQuery('#telephone').validationEngine('hide')">Close Telephone prompt</a>
2828
| <a href="../index.html" >Back to index</a>
2929
</p>
3030
<p>

demos/demoValidators.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<body>
3737
<p>
3838
<a href="#" onclick="alert('is the form valid? '+jQuery('#formID').validationEngine('validate'))">Evaluate form</a>
39-
| <a href="#" onclick="jQuery('#test').validationEngine('validateField', '#sport')">Validate sport1 select</a>
39+
| <a href="#" onclick="jQuery('#sport').validationEngine('validate')">Validate sport1 select field</a>
4040
| <a href="#" onclick="jQuery('#sport').validationEngine('hide')">Close favorite sport 1 prompt</a>
4141
| <a href="#" onclick="jQuery('#formID').validationEngine('hide')">Close all prompts on form</a>
4242
| <a href="#" onclick="jQuery('#formID').validationEngine('updatePromptsPosition')">Update all prompts positions</a>

js/jquery.validationEngine.js

Lines changed: 44 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
$(this).remove();
3333
});
3434
});
35-
3635
}
3736
return this;
3837
},
@@ -43,6 +42,11 @@
4342
*/
4443
attach: function(userOptions) {
4544

45+
if(!$(this).is("form")) {
46+
alert("Sorry, jqv.attach() only applies to a form");
47+
return this;
48+
}
49+
4650
var form = this;
4751
var options;
4852

@@ -73,6 +77,12 @@
7377
* Unregisters any bindings that may point to jQuery.validaitonEngine
7478
*/
7579
detach: function() {
80+
81+
if(!$(this).is("form")) {
82+
alert("Sorry, jqv.detach() only applies to a form");
83+
return this;
84+
}
85+
7686
var form = this;
7787
var options = form.data('jqv');
7888

@@ -91,47 +101,34 @@
91101
form.die("submit", methods.onAjaxFormComplete);
92102
form.removeData('jqv');
93103

94-
if (options.autoPositionUpdate) {
95-
$(window).unbind("resize", methods.updatePromptsPosition)
96-
}
104+
if (options.autoPositionUpdate)
105+
$(window).unbind("resize", methods.updatePromptsPosition);
97106

98107
return this;
99108
},
100109
/**
101-
* Validates the form fields, shows prompts accordingly.
110+
* Validates either a form or a list of fields, shows prompts accordingly.
102111
* Note: There is no ajax form validation with this method, only field ajax validation are evaluated
103112
*
104113
* @return true if the form validates, false if it fails
105114
*/
106-
validate: function() {
107-
return methods._validateFields(this);
108-
},
109-
/**
110-
* Validates one field, shows prompt accordingly.
111-
* Note: There is no ajax form validation with this method, only field ajax validation are evaluated
112-
*
113-
* @return true if the form validates, false if it fails
114-
*/
115-
validateField: function(el) {
116-
var options = $(this).data('jqv');
117-
var r = methods._validateField($(el), options);
118-
119-
if (options.onSuccess && options.InvalidFields.length == 0)
120-
options.onSuccess();
121-
else if (options.onFailure && options.InvalidFields.length > 0)
122-
options.onFailure();
115+
validate: function() {
116+
if($(this).is("form"))
117+
return methods._validateFields(this);
118+
else {
119+
// field validation
120+
var form = $(this).closest('form');
121+
var options = form.data('jqv');
122+
var r = methods._validateField($(this), options);
123+
124+
if (options.onSuccess && options.InvalidFields.length == 0)
125+
options.onSuccess();
126+
else if (options.onFailure && options.InvalidFields.length > 0)
127+
options.onFailure();
123128

124-
return r;
125-
},
126-
/**
127-
* Validates the form fields, shows prompts accordingly.
128-
* Note: this methods performs fields and form ajax validations(if setup)
129-
*
130-
* @return true if the form validates, false if it fails, undefined if ajax is used for form validation
131-
*/
132-
validateform: function() {
133-
return methods._onSubmitEvent.call(this);
134-
},
129+
return r;
130+
}
131+
},
135132
/**
136133
* Redraw prompts position, useful when you change the DOM state when validating
137134
*/
@@ -142,7 +139,7 @@
142139
var noAnimation = event.data.noAnimation;
143140
}
144141
else
145-
var form = $(this.closest('form'));
142+
var form = $(this.closest('form'));
146143

147144
var options = form.data('jqv');
148145
// No option, take default one
@@ -152,7 +149,7 @@
152149
var promptText = $(prompt).find(".formErrorContent").html();
153150

154151
if(prompt)
155-
methods._updatePrompt(field, $(prompt), promptText, undefined, false, options, noAnimation);
152+
methods._updatePrompt(field, $(prompt), promptText, undefined, false, options, noAnimation);
156153
});
157154
return this;
158155
},
@@ -169,38 +166,27 @@
169166
var form = this.closest('form');
170167
var options = form.data('jqv');
171168
// No option, take default one
172-
if(!options) options = methods._saveOptions(this, options);
169+
if(!options)
170+
options = methods._saveOptions(this, options);
173171
if(promptPosition)
174-
options.promptPosition=promptPosition;
172+
options.promptPosition=promptPosition;
175173
options.showArrow = showArrow==true;
176174

177175
methods._showPrompt(this, promptText, type, false, options);
178176
return this;
179177
},
180178
/**
181-
* Closes all error prompts on the page
182-
*/
183-
hidePrompt: function() {
184-
var form = this;
185-
var options = form.data('jqv');
186-
var promptClass = "."+ methods._getClassName($(this).attr("id")) + "formError";
187-
$(promptClass).fadeTo(options.fadeDuration, 0.3, function() {
188-
$(this).parent('.formErrorOuter').remove();
189-
$(this).remove();
190-
});
191-
return this;
192-
},
193-
/**
194179
* Closes form error prompts, CAN be invidual
195180
*/
196181
hide: function() {
197182
var form = $(this).closest('form');
198-
if(form.length == 0) return this;
183+
if(form.length == 0)
184+
return this;
199185
var options = form.data('jqv');
200186
var closingtag;
201-
if($(this).is("form")){
187+
if($(this).is("form")) {
202188
closingtag = "parentForm"+methods._getClassName($(this).attr("id"));
203-
}else{
189+
} else {
204190
closingtag = methods._getClassName($(this).attr("id")) +"formError";
205191
}
206192
$('.'+closingtag).fadeTo(options.fadeDuration, 0.3, function() {
@@ -213,6 +199,7 @@
213199
* Closes all error prompts on the page
214200
*/
215201
hideAll: function() {
202+
216203
var form = this;
217204
var options = form.data('jqv');
218205
var duration = options ? options.fadeDuration:0.3;
@@ -258,6 +245,7 @@
258245

259246
if (r && options.ajaxFormValidation) {
260247
methods._validateFormWithAjax(form, options);
248+
// cancel form auto-submission - process with async call onAjaxFormComplete
261249
return false;
262250
}
263251

@@ -310,10 +298,9 @@
310298
errorFound |= methods._validateField(field, options, skipAjaxValidation);
311299
if (options.doNotShowAllErrosOnSubmit)
312300
return false;
313-
if (errorFound && first_err==null) {
301+
if (errorFound && first_err==null)
314302
first_err=field;
315-
}
316-
names.push(field.attr('name'));
303+
names.push(field.attr('name'));
317304
}
318305
});
319306

@@ -1644,7 +1631,7 @@
16441631
if (typeof(method) == 'string' && method.charAt(0) != '_' && methods[method]) {
16451632

16461633
// make sure init is called once
1647-
if(method != "showPrompt" && method != "hidePrompt" && method != "hide" && method != "hideAll")
1634+
if(method != "showPrompt" && method != "hide" && method != "hideAll")
16481635
methods.init.apply(form);
16491636

16501637
return methods[method].apply(form, Array.prototype.slice.call(arguments, 1));

0 commit comments

Comments
 (0)