Commit 404ec06d authored by Rafael Ostertag's avatar Rafael Ostertag
Browse files

First iteration of InputMatcher.js.

parent 36f6eee6
......@@ -8,6 +8,7 @@ module.exports = function (grunt) {
var js_sources = [
'javascript/src/Helper/*.js',
'javascript/src/Element/*.js',
'javascript/src/Validation/*.js',
'javascript/src/*.js'
];
......
......@@ -154,8 +154,40 @@ QfqNS.Element = QfqNS.Element || {};
}
};
/**
* @public
* @param required
*/
n.FormGroup.prototype.setRequired = function (required) {
this.$element.prop('required', required);
};
/**
* @public
* @param isError
*/
n.FormGroup.prototype.setError = function (isError) {
if (isError) {
this.$formGroup.addClass("has-error has-danger");
} else {
this.$formGroup.removeClass("has-error has-danger");
}
};
n.FormGroup.prototype.setHelp = function (help) {
if (!this.hasHelpBlock()) {
return;
}
this.$helpBlock.text(help);
};
n.FormGroup.prototype.clearHelp = function () {
if (!this.hasHelpBlock()) {
return;
}
this.$helpBlock.empty();
};
})(QfqNS.Element);
\ No newline at end of file
/**
* @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
*/
/* global $ */
/* @depend ../Element/FormGroup.js */
/**
* Qfq Namespace
*
* @namespace QfqNS
*/
var QfqNS = QfqNS || {};
/**
* Qfq.Element Namespace
*
* @namespace QfqNS.Element
*/
QfqNS.Validation = QfqNS.Validation || {};
(function (n) {
'use strict';
function InputMatcher(formId) {
this.formId = formId;
this.list = [];
this.addAllElementsToList();
}
/**
* @private
*/
InputMatcher.prototype.addAllElementsToList = function () {
var $requireMatching = this.findElementsRequireMatchingOnForm();
var that = this;
$requireMatching.each(function (index, element) {
var $element = $(element);
var $slave = that.resolveCompareElements($element);
if (!$slave) {
return;
}
var masterFormGroup = that.convertElementToFormGroup($element);
var slaveFormGroup = that.convertElementToFormGroup($slave);
that.list.push({
master: masterFormGroup,
slave: slaveFormGroup
});
});
};
/**
* @private
*/
InputMatcher.prototype.findElementsRequireMatchingOnForm = function () {
return this.findOnForm('[data-compare-element]');
};
/**
* @private
*/
InputMatcher.prototype.findOnForm = function (selector) {
return $('#' + this.formId).find(selector);
};
/**
* @private
*/
InputMatcher.prototype.resolveCompareElements = function ($element) {
var $slave;
var slaveName = $element.data('compare-element');
if (!slaveName) {
return null;
}
$slave = this.findOnForm('[name=' + slaveName + ']');
if ($slave.length === 0) {
return null;
}
return $($slave[0]);
};
/**
* @private
*/
InputMatcher.prototype.convertElementToFormGroup = function ($element) {
//return new QfqNS.Element.FormGroup($element);
return QfqNS.Element.getElement($element.attr('name'));
};
InputMatcher.prototype.validate = function () {
var length = this.list.length;
var valid = true;
for (var i = 0; i < length; i++) {
var matcher = this.list[i];
if (matcher.master.getValue() !== matcher.slave.getValue()) {
matcher.slave.setError(true);
matcher.slave.setHelp("Does not match");
valid = false;
} else {
matcher.slave.setError(false);
matcher.slave.clearHelp();
}
}
return valid;
};
n.InputMatcher = InputMatcher;
})(QfqNS.Validation);
\ No newline at end of file
......@@ -14,6 +14,7 @@
</head>
<body>
<button id="validateMatchers">Validate</button>
<label>Submit to
<select name="submitTo" id="submitTo">
......@@ -142,6 +143,17 @@
console.log("Invalid event catched");
});
$('#validateMatchers').on("click", function () {
var inputMatcher = new QfqNS.Validation.InputMatcher("myForm");
if (!inputMatcher.validate()) {
console.log("Invalid");
} else {
console.log("Valid");
}
});
QfqNS.Log.level = 0;
});
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment