Permalink
Browse files

Name validation for all adds and edits

  • Loading branch information...
1 parent 4c89468 commit 2627afa2e50262b919da8d537ba477d03f0b75d3 @winton committed Oct 29, 2010
Showing with 82 additions and 11 deletions.
  1. +82 −11 public/js/dashboard.js
View
@@ -188,19 +188,30 @@ window.Dashboard = function(sites) {
lightbox(dialog);
$('form', dialog).submit(function() {
+ var data = currentState();
+ var category = data.category;
+ var site = data.site;
var form = $(this);
+ var name = $('#test_name').val();
var submits = $('.submit input', form);
+ var valid = true;
+
+ if (category.tests)
+ $.each(category.tests, function(i, item) {
+ if (item.name == name && item.id != id)
+ valid = false;
+ });
+ if (!valid) {
+ alert('Name already taken.');
+ return false;
+ }
$(submits[0]).attr({
'disabled': true,
'value': 'One moment...'
});
$(submits[1]).remove();
- var data = currentState();
- var category = data.category;
- var site = data.site;
-
queue.queue(function() {
$.post(
'/tests.json',
@@ -212,6 +223,7 @@ window.Dashboard = function(sites) {
});
queue.dequeue();
$('#categories .selected').click().click();
+ dialog.trigger('close');
},
'json'
);
@@ -274,6 +286,17 @@ window.Dashboard = function(sites) {
var name = $('#env_name').val();
var form = $(this);
var submits = $('.submit input', form);
+ var valid = true;
+
+ if (site.envs)
+ $.each(site.envs, function(i, item) {
+ if (item.name == name)
+ valid = false;
+ });
+ if (!valid) {
+ alert('Name already taken.');
+ return false;
+ }
$(submits[0]).attr({
'disabled': true,
@@ -332,11 +355,24 @@ window.Dashboard = function(sites) {
selectable.input.select();
selectable.form.submit(function() {
+ var container = $(this).parents('.selectable');
var data = currentState();
var site = data.site;
var name = selectable.input.val();
+ var valid = true;
var post;
-
+
+ // validate
+ container.siblings().each(function(i, item) {
+ if ($(item).text() == name)
+ valid = false;
+ });
+ if (!valid) {
+ alert("Name already exists.");
+ return false;
+ }
+
+ // post data
if (id == 'envs' || id == 'categories') {
post = { name: name, site_id: site.id };
site[id] = site[id] || [];
@@ -346,9 +382,10 @@ window.Dashboard = function(sites) {
sites.push(post);
post = $.extend({ include: [ 'envs', 'categories' ] }, post);
}
-
+
+ // create selectable
selectable = createSelectable(name);
- $(this).parents('.selectable').replaceWith(selectable.div);
+ container.replaceWith(selectable.div);
addLastClassToSelectables();
selectable.div.click();
@@ -387,19 +424,30 @@ window.Dashboard = function(sites) {
lightbox(dialog);
$('form', dialog).submit(function() {
+ var data = currentState();
+ var category = data.category;
+ var site = data.site;
var form = $(this);
+ var name = $('#test_name').val();
var submits = $('.submit input', form);
+ var valid = true;
+
+ if (category.tests)
+ $.each(category.tests, function(i, item) {
+ if (item.name == name)
+ valid = false;
+ });
+ if (!valid) {
+ alert('Name already taken.');
+ return false;
+ }
$(submits[0]).attr({
'disabled': true,
'value': 'One moment...'
});
$(submits[1]).remove();
- var data = currentState();
- var category = data.category;
- var site = data.site;
-
queue.queue(function() {
$.post(
'/tests.json',
@@ -512,6 +560,17 @@ window.Dashboard = function(sites) {
var name = $('#env_name').val();
var form = $(this);
var submits = $('.submit input', form);
+ var valid = true;
+
+ if (site.envs)
+ $.each(site.envs, function(i, item) {
+ if (item.name == name && item.id != env.id)
+ valid = false;
+ });
+ if (!valid) {
+ alert('Name already taken.');
+ return false;
+ }
$(submits[0]).attr({
'disabled': true,
@@ -560,7 +619,19 @@ window.Dashboard = function(sites) {
selectable.form.submit(function() {
var name = selectable.input.val();
var post = { name: name, _method: 'put' };
+ var valid = true;
+
+ // validate
+ selectable.div.siblings().each(function(i, item) {
+ if ($(item).text() == name)
+ valid = false;
+ });
+ if (!valid) {
+ alert("Name already exists.");
+ return false;
+ }
+ // post data
if (id == 'sites') {
data.site.name = name;
post.id = data.site.id;

0 comments on commit 2627afa

Please sign in to comment.