Skip to content

Commit

Permalink
#3001 #2579, add new form validation shorthand, drastically simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
jlukic committed Sep 13, 2015
1 parent 72639db commit e364c16
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
6 changes: 3 additions & 3 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
**Critical Bugfixes**
- **Build** - Fixed issue where using a packaged theme without a `site.variables` would cause build to fail

**Minor Enhancements**
- **Form Validation** - All form rules now have default validation prompts that will appear if no prompt is specified. These prompts include custom templating.
- **Form Validation** - Form validation bracketed rules can now use simple templating `{value}` to represent bracketed rule in prompt, For example 'Please enter at least {value} characters' with `minLength` rule.
**Enhancements**
- **Form Validation** - `identifier` and `prompt` are now optional for all form validation rules. Default prompt values have been added for all rule types, and identifier will now automatically match on the named value for rule if no ID is specified.
- **Form Validation** - All form prompts now support templates values, `{value}`, `{name}`, `{ruleValue}`, and `{identifier}`

**Bugfixes**
- **Form Validation** - Validation messages in `error message` group are now correctly removed when invalid field revalidates on blur
Expand Down
27 changes: 21 additions & 6 deletions src/definitions/behaviors/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ $.fn.form = function(parameters) {
var
ruleName = module.get.ruleName(rule),
ancillary = module.get.ancillaryValue(rule),
prompt = settings.prompt[ruleName] || settings.text.unspecifiedRule,
prompt = rule.prompt || settings.prompt[ruleName] || settings.text.unspecifiedRule,
requiresValue = (prompt.search('{value}') !== -1),
requiresName = (prompt.search('{name}') !== -1),
$label,
Expand Down Expand Up @@ -381,15 +381,13 @@ $.fn.form = function(parameters) {
return prompt;
},
settings: function() {
var
firstProperty
;
if($.isPlainObject(parameters)) {
var
keys = Object.keys(parameters),
keys = Object.keys(parameters),
isLegacySettings = (keys.length > 0)
? (parameters[keys[0]].identifier !== undefined && parameters[keys[0]].rules !== undefined)
: false
: false,
ruleKeys
;
if(isLegacySettings) {
// 1.x (ducktyped)
Expand All @@ -400,6 +398,23 @@ $.fn.form = function(parameters) {
}
else {
// 2.x
if(parameters.fields) {
ruleKeys = Object.keys(parameters.fields);
if( typeof parameters.fields[ruleKeys[0]] == 'string' || $.isArray(parameters.fields[ruleKeys[0]]) ) {
$.each(parameters.fields, function(name, rules) {
if(typeof rules == 'string') {
rules = [rules];
}
parameters.fields[name] = {
rules: []
};
$.each(rules, function(index, rule) {
parameters.fields[name].rules.push({ type: rule });
});
});
}
}

settings = $.extend(true, {}, $.fn.form.settings, parameters);
validation = $.extend({}, $.fn.form.settings.defaults, settings.fields);
module.verbose('Extending settings', validation, settings);
Expand Down

0 comments on commit e364c16

Please sign in to comment.