[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Vtiger/resources/validator/ -> BaseValidator.js (source)

   1  /*+***********************************************************************************
   2   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   3   * ("License"); You may not use this file except in compliance with the License
   4   * The Original Code is:  vtiger CRM Open Source
   5   * The Initial Developer of the Original Code is vtiger.
   6   * Portions created by vtiger are Copyright (C) vtiger.
   7   * All Rights Reserved.
   8   *************************************************************************************/
   9  jQuery.Class("Vtiger_Base_Validator_Js",{
  10      
  11      moduleName : false,
  12      
  13      /**
  14       *Function which invokes field validation
  15       *@param accepts field element as parameter
  16       * @return error if validation fails true on success
  17       */
  18      invokeValidation: function(field, rules, i, options){
  19          //If validation engine already maked the field as error 
  20          // we dont want to proceed
  21          if(typeof options !=  "undefined") {
  22              if(options.isError == true){
  23                  return;
  24              }
  25          }
  26          var listOfValidators = Vtiger_Base_Validator_Js.getValidator(field);
  27          for(var i=0; i<listOfValidators.length; i++){
  28              var validatorList = listOfValidators[i];
  29              var validatorName = validatorList.name;
  30              var validatorInstance = new validatorName();
  31              validatorInstance.setElement(field);
  32              if(validatorList.hasOwnProperty("params")){
  33                  var result = validatorInstance.validate(validatorList.params);
  34              }else{
  35                  var result = validatorInstance.validate();
  36              }
  37              if(!result){
  38                  return validatorInstance.getError();
  39              }
  40          }
  41      },
  42  
  43      /**
  44       *Function which gets the complete list of validators based on type and data-validator
  45       *@param accepts field element as parameter
  46       * @return list of validators for field
  47       */
  48      getValidator: function(field){
  49          var listOfValidators = new Array();
  50          var fieldData = field.data();
  51          var fieldInfo = fieldData.fieldinfo;
  52          if(typeof fieldInfo == 'string') {
  53              fieldInfo = JSON.parse(fieldInfo);
  54          }
  55          var dataValidator = "validator";
  56          var moduleEle = field.closest('form').find('[name="module"]');
  57          if(Vtiger_Base_Validator_Js.moduleName == false && moduleEle.length > 0) {
  58              Vtiger_Base_Validator_Js.moduleName = moduleEle.val();
  59          }
  60          
  61          var fieldInstance = Vtiger_Field_Js.getInstance(fieldInfo);
  62          var validatorsOfType = Vtiger_Base_Validator_Js.getValidatorsFromFieldType(fieldInstance);
  63          for(var key in validatorsOfType){
  64              //IE for loop fix
  65              if(!validatorsOfType.hasOwnProperty(key)){
  66                  continue;
  67              }
  68              var value = validatorsOfType[key]; 
  69              if(value != ""){
  70                  var tempValidator = {'name' : value}; 
  71                  listOfValidators.push(tempValidator); 
  72              }
  73          } 
  74          if(fieldData.hasOwnProperty(dataValidator)){
  75              var specialValidators = fieldData[dataValidator];
  76              for(var key in specialValidators){
  77                  //IE for loop fix
  78                  if(!specialValidators.hasOwnProperty(key)){
  79                      continue;
  80                  }
  81                  var specialValidator = specialValidators[key];
  82                  var tempSpecialValidator = jQuery.extend({},specialValidator);
  83                  var validatorOfNames = Vtiger_Base_Validator_Js.getValidatorClassName(specialValidator.name);
  84                  if(validatorOfNames != ""){
  85                      tempSpecialValidator.name =  validatorOfNames;                            
  86                      if(! jQuery.isEmptyObject(tempSpecialValidator)){
  87                          listOfValidators.push(tempSpecialValidator);
  88                      } 
  89                  }
  90              }
  91          }
  92          return listOfValidators;
  93      },
  94  
  95      /**
  96       *Function which gets the list of validators based on data type of field
  97       *@param accepts fieldInstance as parameter
  98       * @return list of validators for particular field type
  99       */
 100      getValidatorsFromFieldType: function(fieldInstance){
 101          var fieldType = fieldInstance.getType();
 102          var validatorsOfType = new Array();
 103          fieldType = fieldType.charAt(0).toUpperCase() + fieldType.slice(1).toLowerCase();
 104          validatorsOfType.push(Vtiger_Base_Validator_Js.getValidatorClassName(fieldType));
 105          return validatorsOfType;
 106      },
 107      
 108      getValidatorClassName: function(validatorName){
 109          var validatorsOfType = '';
 110          var className = Vtiger_Base_Validator_Js.getClassName(validatorName);
 111          var fallBackClassName = Vtiger_Base_Validator_Js.getFallBackClassName(validatorName);
 112          if (typeof window[className] != 'undefined'){
 113              validatorsOfType = (window[className]);
 114          }else if (typeof window[fallBackClassName] != 'undefined'){
 115              validatorsOfType = (window[fallBackClassName]);
 116          }
 117          return validatorsOfType;
 118      },
 119      /**
 120       *Function which gets validator className
 121       *@param accepts validatorName as parameter
 122       * @return module specific validator className
 123       */
 124      getClassName: function(validatorName){
 125          if(Vtiger_Base_Validator_Js.moduleName != false) {
 126              var moduleName = Vtiger_Base_Validator_Js.moduleName;
 127          } else {
 128              var moduleName = app.getModuleName();
 129          }
 130          
 131          if(moduleName == 'Events') {
 132              moduleName = 'Calendar';
 133          }
 134          
 135          return moduleName+"_"+validatorName+"_Validator_Js";
 136      },
 137  
 138      /**
 139       *Function which gets validator className
 140       *@param accepts validatorName as parameter
 141       * @return generic validator className
 142       */
 143      getFallBackClassName: function(validatorName){
 144          return "Vtiger_"+validatorName+"_Validator_Js";
 145      }
 146  },{
 147      field: "",
 148      error: "",
 149  
 150      /**
 151       *Function which validates the field data
 152       * @return true
 153       */
 154      validate: function(){
 155          
 156          return true;
 157      },
 158  
 159      /**
 160       *Function which gets error message
 161       * @return error message
 162       */
 163      getError: function(){
 164          if(this.error != null){
 165              return this.error;
 166          }
 167          return "Validation Failed";
 168      },
 169  
 170      /**
 171       *Function which sets error message
 172       * @return Instance
 173       */
 174      setError: function(errorInfo){
 175          this.error = errorInfo;
 176          return this;
 177      },
 178  
 179      /**
 180       *Function which sets field attribute of class
 181       * @return Instance
 182       */
 183      setElement: function(field){
 184          this.field = field;
 185          return this;
 186      },
 187  
 188      /**
 189       *Function which gets field attribute of class
 190       * @return Instance
 191       */
 192      getElement: function(){
 193          return this.field;
 194      },
 195  
 196      /**
 197       *Function which gets trimed field value
 198       * @return fieldValue
 199       */
 200      getFieldValue: function(){
 201          var field = this.getElement();
 202          return jQuery.trim(field.val());
 203      }
 204  });


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1