Skip to content

Commit

Permalink
Refactored so that our customed validations are stored in one place n…
Browse files Browse the repository at this point in the history
…eed to refactor the rest of the forms to use the new customised plugin
  • Loading branch information
baphled committed Jun 19, 2010
1 parent 5fa6736 commit 45a52e9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/views/projects/_form.html.erb
@@ -1,6 +1,6 @@
<% content_for :js_head do %>
<%= javascript_include_tag "/javascripts/validations/project" %>
<%= javascript_include_tag "/javascripts/form" %>
<%= javascript_include_tag "/javascripts/validations/project" %>
<% end %>
<%= render :partial => "/common/hover_dialog" %>
Expand Down
52 changes: 52 additions & 0 deletions public/javascripts/form.js
Expand Up @@ -20,5 +20,57 @@ $(document).ready(function() {
});
});

$.fn.customValidations = function(options) {
var opts = $.extend({}, $.fn.animateIconPanel.defaults, options);

$(this).validate({
ignoreTitle: true,
highlight: function(element, errorClass, validClass) {
$(element).addClass(errorClass).removeClass(validClass);
$(element).fadeOut(function() {
var $parent = $(element).parent();
var $validElements = $parent.find('span.valid');
$validElements.remove();

$parent.addClass(errorClass).removeClass(validClass);
$(element).fadeIn();
});
},
unhighlight: function(element, errorClass, validClass) {
var $parent = $(element).parent();
$(element).removeClass(errorClass).addClass(validClass);
$parent.removeClass(errorClass).addClass(validClass);
},
success: function(label) {
console.log(label);
var $parent = $(label).parent();
// console.log($parent.find('span'));
var $validElements = $parent.find('span.valid');
$(label).html();
$validElements.remove();
label.removeClass('error ui-state-error').addClass("valid");
},
successClass: 'valid',
errorClass: 'error ui-state-error',
errorElement: "span",
errorPlacement: function(error, element) {
var $warningSpan = $('<span></span')
.css({'float': 'left', 'margin': '0.3em'})
.addClass('ui-icon ui-icon-alert'),
$parent = element.parent(),
$previousError = $parent.find('span.error');

if ($previousError.html() == null) {
error
.wrap("<span class='ui-state-error ui-corner-all'></span>")
.insertAfter(element)
.prepend($warningSpan);
} else {
$previousError.show();
}
},
rules: opts.rules
});
}
$('fieldset.inputs > ol li').generateFormatasticTooltips();
});
32 changes: 1 addition & 31 deletions public/javascripts/validations/project.js
@@ -1,34 +1,5 @@
$(document).ready(function () {
$("form.project").validate({
highlight: function(element, errorClass) {
$(element).fadeOut(function() {
$(element).fadeIn();
$(element).addClass(errorClass);
});
},
unhighlight: function(element, errorClass) {
var $parent = $(element).parent();
$parent.find('span.error').hide();
},
successClass: 'valid',
errorClass: 'error ui-state-error',
errorElement: "span",
errorPlacement: function(error, element) {
var $warningSpan = $('<span></span')
.css({'float': 'left', 'margin': '0.3em'})
.addClass('ui-icon ui-icon-alert'),
$parent = element.parent(),
$previousError = $parent.find('span.error');

if ($previousError.html() == null) {
error
.wrap("<span class='ui-state-error ui-corner-all'></span>")
.insertAfter(element)
.prepend($warningSpan);
} else {
$previousError.show();
}
},
$("form.project").customValidations({
rules: {
"project[title]": {required: true, minlength: 3,
remote: {
Expand Down Expand Up @@ -58,5 +29,4 @@ $(document).ready(function () {
}
}
});

});

0 comments on commit 45a52e9

Please sign in to comment.