Skip to content

Commit

Permalink
implimented enable and disable in the standalone version, along with …
Browse files Browse the repository at this point in the history
…unit tests for them.

git-svn-id: svn://livevalidation.com/livevalidation/trunk@42 c130c166-013a-0410-90be-eade3a03707a
  • Loading branch information
alechill committed Jan 24, 2008
1 parent da6ac5b commit 5ea2220
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/livevalidation_prototype.js
Expand Up @@ -288,19 +288,19 @@ LiveValidation.prototype = {
},

/**
* enables the field
* enables the field
*
* @return {Boolean} - whether the all the validations passed or if one failed
* @return {LiveValidation} - the LiveValidation object for chaining
*/
enable: function(){
this.element.disabled = false;
return this;
},

/**
* disables the field and removes any message and stylesassociated with the field
* disables the field and removes any message and styles associated with the field
*
* @return {Boolean} - whether the all the validations passed or if one failed
* @return {LiveValidation} - the LiveValidation object for chaining
*/
disable: function(){
this.element.disabled = true;
Expand Down
43 changes: 33 additions & 10 deletions src/livevalidation_standalone.js
Expand Up @@ -113,7 +113,7 @@ LiveValidation.prototype = {
this.element.onchange = function(e){ self.validate(); return self.oldOnChange.call(this, e); }
break;
default:
if(!this.onlyOnBlur) this.element.onkeyup = function(e){;self.deferValidation(); return self.oldOnKeyup.call(this, e); }
if(!this.onlyOnBlur) this.element.onkeyup = function(e){ self.deferValidation(); return self.oldOnKeyup.call(this, e); }
this.element.onblur = function(e){ self.doOnBlur(e); return self.oldOnBlur.call(this, e); }
}
}
Expand Down Expand Up @@ -144,7 +144,7 @@ LiveValidation.prototype = {
this.element.onblur = this.oldOnBlur;
}
}
// @todo - remove message and field class
this.removeMessageAndFieldClass();
},

/**
Expand Down Expand Up @@ -275,15 +275,38 @@ LiveValidation.prototype = {
* @return {Boolean} - whether the all the validations passed or if one failed
*/
validate: function(){
var isValid = this.doValidations();
if(isValid){
this.onValid();
return true;
}else{
this.onInvalid();
return false;
}
if(!this.element.disabled){
var isValid = this.doValidations();
if(isValid){
this.onValid();
return true;
}else {
this.onInvalid();
return false;
}
}
},

/**
* enables the field
*
* @return {LiveValidation} - the LiveValidation object for chaining
*/
enable: function(){
this.element.disabled = false;
return this;
},

/**
* disables the field and removes any message and styles associated with the field
*
* @return {LiveValidation} - the LiveValidation object for chaining
*/
disable: function(){
this.element.disabled = true;
this.removeMessageAndFieldClass();
return this;
},

/** Message insertion methods ****************************
*
Expand Down
37 changes: 26 additions & 11 deletions src/tests_standalone_version.js
Expand Up @@ -120,6 +120,8 @@ function runTests(){
setup: function() { with(this) {
// set up some pre defined events to check they are preserved
var el = $('myText');
el.value = '';
el.disabled = false;
el.focusCheck = false;
el.onfocus = function(){ this.focusCheck = true; }
el.blurCheck = false;
Expand All @@ -137,6 +139,7 @@ function runTests(){
formEl.onsubmit = function(){ this.submitCheck = true; return false; }
// preset inline check values
var iEl = $('myInlineText');
iEl.disabled = false;
iEl.inlineFocusCheck = false;
iEl.inlineBlurCheck = false;
iEl.inlineKeyupCheck = false;
Expand Down Expand Up @@ -684,7 +687,7 @@ function runTests(){
/*********************** LiveValidation *****************************/

testInstanciateLiveValidation: function(){ with(this){
// test that a LiveValidation object can be set up
// test that a LiveValidation object has been set up
assertEqual($('myText'), subjects.lv.element, "Expecting LiveValidation element to be myField0");
// test its default values
assertEqual('Thankyou!', subjects.lv.validMessage);
Expand All @@ -704,7 +707,7 @@ function runTests(){
assertEqual('function', typeof subjects.lv.onInvalid);
}},

testadd: function(){ with(this){
testAdd: function(){ with(this){
// test validations array is empty to start
assertEqual(0, subjects.lv.validations.length , "Length should be 0" );
// add a validation
Expand All @@ -723,19 +726,19 @@ function runTests(){
// test it is a text input
assertEqual(LiveValidation.TEXT, subjects.lv.getElementType('myText'));
// test it is a textarea
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('myTextarea');
assertEqual(LiveValidation.TEXTAREA, subjects.lv.getElementType('myTextarea'));
// test it is a password input
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('myPassword');
assertEqual(LiveValidation.PASSWORD, subjects.lv.getElementType('myPassword'));
// test it is a checkbox input
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('myCheckbox');
assertEqual(LiveValidation.CHECKBOX, subjects.lv.getElementType('myCheckbox'));
// test it is a select element
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('mySelect');
assertEqual(LiveValidation.SELECT, subjects.lv.getElementType('mySelect'));
}},
Expand All @@ -761,7 +764,7 @@ function runTests(){
testDoValidations: function(){ with(this){
subjects.lv.add(Validate.Presence);
// test that it returns false if the validation should fail
assertEqual(false, !subjects.lv.doValidations(), "Should return false as this should fail");
assertEqual(false, subjects.lv.doValidations(), "Should return false as this should fail");
assert(subjects.lv.displayMessageWhenEmpty, "subjects.lv.displayMessageWhenEmpty should be true for a presence validation");
// test that it returns true if the validation should pass
subjects.lv.element.value = 'hello world';
Expand Down Expand Up @@ -916,7 +919,7 @@ function runTests(){
}},

testOnlyOnBlur: function(){ with(this){
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('myText', {onlyOnBlur: true});
subjects.lv.add(Validate.Presence);
Event.simulateEvent(subjects.lv.element, 'focus');
Expand All @@ -930,7 +933,7 @@ function runTests(){
}},

testDeferValidation: function(){ with(this){
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('myText', {wait: 1500});
subjects.lv.add(Validate.Presence);
subjects.lv.element.value = '';
Expand All @@ -940,7 +943,7 @@ function runTests(){
}},

testOnlyOnSubmit: function(){ with(this){
subjects.lv.destroy(); //destroy previous to remove events
subjects.lv.destroy(); //destroy previous to remove events etc.
subjects.lv = new LiveValidation('myText', {onlyOnSubmit: true});
subjects.lv.add(Validate.Presence);
Event.simulateEvent(subjects.lv.element, 'focus');
Expand All @@ -950,6 +953,18 @@ function runTests(){
Event.simulateEvent(subjects.lv.form, 'submit');
assertEqual("Can't be empty!", subjects.lv.message, "Message should be set to default Presence failure message now that the form has submitted");
}},

testEnableDisable: function(){ with(this){
subjects.lv.add(Validate.Presence);
subjects.lv.element.value = '';
subjects.lv.disable();
subjects.lv.element.value = 'Woooo';
subjects.lv.validate();
assertEqual(undefined, subjects.lv.message, "Message should still be undefined, as the validation should not have run again as disabled");
subjects.lv.enable();
subjects.lv.validate();
assertEqual("Thankyou!", subjects.lv.message, "Message should be set to default valid message, as validation has been run and valid");
}},

testPreserveOldOnFocus: function(){ with(this){
Event.simulateEvent(subjects.lv.element, 'focus');
Expand Down Expand Up @@ -1032,7 +1047,7 @@ function runTests(){
subjects.ilv.add(Validate.Presence);

Event.simulateEvent(subjects.ilv.form, 'submit');
assertEqual(false, subjects.ilv.form.inlineSubmitCheck, 'Should be false because old was not run as not valid');
assertEqual(false, subjects.ilv.form.inlineSubmitCheck, 'Should be false because old should not run as not valid');
subjects.ilv.element.value = 'i am valid';
Event.simulateEvent(subjects.ilv.form, 'submit');
assertEqual(true, subjects.ilv.form.inlineSubmitCheck, 'Should be true because old onsubmit should have run as it is now valid');
Expand Down

0 comments on commit 5ea2220

Please sign in to comment.