1 // ==========================================================================
  2 // Project:   SproutCore - JavaScript Application Framework
  3 // Copyright: ©2006-2011 Strobe Inc. and contributors.
  4 //            Portions ©2008-2011 Apple Inc. All rights reserved.
  5 // License:   Licensed under MIT license (see license.js)
  6 // ==========================================================================
  7 
  8 sc_require('validators/validator') ;
  9 
 10 /**
 11   Ensures all fields with the Password validator attached in the same form
 12   contain the same value.
 13   
 14   @class
 15   @extends SC.Validator
 16   @author Charles Jolley
 17   @version 1.0
 18 */
 19 SC.Validator.Password = SC.Validator.extend(
 20 /** @scope SC.Validator.Password.prototype */ {
 21 
 22   attachTo: function(form,field) {
 23     sc_super();
 24     if (!this.fields) this.fields = [] ;
 25     this.fields.push(field) ;
 26   },
 27 
 28   validate: function(force) {
 29     if (!this.fields || this.fields.length === 0) return true ;
 30     
 31     var empty = false ;
 32     var notEmpty = false ;
 33     var ret = true ;
 34     var value = this.fields[0].get('fieldValue') ;
 35     this.fields.forEach(function(field) {
 36       var curValue = field.get('fieldValue') ;
 37       if (curValue != value) ret= false ;
 38       if (!curValue || curValue.length === 0) empty = true ;
 39       if (curValue && curValue.length > 0) notEmpty = true ;
 40     }) ;
 41 
 42     // if forces, valid OK if there was an empty.  If not forced, valid OK 
 43     // only if all fields match AND they are not all empty.
 44     if (force) {
 45       return (notEmpty === false) ? false : ret ;
 46     } else {
 47       return (empty === true) ? true : ret ;
 48     }
 49   },
 50   
 51   // update field states
 52   updateFields: function(form,valid) {
 53     if (!this.fields || this.fields.length === 0) return true ;
 54     var err = SC.String.loc("Invalid.Password");
 55     var topField = this._field ;
 56     this.fields.forEach(function(f) {
 57       var msg = (valid) ? null : ((f == topField) ? err : '') ;
 58       form.setErrorFor(f,msg) ;
 59     }) ;
 60     return (valid) ? SC.VALIDATE_OK : err ;
 61   },
 62   
 63   validateChange: function(form, field, oldValue) { 
 64     return this.updateFields(form, this.validate(false)) ;
 65   },
 66 
 67   // this method is called just before the form is submitted.
 68   // field: the field to validate.
 69   validateSubmit: function(form, field) { 
 70     return this.updateFields(form, this.validate(true)) ;
 71   },
 72 
 73   // this method gets called 1ms after the user types a key (if a change is
 74   // allowed).  You can use this validate the new partial string and return 
 75   // an error if needed.
 76   //
 77   // The default will validate a partial only if there was already an error.
 78   // this allows the user to try to get it right before you bug them.
 79   validatePartial: function(form, field) {
 80     var isInvalid = !this._field.get('isValid') ;
 81     if (isInvalid) {
 82       return this.updateFields(form, this.validate(false)) ;
 83     } else return SC.VALIDATE_NO_CHANGE ;
 84   }
 85     
 86 }) ;
 87