Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

validation started working :)

  • Loading branch information...
commit c25044bbb455f8691746d1a4d58d2b03138365ac 1 parent b808423
Dan Caragea authored

Showing 3 changed files with 63 additions and 42 deletions. Show diff stats Hide diff stats

  1. +2 0  .gitignore
  2. +6 5 js/sco.tooltip.js
  3. +55 37 js/sco.valid.js
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +
  2 +*.komodoproject
11 js/sco.tooltip.js
@@ -186,7 +186,12 @@
186 186 on_close: function() {}
187 187 };
188 188 self.leave_timeout = null;
189   - self.$target = $trigger;
  189 + if (typeof options.target !== 'undefined') {
  190 + self.$target = $(options.target);
  191 + delete options.target;
  192 + } else {
  193 + self.$target = $trigger;
  194 + }
190 195 if (typeof options.content_elem != 'undefined' && options.content_elem !== null) {
191 196 options.content = $(options.content_elem).html();
192 197 delete options.content_elem;
@@ -195,10 +200,6 @@
195 200 options.content = $trigger.attr(options.content_attr);
196 201 delete options.content_attr;
197 202 }
198   - if (typeof options.target !== 'undefined') {
199   - self.$target = $(options.target);
200   - delete options.target;
201   - }
202 203 self.$tooltip = $('<div class="tooltip"><span></span><div class="pointer"></div></div>').appendTo('body');
203 204 self.data = $.extend(true, {}, $.fn.scotip.defaults, options);
204 205 apply_data(self.data);
92 js/sco.valid.js
... ... @@ -1,29 +1,21 @@
1   -/* ==========================================================
2   - * sco.valid.js
3   - * http://github.com/terebentina/sco.js
4   - * ==========================================================
5   - * Copyright 2012 Dan Caragea.
6   - *
7   - * Licensed under the Apache License, Version 2.0 (the "License");
8   - * you may not use this file except in compliance with the License.
9   - * You may obtain a copy of the License at
10   - *
11   - * http://www.apache.org/licenses/LICENSE-2.0
12   - *
13   - * Unless required by applicable law or agreed to in writing, software
14   - * distributed under the License is distributed on an "AS IS" BASIS,
15   - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   - * See the License for the specific language governing permissions and
17   - * limitations under the License.
18   - * ========================================================== */
19   -
20   -!function($) {
  1 +(function (factory) {
21 2 "use strict";
22 3
23   - $.scovalid = function( $form, options ) {
  4 + if (typeof define === 'function' && define.amd) {
  5 + // Register as an anonymous AMD module:
  6 + define([
  7 + 'jquery',
  8 + ], factory);
  9 + } else {
  10 + // Browser globals:
  11 + factory(window.jQuery);
  12 + }
  13 +}(function($) {
  14 + "use strict";
  15 +
  16 + $.ctvalid = function( $form, options ) {
24 17 this.$form = $form;
25   - this.options = $.extend({}, $.scovalid.defaults, options);
26   - this.form_fields = this.$form.serializeArray();
  18 + this.options = $.extend({}, $.ctvalid.defaults, options);
27 19 this.allowed_rules = [];
28 20 var that = this;
29 21 $.each(this.methods, function(k,v) {
@@ -31,19 +23,32 @@
31 23 });
32 24 };
33 25
34   - $.extend($.scovalid, {
  26 + $.extend($.ctvalid, {
35 27 defaults: {
36   - debug: false
  28 + // the tag that wraps the field and possibly the label and which defines a "row" of the form
  29 + wrapper: 'label'
  30 + // array of rules to check the form against. Each value should be either a string with the name of the method to use as rule or a hash like {method: <method params>}
37 31 ,rules: {}
  32 + // custom error messages like {username: {not_empty: 'hey you forgot to enter your username', min_length: 'come on, more than 2 chars, ok?'}, password: {....}}
38 33 ,messages: {}
39 34 }
40 35
  36 +
41 37 ,prototype: {
42   - // this is the main function - it returns either true if the validation passed or an object like {field1: 'error text', field2: 'error text', ...}
  38 + // this is the main function - it returns either true if the validation passed or a hash like {field1: 'error text', field2: 'error text', ...}
43 39 validate: function() {
44 40 var errors = {}
45   - ,that = this;
46   - $.each(this.form_fields, function(idx, field) {
  41 + ,that = this
  42 + ,form_fields = this.$form.serializeArray();
  43 +
  44 + $.each(form_fields, function(idx, field) {
  45 + // remove any possible errors from previous runs
  46 + var $input = that.$form.find('[name='+field.name+']');
  47 + if ($input.siblings('span').length > 0) {
  48 + $input.siblings('span').html('');
  49 + }
  50 + $input.parents('label').removeClass('error');
  51 +
47 52 //console.log('idx', idx, value);
48 53 if (typeof that.options.rules[field.name] !== 'undefined') {
49 54 var rules = that.options.rules[field.name]
@@ -72,7 +77,19 @@
72 77 });
73 78
74 79 if (errors !== {}) {
75   - return errors;
  80 + $.each(errors, function(k, v) {
  81 + var $input = that.$form.find('[name='+k+']')
  82 + ,$span = $input.siblings('span');
  83 + if (that.options.wrapper !== null) {
  84 + $input.parents(that.options.wrapper).addClass('error');
  85 + }
  86 + if ($span.length == 0) {
  87 + $span = $('<span/>');
  88 + $input.after($span);
  89 + }
  90 + $span.html(v);
  91 + });
  92 + return false;
76 93 } else {
77 94 return true;
78 95 }
@@ -186,7 +203,7 @@
186 203 }
187 204
188 205 ,matches: function(field, value, param) {
189   - return value === this.$form.find(param).val();
  206 + return value === this.$form.find('[name='+param+']').val();
190 207 }
191 208 }
192 209
@@ -210,7 +227,7 @@
210 227 ,range: 'Please enter a value between {min} and {max}.'
211 228 ,decimal: 'Please enter a decimal number.'
212 229 ,color: ''
213   - ,matches: ''
  230 + ,matches: 'Must match the previous value.'
214 231 }
215 232
216 233 /**
@@ -240,15 +257,16 @@
240 257
241 258
242 259
243   - $.fn.scovalid = function ( options ) {
  260 + $.fn.ctvalid = function ( options ) {
244 261 var $this = this.eq(0)
245   - ,validator = $this.data('scovalid');
  262 + ,validator = $this.data('ctvalid');
246 263 if (!validator) {
247   - validator = new $.scovalid($this, options);
248   - $this.data("scovalid", validator);
  264 + validator = new $.ctvalid($this, options);
  265 + $this.data("ctvalid", validator);
249 266 }
250 267
251   - return validator.validate();
  268 + var result = validator.validate();
  269 + return result;
252 270 }
253 271
254   -}(window.jQuery);
  272 +}));

0 comments on commit c25044b

Please sign in to comment.
Something went wrong with that request. Please try again.