Permalink
Browse files

Issue 7 : Testing submit_callback with the new checkboxes demo

We still have a sequencing problem with this, the submit callback can't
clear errors before the regular validators run. We may need to shift
this around.  Additionally, the showFormErrors and showFormSuccess have
been moved. I'm not a huge fan of name spacing this way, but it does
work.
  • Loading branch information...
1 parent 528b087 commit f5257f03de458bb3f7a2261349b6c64ecfab6bae @LearningStation LearningStation committed Aug 6, 2011
Showing with 189 additions and 64 deletions.
  1. +0 −1 .gitignore
  2. +116 −0 demos/checkboxes.html
  3. +67 −57 js/uni-form-validation.jquery.js
  4. +6 −6 js/uni-form-validation.jquery.min.js
View
@@ -4,4 +4,3 @@ demos/dev.html
build.sh
js/min.header.js
.DS_Store
-demos/checkboxes.html
View
@@ -0,0 +1,116 @@
+<!doctype html>
+<html lang="en-us" dir="ltr">
+ <head>
+ <meta charset="utf-8">
+<!--
+
+ Copyright (c) 2010, Dragan Babic
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
+
+-->
+ <title>Uni&ndash;Form</title>
+
+ <link href="../css/uni-form.css" media="screen" rel="stylesheet"/>
+ <link href="../css/default.uni-form.css" title="Default Style" media="screen" rel="stylesheet"/>
+ <link href="./css/demo.css" media="screen" rel="stylesheet"/>
+
+ <!--[if lte ie 7]>
+ <style type="text/css" media="screen">
+ /* Move these to your IE6/7 specific stylesheet if possible */
+ .uniForm, .uniForm fieldset, .uniForm .ctrlHolder, .uniForm .formHint, .uniForm .buttonHolder, .uniForm .ctrlHolder ul{ zoom:1; }
+ </style>
+ <![endif]-->
+
+ </head>
+
+ <body>
+
+ <h1><a href="http://sprawsm.com/uni-form/" title="visit Uni–Form online"><img src="../img/uni-form-logo.png" alt=""/></a></h1>
+
+ <form action="#" class="uniForm">
+
+ <fieldset>
+ <h3>Checkbox validation example with custom callback</h3>
+
+ <div class="ctrlHolder" id="color_select">
+ <p class="label">Favorite colors</p>
+ <ul>
+ <li><label for="color_1"><input type="checkbox" name="color[]" id="color_1" class="color_selection" value="blue"> blue</label></li>
+ <li><label for="color_2"><input type="checkbox" name="color[]" id="color_2" class="color_selection" value="red"> red</label></li>
+ <li><label for="color_3"><input type="checkbox" name="color[]" id="color_3" class="color_selection" value="purple"> purple</label></li>
+ <li><label for="color_4"><input type="checkbox" name="color[]" id="color_4" class="color_selection" value="brown"> brown</label></li>
+ <li><label for="color_5"><input type="checkbox" name="color[]" id="color_5" class="color_selection" value="pink"> pink</label></li>
+ <li><label for="color_6"><input type="checkbox" name="color[]" id="color_6" class="color_selection" value="navy"> navy</label></li>
+ </ul>
+ <p class="formHint">Please select two.</p>
+ </div>
+
+ </fieldset>
+
+ <div class="buttonHolder">
+ <button type="submit" class="secondaryAction">← Cancel and go back</button>
+ <button type="submit" class="primaryAction">Submit</button>
+ </div>
+
+ </form>
+
+ <div id="footer">
+ <p>Uni-Form is created by <a href="http://sprawsm.com/" title="Dragan is the founder of Superawesome">Dragan Babić</a> and is licensed under the MIT license.</p>
+ </div>
+
+
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
+ <script type="text/javascript" src="../js/uni-form-validation.jquery.js" charset="utf-8"></script>
+ <script type="text/javascript">
+ /**
+ * Check that at least two checkboxes have been selected
+ *
+ * @param object form jQuery object of the form
+ *
+ * return false on error
+ */
+ my_submit_callback = function(form) {
+
+ // Are at least two selectd?
+ if ($('input.color_selection:checked').length < 2) {
+ // show the error
+ $('form.uniForm').uniform.showFormError(
+ form,
+ 'Oops, there was an error',
+ ['Please select at least two colors from the favorite colors']
+ );
+ return false;
+ }
+ return true;
+ }
+
+ $(function(){
+ $('form.uniForm').uniform({
+ prevent_submit : true,
+ submit_callback : my_submit_callback
+ });
+ });
+
+ </script>
+ </body>
+</html>
@@ -417,61 +417,6 @@ jQuery.fn.uniform = function (extended_settings) {
return out;
};
- /**
- * Uni-Form form validation error
- *
- * @param string title of the form error
- * @param array list of error messages to show
- *
- * @return false
- */
- var showFormError = function (form, title, messages) {
- var m, $message;
-
- if ($('#errorMsg').length) {
- $('#errorMsg').remove();
- }
-
- $message = $('<div />')
- .attr('id', 'errorMsg')
- .html("<h3>" + title + "</h3>");
-
- if (messages.length) {
- $message.append($('<ol />'));
- for (m in messages) {
- $('ol', $message).append(
- $('<li />').text(messages[m])
- );
- }
- }
-
- form.prepend($message);
-
- $('html, body').animate({
- scrollTop: form.offset().top
- }, 500);
-
- $('#errorMsg').slideDown();
- return false;
- };
-
- var showFormSuccess = function (form, title) {
- var $message;
-
- if ($('#okMsg').length) {
- $('#okMsg').remove();
- }
- $message = $('<div />')
- .attr('id', 'okMsg')
- .html("<h3>" + title + "</h3>");
- form.prepend($message);
- $('html, body').animate({
- scrollTop: form.offset().top
- }, 500);
- $('#okMsg').slideDown();
- return false;
- };
-
return this.each(function () {
var form = jQuery(this);
@@ -606,7 +551,7 @@ jQuery.fn.uniform = function (extended_settings) {
) {
return_val = ($.isFunction(settings.prevent_submit_callback))
? settings.prevent_submit_callback(form, i18n('submit_msg'), [i18n('submit_help')])
- : showFormError(form, i18n('submit_msg'), [i18n('submit_help')]);
+ : jQuery.fn.uniform.showFormError(form, i18n('submit_msg'), [i18n('submit_help')]);
} // end form error counting
// if we have a submit callback, we'll give it a chance to inspect the data now
@@ -737,7 +682,72 @@ jQuery.fn.uniform = function (extended_settings) {
// the manually run the focus event here to deal with style changes
// and any handling of the default data
$('input[autofocus]:first').focus();
- });
+
+ }); // end for each form
+};
+
+/**
+ * Uni-Form form validation error
+ *
+ * @param object jQuery form element
+ * @param string title of the form error
+ * @param array list of error messages to show
+ *
+ * @return false
+ */
+jQuery.fn.uniform.showFormError = function (form, title, messages) {
+ var m, $message;
+
+ if ($('#errorMsg').length) {
+ $('#errorMsg').remove();
+ }
+
+ $message = $('<div />')
+ .attr('id', 'errorMsg')
+ .html("<h3>" + title + "</h3>");
+
+ if (messages.length) {
+ $message.append($('<ol />'));
+ for (m in messages) {
+ $('ol', $message).append(
+ $('<li />').text(messages[m])
+ );
+ }
+ }
+
+ form.prepend($message);
+
+ $('html, body').animate({
+ scrollTop: form.offset().top
+ }, 500);
+
+ $('#errorMsg').slideDown();
+ return false;
+};
+
+/**
+ * Uni-Form form success message
+ *
+ * @param object jQuery form element
+ * @param string title of the success message
+ *
+ * @return false
+ */
+jQuery.fn.uniform.showFormSuccess = function (form, title) {
+ var $message;
+
+ if ($('#okMsg').length) {
+ $('#okMsg').remove();
+ }
+ $message = $('<div />')
+ .attr('id', 'okMsg')
+ .html("<h3>" + title + "</h3>");
+ form.prepend($message);
+ $('html, body').animate({
+ scrollTop: form.offset().top
+ }, 500);
+ $('#okMsg').slideDown();
+ return false;
};
/**

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit f5257f0

Please sign in to comment.