Skip to content

Commit

Permalink
Form data and field name passed to required validation function rule
Browse files Browse the repository at this point in the history
  • Loading branch information
indrimuska committed Apr 12, 2016
1 parent 95c7842 commit 1f67ffb
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 118 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 4.1.12
Form data and field name passed to required validation function rule.

## 4.1.11
Fixed `TypeError: Cannot read property 'unwatchers' of undefined` bug reported in issue #186.
Special thanks to @baconcutter for submitting this PR.
Expand Down
9 changes: 0 additions & 9 deletions dist/form-for.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
text-align: center;
vertical-align: middle;
display: inline-block;
background-image: -webkit-linear-gradient(bottom, #f9f9f9, #fff);
background-image: linear-gradient(to top, #f9f9f9, #fff);
border: 1px solid #aaa;
position: relative;
Expand Down Expand Up @@ -157,7 +156,6 @@ field-label {
text-align: center;
vertical-align: middle;
display: inline-block;
background-image: -webkit-linear-gradient(bottom, #f9f9f9, #fff);
background-image: linear-gradient(to top, #f9f9f9, #fff);
border: 1px solid #aaa;
position: relative;
Expand Down Expand Up @@ -233,7 +231,6 @@ select-field {
}
/************************* Select *************************/
.form-for-field select {
background: -webkit-linear-gradient(top, #fff, #efefef);
background: linear-gradient(top, #fff, #efefef);
padding-right: 30px;
}
Expand Down Expand Up @@ -339,7 +336,6 @@ submit-field {
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-transition: all;
transition: all;
border-radius: 6px;
border-style: none;
Expand All @@ -361,7 +357,6 @@ submit-field {
color: #fff;
}
.submit-button:active {
-webkit-transition: none;
transition: none;
box-shadow: inset 0 4px 2px rgba(0,0,0,0.1);
}
Expand Down Expand Up @@ -449,11 +444,9 @@ text-field {
}

.form-for-tooltip {
display: -webkit-inline-box;
display: -webkit-inline-flex;
display: -ms-inline-flexbox;
display: inline-flex;
-webkit-box-align: center;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
Expand All @@ -464,7 +457,6 @@ text-field {
display: inline-block;
width: 15px;
height: 15px;
-webkit-box-flex: 0;
-webkit-flex: 0 0 15px;
-ms-flex: 0 0 15px;
flex: 0 0 15px;
Expand All @@ -485,7 +477,6 @@ text-field {
display: none;
position: relative;
z-index: 2;
-webkit-box-flex: 0;
-webkit-flex: 0 0 350px;
-ms-flex: 0 0 350px;
flex: 0 0 350px;
Expand Down
206 changes: 103 additions & 103 deletions dist/form-for.js
Original file line number Diff line number Diff line change
Expand Up @@ -2362,106 +2362,6 @@ var formFor;
;
})(formFor || (formFor = {}));
;
/// <reference path="nested-object-helper.ts" />
var formFor;
(function (formFor) {
/*
* Organizes state management for form-submission and field validity.
*
* <p>Intended for use only by formFor directive; this class is not exposed to the $injector.
*/
var FormForStateHelper = (function () {
// TODO Add some documentation
function FormForStateHelper($parse, $scope) {
this.formForScope_ = $scope;
this.nestedObjectHelper_ = new formFor.NestedObjectHelper($parse);
this.formForScope_.fieldNameToErrorMap = $scope.fieldNameToErrorMap || {};
this.formForScope_.valid = true;
this.fieldNameToModifiedStateMap_ = {};
this.formSubmitted_ = false;
this.fieldNameToErrorMap_ = {};
this.watchable = 0;
}
FormForStateHelper.prototype.getFieldError = function (fieldName) {
return this.nestedObjectHelper_.readAttribute(this.formForScope_.fieldNameToErrorMap, fieldName);
};
FormForStateHelper.prototype.hasFieldBeenModified = function (fieldName) {
return this.nestedObjectHelper_.readAttribute(this.fieldNameToModifiedStateMap_, fieldName);
};
FormForStateHelper.prototype.hasFormBeenSubmitted = function () {
return this.formSubmitted_;
};
FormForStateHelper.prototype.isFormInvalid = function () {
return !this.isFormValid();
};
FormForStateHelper.prototype.isFormValid = function () {
for (var prop in this.fieldNameToErrorMap_) {
return false;
}
return true;
};
FormForStateHelper.prototype.resetFieldErrors = function () {
this.formForScope_.fieldNameToErrorMap = {};
};
FormForStateHelper.prototype.setFieldError = function (fieldName, error) {
var safeFieldName = this.nestedObjectHelper_.flattenAttribute(fieldName);
this.nestedObjectHelper_.writeAttribute(this.formForScope_.fieldNameToErrorMap, fieldName, error);
if (error) {
this.fieldNameToErrorMap_[safeFieldName] = error;
}
else {
delete this.fieldNameToErrorMap_[safeFieldName];
}
this.formForScope_.valid = this.isFormValid();
this.watchable++;
};
FormForStateHelper.prototype.setFieldHasBeenModified = function (fieldName, hasBeenModified) {
this.nestedObjectHelper_.writeAttribute(this.fieldNameToModifiedStateMap_, fieldName, hasBeenModified);
this.watchable++;
};
FormForStateHelper.prototype.setFormSubmitted = function (submitted) {
this.formSubmitted_ = submitted;
this.watchable++;
};
return FormForStateHelper;
})();
formFor.FormForStateHelper = FormForStateHelper;
})(formFor || (formFor = {}));
var formFor;
(function (formFor) {
/**
* Utility for working with strings.
*
* <p>Intended for use only by formFor directive; this class is not exposed to the $injector.
*/
var StringUtil = (function () {
function StringUtil() {
}
/**
* Converts text in common variable formats to humanized form.
*
* @param text Name of variable to be humanized (ex. myVariable, my_variable)
* @returns Humanized string (ex. 'My Variable')
*/
StringUtil.humanize = function (text) {
if (!text) {
return '';
}
text = text.replace(/[A-Z]/g, function (match) {
return ' ' + match;
});
text = text.replace(/_([a-z])/g, function (match, $1) {
return ' ' + $1.toUpperCase();
});
text = text.replace(/\s+/g, ' ');
text = text.trim();
text = text.charAt(0).toUpperCase() + text.slice(1);
return text;
};
return StringUtil;
})();
formFor.StringUtil = StringUtil;
})(formFor || (formFor = {}));
/// <reference path="../../definitions/angular.d.ts" />
/// <reference path="form-for-configuration.ts" />
/// <reference path="../utils/nested-object-helper.ts" />
Expand Down Expand Up @@ -2574,7 +2474,7 @@ var formFor;
if (value === undefined || value === null) {
value = ""; // Escape falsy values liked null or undefined, but not ones like 0
}
return this.validateFieldRequired_(value, validationRules) ||
return this.validateFieldRequired_(value, validationRules, formData, fieldName) ||
this.validateFieldMinimum_(value, validationRules) ||
this.validateFieldMinLength_(value, validationRules) ||
this.validateFieldIncrement_(value, validationRules) ||
Expand Down Expand Up @@ -2840,12 +2740,12 @@ var formFor;
}
return null;
};
ModelValidator.prototype.validateFieldRequired_ = function (value, validationRules) {
ModelValidator.prototype.validateFieldRequired_ = function (value, validationRules, formData, fieldName) {
if (validationRules.required) {
var required = angular.isObject(validationRules.required)
? validationRules.required.rule
: angular.isFunction(validationRules.required)
? validationRules.required.call(this, value)
? validationRules.required.apply(this, [value, formData, fieldName])
: validationRules.required;
// Compare both string and numeric values to avoid rejecting non-empty but falsy values (e.g. 0).
var stringValue = value.toString().replace(/\s+$/, ''); // Disallow an all-whitespace string
Expand Down Expand Up @@ -2935,4 +2835,104 @@ var formFor;
return new ModelValidator($interpolate, $parse, $q, FormForConfiguration);
}]);
})(formFor || (formFor = {}));
/// <reference path="nested-object-helper.ts" />
var formFor;
(function (formFor) {
/*
* Organizes state management for form-submission and field validity.
*
* <p>Intended for use only by formFor directive; this class is not exposed to the $injector.
*/
var FormForStateHelper = (function () {
// TODO Add some documentation
function FormForStateHelper($parse, $scope) {
this.formForScope_ = $scope;
this.nestedObjectHelper_ = new formFor.NestedObjectHelper($parse);
this.formForScope_.fieldNameToErrorMap = $scope.fieldNameToErrorMap || {};
this.formForScope_.valid = true;
this.fieldNameToModifiedStateMap_ = {};
this.formSubmitted_ = false;
this.fieldNameToErrorMap_ = {};
this.watchable = 0;
}
FormForStateHelper.prototype.getFieldError = function (fieldName) {
return this.nestedObjectHelper_.readAttribute(this.formForScope_.fieldNameToErrorMap, fieldName);
};
FormForStateHelper.prototype.hasFieldBeenModified = function (fieldName) {
return this.nestedObjectHelper_.readAttribute(this.fieldNameToModifiedStateMap_, fieldName);
};
FormForStateHelper.prototype.hasFormBeenSubmitted = function () {
return this.formSubmitted_;
};
FormForStateHelper.prototype.isFormInvalid = function () {
return !this.isFormValid();
};
FormForStateHelper.prototype.isFormValid = function () {
for (var prop in this.fieldNameToErrorMap_) {
return false;
}
return true;
};
FormForStateHelper.prototype.resetFieldErrors = function () {
this.formForScope_.fieldNameToErrorMap = {};
};
FormForStateHelper.prototype.setFieldError = function (fieldName, error) {
var safeFieldName = this.nestedObjectHelper_.flattenAttribute(fieldName);
this.nestedObjectHelper_.writeAttribute(this.formForScope_.fieldNameToErrorMap, fieldName, error);
if (error) {
this.fieldNameToErrorMap_[safeFieldName] = error;
}
else {
delete this.fieldNameToErrorMap_[safeFieldName];
}
this.formForScope_.valid = this.isFormValid();
this.watchable++;
};
FormForStateHelper.prototype.setFieldHasBeenModified = function (fieldName, hasBeenModified) {
this.nestedObjectHelper_.writeAttribute(this.fieldNameToModifiedStateMap_, fieldName, hasBeenModified);
this.watchable++;
};
FormForStateHelper.prototype.setFormSubmitted = function (submitted) {
this.formSubmitted_ = submitted;
this.watchable++;
};
return FormForStateHelper;
})();
formFor.FormForStateHelper = FormForStateHelper;
})(formFor || (formFor = {}));
var formFor;
(function (formFor) {
/**
* Utility for working with strings.
*
* <p>Intended for use only by formFor directive; this class is not exposed to the $injector.
*/
var StringUtil = (function () {
function StringUtil() {
}
/**
* Converts text in common variable formats to humanized form.
*
* @param text Name of variable to be humanized (ex. myVariable, my_variable)
* @returns Humanized string (ex. 'My Variable')
*/
StringUtil.humanize = function (text) {
if (!text) {
return '';
}
text = text.replace(/[A-Z]/g, function (match) {
return ' ' + match;
});
text = text.replace(/_([a-z])/g, function (match, $1) {
return ' ' + $1.toUpperCase();
});
text = text.replace(/\s+/g, ' ');
text = text.trim();
text = text.charAt(0).toUpperCase() + text.slice(1);
return text;
};
return StringUtil;
})();
formFor.StringUtil = StringUtil;
})(formFor || (formFor = {}));
/// <reference path="../../../definitions/angular.d.ts" />
2 changes: 1 addition & 1 deletion dist/form-for.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/form-for.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular-form-for",
"version": "4.1.11",
"version": "4.1.12",
"description": "Set of Angular directives to simplify creating and validating HTML forms.",
"keywords": [
"angular",
Expand Down
6 changes: 3 additions & 3 deletions source/services/model-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ module formFor {
value = ""; // Escape falsy values liked null or undefined, but not ones like 0
}

return this.validateFieldRequired_(value, validationRules) ||
return this.validateFieldRequired_(value, validationRules, formData, fieldName) ||
this.validateFieldMinimum_(value, validationRules) ||
this.validateFieldMinLength_(value, validationRules) ||
this.validateFieldIncrement_(value, validationRules) ||
Expand Down Expand Up @@ -472,12 +472,12 @@ module formFor {
return null;
}

private validateFieldRequired_(value:any, validationRules:ValidationRules):any {
private validateFieldRequired_(value:any, validationRules:ValidationRules, formData:any, fieldName:any):any {
if (validationRules.required) {
var required:boolean = angular.isObject(validationRules.required)
? (<ValidationRuleBoolean> validationRules.required).rule
: angular.isFunction(validationRules.required)
? validationRules.required.call(this, value)
? validationRules.required.apply(this, [value, formData, fieldName])
: <boolean> validationRules.required;

// Compare both string and numeric values to avoid rejecting non-empty but falsy values (e.g. 0).
Expand Down

0 comments on commit 1f67ffb

Please sign in to comment.