rapscallion 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +28 -6
- data/app/assets/javascripts/rapscallion/validations.js +100 -73
- data/lib/rapscallion/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1eb9596f7264fe5da4e2fcd9e84bdbdaba00d03
|
4
|
+
data.tar.gz: 9e4a2f9cfdf540669207c7744e78a3620740da4b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34efc3abee76681704f75a43d0a60878a6915cde9782dbf8ef119263a492373f43084e5f623fbd0ee6e764fa548bafdb1377a1f8bc7c13d0c8767345d03ed302
|
7
|
+
data.tar.gz: 6e3688cddf645cd664d1ba688baf67edc067bdef7b1b5a81b99040723c22399a049e22102fcba0afa57ac12da88985e6a34f690f418b1fd7b58a06bdb3581039
|
data/README.md
CHANGED
@@ -1,34 +1,56 @@
|
|
1
|
-
|
1
|
+
Rapscallion
|
2
2
|
===========
|
3
3
|
|
4
4
|
Ruby on Rails client side validations
|
5
5
|
|
6
|
-
application.js:
|
6
|
+
### Add Rapscallion to application.js:
|
7
7
|
|
8
8
|
``` javascript
|
9
9
|
//= require rapscallion/validations
|
10
10
|
```
|
11
11
|
|
12
|
-
|
12
|
+
### Activate in Javascript:
|
13
|
+
$(selector).rapscallion();
|
14
|
+
|
15
|
+
#### Options:
|
16
|
+
|
17
|
+
- **Change class of error message div**
|
18
|
+
error_message_container_class: "error_messages"
|
19
|
+
|
20
|
+
- **Set a class on field with error**
|
21
|
+
field_with_error_class: 'has_error'
|
22
|
+
|
23
|
+
- **Set a class on field with success**
|
24
|
+
field_valid_class: 'is_valid'
|
25
|
+
|
26
|
+
- **Container for input – used to add and remove error messages**
|
27
|
+
field_container: 'div.input'
|
28
|
+
|
29
|
+
- **Event that triggers validation**
|
30
|
+
trigger: 'blur'
|
31
|
+
|
32
|
+
eg $('.rapscallion').rapscallion({error_message_container_class: 'errors', field_container: '.field'})
|
33
|
+
|
34
|
+
### Add Rapscallion to Gemfile:
|
13
35
|
|
14
36
|
``` ruby
|
15
37
|
gem 'rapscallion'
|
16
38
|
```
|
17
39
|
|
18
|
-
View example with Simple Form:
|
40
|
+
### View example of input tag with Simple Form:
|
19
41
|
|
20
42
|
``` haml
|
21
43
|
.input
|
22
44
|
= f.input :username, input_html: {class: 'rapscallion'}
|
23
45
|
```
|
24
46
|
|
25
|
-
|
47
|
+
### Form tag when validating an existing record (for example to avoid uniqueness validation problems):
|
26
48
|
|
27
49
|
``` haml
|
28
50
|
= simple_form_for @thing, html: {data: {existing_record: @thing.id}} do |f|
|
29
51
|
```
|
30
52
|
|
31
|
-
Model:
|
53
|
+
### Model:
|
32
54
|
|
33
55
|
``` ruby
|
34
56
|
class User < ActiveRecord::Base
|
@@ -1,83 +1,110 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
var settings = {
|
4
|
-
|
5
|
-
/* error messages will be shown in a div set the class for that here */
|
6
|
-
error_message_container_class: "error_messages",
|
7
|
-
|
8
|
-
/* fields with errors will get a class to indicate errors, change that class name here */
|
9
|
-
field_with_error_class: 'has_error',
|
1
|
+
/*
|
10
2
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
3
|
+
Rapscallion
|
4
|
+
Ruby on Rails Client Side Validations
|
5
|
+
Author: Gordon B. Isnor
|
6
|
+
http://www.github.com/gordonbisnor/rapscallion
|
7
|
+
http://blog.isnorcreative.com/2014/06/10/rapscallion.html
|
16
8
|
|
17
|
-
|
18
|
-
|
19
|
-
/* the input in question */
|
20
|
-
var el = $(this);
|
21
|
-
|
22
|
-
/* the form */
|
23
|
-
var form = el.closest('form');
|
24
|
-
|
25
|
-
/* model reference from field name eg user from user[username] */
|
26
|
-
var klass = el.prop('name').replace(/\[.*\]/,'');
|
27
|
-
|
28
|
-
/* field name eg username from user[username] */
|
29
|
-
var attr = el.prop('id').replace(klass+"_",'');
|
30
|
-
|
31
|
-
/* field value */
|
32
|
-
var field_val = el.val();
|
33
|
-
|
34
|
-
/* set up basic hash for validation */
|
35
|
-
validation_data = {klass: klass, attr: attr, field_val: field_val}
|
36
|
-
|
37
|
-
/*
|
38
|
-
confirmation fields are special cases, and add the field to confirm for
|
39
|
-
eg password and password_confirmation
|
40
|
-
*/
|
41
|
-
if(attr.match(/.*_confirmation/) != undefined){
|
42
|
-
var confirmation_field = attr.replace('_confirmation','');
|
43
|
-
var confirmation_value = form.find('#' + klass + "_" + confirmation_field).val();
|
44
|
-
};
|
45
|
-
|
46
|
-
validation_data[confirmation_field] = confirmation_value;
|
9
|
+
*/
|
47
10
|
|
48
|
-
|
49
|
-
if(form.data('existing-record') != undefined) {
|
50
|
-
validation_data['existing_record'] = form.data('existing-record');
|
51
|
-
};
|
11
|
+
(function($){
|
52
12
|
|
53
|
-
|
54
|
-
$.ajax({
|
55
|
-
type: "POST",
|
56
|
-
url: '/rapscallion',
|
57
|
-
dataType: 'json',
|
58
|
-
data: validation_data,
|
13
|
+
$.fn.rapscallion = function(options){
|
59
14
|
|
60
|
-
|
15
|
+
var settings = $.extend({
|
16
|
+
|
17
|
+
/* error messages will be shown in a div set the class for that here */
|
18
|
+
error_message_container_class: "error_messages",
|
19
|
+
|
20
|
+
/* fields with errors will get a class to indicate errors, change that class name here */
|
21
|
+
field_with_error_class: 'has_error',
|
22
|
+
|
23
|
+
/* valid fields will get a class to indicate success, change that class name here */
|
24
|
+
field_valid_class: 'is_valid',
|
61
25
|
|
62
|
-
/* remove
|
63
|
-
|
64
|
-
|
65
|
-
/*
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
26
|
+
/* error messages will be remove from input container, eg closest(:selector), set that here */
|
27
|
+
field_container: 'div.input',
|
28
|
+
|
29
|
+
/* event that triggers validation */
|
30
|
+
trigger: 'blur'
|
31
|
+
|
32
|
+
}, options);
|
33
|
+
|
34
|
+
return this.each(function() {
|
70
35
|
|
71
|
-
|
72
|
-
/* cycle through json response adding errors for each */
|
73
|
-
$.each( data, function( key, val ) {
|
74
|
-
el.after('<div class="' + settings.error_message_container_class + '">' + val + '</div>');
|
75
|
-
el.removeClass(settings.field_valid_class).addClass(settings.field_with_error_class)
|
76
|
-
});
|
77
|
-
}; /* end if errors or not */
|
36
|
+
$(this).on(settings.trigger, function(){
|
78
37
|
|
79
|
-
|
38
|
+
/* the input in question */
|
39
|
+
var el = $(this);
|
40
|
+
|
41
|
+
/* the form */
|
42
|
+
var form = el.closest('form');
|
43
|
+
|
44
|
+
/* model reference from field name eg user from user[username] */
|
45
|
+
var klass = el.prop('name').replace(/\[.*\]/,'');
|
46
|
+
|
47
|
+
/* field name eg username from user[username] */
|
48
|
+
var attr = el.prop('id').replace(klass+"_",'');
|
49
|
+
|
50
|
+
/* field value */
|
51
|
+
var field_val = el.val();
|
52
|
+
|
53
|
+
/* set up basic hash for validation */
|
54
|
+
validation_data = {klass: klass, attr: attr, field_val: field_val}
|
80
55
|
|
81
|
-
|
56
|
+
/*
|
57
|
+
confirmation fields are special cases, and add the field to confirm for
|
58
|
+
eg password and password_confirmation
|
59
|
+
*/
|
60
|
+
if(attr.match(/.*_confirmation/) != undefined){
|
61
|
+
var confirmation_field = attr.replace('_confirmation','');
|
62
|
+
var confirmation_value = form.find('#' + klass + "_" + confirmation_field).val();
|
63
|
+
};
|
82
64
|
|
83
|
-
|
65
|
+
validation_data[confirmation_field] = confirmation_value;
|
66
|
+
|
67
|
+
/* existing record? */
|
68
|
+
if(form.data('existing-record') != undefined) {
|
69
|
+
validation_data['existing_record'] = form.data('existing-record');
|
70
|
+
};
|
71
|
+
|
72
|
+
/* do ajax request */
|
73
|
+
$.ajax({
|
74
|
+
type: "POST",
|
75
|
+
url: '/rapscallion',
|
76
|
+
dataType: 'json',
|
77
|
+
data: validation_data,
|
78
|
+
|
79
|
+
success: function(data, status, xhr) {
|
80
|
+
|
81
|
+
/* remove existing error messages */
|
82
|
+
el.closest(settings.field_container).find('.'+settings.error_message_container_class).remove();
|
83
|
+
|
84
|
+
/* if no errors present field is valid, add is_valid css class */
|
85
|
+
if(data.length == 0) {
|
86
|
+
el.removeClass(settings.field_with_error_class).addClass(settings.field_valid_class);
|
87
|
+
|
88
|
+
/* if error present field is not valid, add has_error class to input, and error messages after input */
|
89
|
+
|
90
|
+
} else {
|
91
|
+
/* cycle through json response adding errors for each */
|
92
|
+
$.each( data, function( key, val ) {
|
93
|
+
el.after('<div class="' + settings.error_message_container_class + '">' + val + '</div>');
|
94
|
+
el.removeClass(settings.field_valid_class).addClass(settings.field_with_error_class)
|
95
|
+
});
|
96
|
+
}; /* end if errors or not */
|
97
|
+
|
98
|
+
} /* end success */
|
99
|
+
|
100
|
+
}); /* end ajax */
|
101
|
+
|
102
|
+
|
103
|
+
});
|
104
|
+
|
105
|
+
|
106
|
+
});
|
107
|
+
|
108
|
+
|
109
|
+
};
|
110
|
+
}(jQuery));
|
data/lib/rapscallion/version.rb
CHANGED