Add Applications Form Validator

This commit is contained in:
Nicolas Le Goff
2013-06-11 12:28:23 +02:00
parent e89c502259
commit 7dad897ab7
8 changed files with 437 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
define([
"jquery",
"underscore",
"backbone",
"bootstrap",
"multiselect"
], function($, _, Backbone, bootstrap, multiselect) {
var initialize = function() {
// close alerts
$(document).on("click", ".alert .alert-block-close a", function(e){
e.preventDefault();
$(this).closest('.alert').alert('close');
return false;
});
$("select[multiple='multiple']").multiselect({
buttonWidth : "100%",
buttonClass: 'btn btn-inverse'
});
};
return {
initialize: initialize,
languagePath: '/login/language.json'
};
});

View File

@@ -0,0 +1,31 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// configure AMD loading
require.config({
baseUrl: "/scripts",
paths: {
jquery: "../include/jslibs/jquery-1.7.1",
jqueryui: "../include/jslibs/jquery-ui-1.8.17/js/jquery-ui-1.8.17.custom.min",
underscore: "../assets/underscore-amd/underscore",
backbone: "../assets/backbone-amd/backbone",
i18n: "../assets/i18next/release/i18next.amd-1.6.2.min",
bootstrap: "../skins/html5/bootstrap/js/bootstrap.min",
multiselect: "../assets/bootstrap-multiselect/js/bootstrap-multiselect"
},
shim: {
bootstrap : ["jquery"],
jqueryui: {
deps: ["jquery"]
},
multiselect: {
deps: ["jquery", "bootstrap"]
}
}
});

View File

@@ -0,0 +1,34 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require([
"jquery",
"i18n",
"apps/login/home/common",
"apps/login/home/views/form"
], function($, i18n, Common, ForgotPassWordForm) {
Common.initialize();
i18n.init({
resGetPath: Common.languagePath
}, function() {
new ForgotPassWordForm({
el : $("form[name=forgottenPasswordForm]"),
rules: [{
name: "email",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "email",
rules: "valid_email",
message: i18n.t("validation_email")
}]
});
});
});

View File

@@ -0,0 +1,34 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require([
"jquery",
"i18n",
"apps/login/home/common",
"apps/login/home/views/form"
], function($, i18n, Common, LoginForm) {
Common.initialize();
i18n.init({
resGetPath: Common.languagePath
}, function() {
new LoginForm({
el : $("form[name=loginForm]"),
rules: [{
name: "login",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "password",
rules: "required",
message: i18n.t("validation_blank")
}]
});
});
});

View File

@@ -0,0 +1,88 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// launch application
require([
"jquery",
"i18n",
"apps/login/home/common",
"apps/login/home/views/form"
], function(Common, RegisterForm) {
Common.initialize();
var fieldsConfiguration = [];
$.when.apply($, [
$.ajax({
url: '/login/registration-fields/',
success: function(config) {
fieldsConfiguration = config;
}
})
]).done(function(){
$.i18n.init({
resGetPath: Common.languagePath
}, function() {
var rules = [{
name: "email",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "email",
rules: "valid_email",
message: i18n.t("validation_email")
},{
name: "password",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "password",
rules: "min_length[5]",
message: i18n.t("validation_length_min", {
postProcess: "sprintf",
sprintf: ["5"]
})
},{
name: "passwordConfirm",
rules: "matches[password]",
message: i18n.t("password_match")
},{
name: "accept-tou",
rules: "required",
message: i18n.t("accept_tou"),
type: "checkbox"
},{
name: "collections[]",
rules: "min_length[1]",
message: i18n.t("validation_choice_min", {
postProcess: "sprintf",
sprintf: ["1"]
}),
type: "multiple"
}];
_.each(fieldsConfiguration, function(field) {
if (field.required) {
var rule = {
"name": field.name,
"rules": "required",
"message": i18n.t("validation_blank")
};
rules.push(rule);
}
});
new RegisterForm({
el : $("form[name=registerForm]"),
rules: rules
});
});
});
});

View File

@@ -0,0 +1,88 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
// launch application
require([
"jquery",
"i18n",
"apps/login/home/common",
"apps/login/home/views/form"
], function($, i18n, Common, RegisterForm) {
Common.initialize();
var fieldsConfiguration = [];
$.when.apply($, [
$.ajax({
url: '/login/registration-fields/',
success: function(config) {
fieldsConfiguration = config;
}
})
]).done(function(){
i18n.init({
resGetPath: Common.languagePath
}, function() {
var rules = [{
name: "email",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "email",
rules: "valid_email",
message: i18n.t("validation_email")
},{
name: "password",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "password",
rules: "min_length[5]",
message: i18n.t("validation_length_min", {
postProcess: "sprintf",
sprintf: ["5"]
})
},{
name: "passwordConfirm",
rules: "matches[password]",
message: i18n.t("password_match")
},{
name: "accept-tou",
rules: "required",
message: i18n.t("accept_tou"),
type: "checkbox"
},{
name: "collections[]",
rules: "min_length[1]",
message: i18n.t("validation_choice_min", {
postProcess: "sprintf",
sprintf: ["1"]
}),
type: "multiple"
}];
_.each(fieldsConfiguration, function(field) {
if (field.required) {
var rule = {
"name": field.name,
"rules": "required",
"message": i18n.t("validation_blank")
};
rules.push(rule);
}
});
new RegisterForm({
el : $("form[name=registerForm]"),
rules: rules
});
});
});
});

View File

@@ -0,0 +1,41 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require([
"jquery",
"i18n",
"apps/login/home/common",
"apps/login/home/views/form"
], function($, i18n, Common, RenewPassword) {
Common.initialize();
i18n.init({
resGetPath: Common.languagePath
}, function() {
new RenewPassword({
el : $("form[name=passwordRenewForm]"),
rules: [{
name: "password",
rules: "required",
message: i18n.t("validation_blank")
},{
name: "password",
rules: "min_length[5]",
message: i18n.t("validation_length_min", {
postProcess: "sprintf",
sprintf: ["5"]
})
},{
name: "passwordConfirm",
rules: "matches[password]",
message: i18n.t("password_match")
}]
});
});
});

View File

@@ -0,0 +1,86 @@
/*
* This file is part of Phraseanet
*
* (c) 2005-2013 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
define([
"jquery",
"underscore",
"backbone",
"bootstrap",
"common/validator",
"apps/login/home/views/inputView"
], function($, _, Backbone, bootstrap, Validator, InputView) {
var RegisterForm = Backbone.View.extend({
events: {
"submit": "onSubmit"
},
initialize: function(options) {
var self = this;
var rules = [];
if (options) {
rules = options.rules || [];
}
// get a new validator defined rules
this.validator = new Validator(rules);
this.inputViews = {};
// creates input views for each input
_.each(this.$el.serializeArray(), function (input) {
self._addInputView(input);
});
},
onSubmit: function (event) {
var self = this;
// reset previous errors in the view
this._resetInputErrors();
// validate new values
this.validator.validate(this.$el.serializeArray());
if (this.validator.hasErrors()) {
// cancel submit
event.preventDefault();
// group errors by input
_.each(_.groupBy(this.validator.getErrors(), function(error){
return error.name;
}), function (errors, name) {
// show new errors in the views
if (name in self.inputViews) {
self.inputViews[name].showErrors(errors);
} else {
// Because serialize array do not serialize non checked input
// We must create view for errored input name on the fly
var input = {"name": name};
self._addInputView(input);
self.inputViews[name].showErrors(errors);
}
});
}
},
_resetInputErrors: function() {
_.each(this.inputViews, function(view) {
view.resetErrors();
});
},
_addInputView: function(input) {
var name = input.name;
this.inputViews[name] = new InputView({
name: name,
el : $("input[name='"+name+"'], select[name='"+name+"'], textarea[name='"+name+"']", this.$el).first().closest("div")
});
return this;
}
});
return RegisterForm;
});