rails-ujs-form 0.1.3 → 0.2.0
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.
@@ -42,6 +42,16 @@ var methods = {
|
|
42
42
|
return $field.parent('.field_with_errors').length > 0;
|
43
43
|
},
|
44
44
|
|
45
|
+
clear_errors: function() {
|
46
|
+
var $form = this;
|
47
|
+
this.rails_form('fields').each(function() {
|
48
|
+
$form.rails_form('clear_error', $(this));
|
49
|
+
});
|
50
|
+
|
51
|
+
// clear base errors too
|
52
|
+
this.find('.errors').html('');
|
53
|
+
},
|
54
|
+
|
45
55
|
clear_error: function($field) {
|
46
56
|
var id = $field.attr('id');
|
47
57
|
if (this.rails_form('has_error', $field)) { $field.unwrap() }
|
@@ -50,24 +60,59 @@ var methods = {
|
|
50
60
|
.unwrap()
|
51
61
|
.next('.formError').remove();
|
52
62
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
add_error: function($field, error) {
|
57
|
-
var id = $field.attr('id');
|
58
|
-
$field.wrap('<div class="field_with_errors">');
|
59
|
-
this.find('label[for=' + id + ']')
|
60
|
-
.after('<div class="formError">' + error + '</div>')
|
61
|
-
.wrap('<div class="field_with_errors">');
|
63
|
+
// remove from a .errors ul
|
64
|
+
this.find('.errors [data-for=' + id + ']').remove();
|
62
65
|
|
63
66
|
return this;
|
64
67
|
},
|
65
68
|
|
66
69
|
// display standard errors as returned by JSON
|
67
70
|
set_errors: function(errors) {
|
71
|
+
this.rails_form('clear_errors');
|
68
72
|
for (var name in errors) {
|
69
|
-
|
73
|
+
for (var i in errors[name]) {
|
74
|
+
this.rails_form('add_error', name, errors[name][i]);
|
75
|
+
}
|
70
76
|
}
|
77
|
+
return this;
|
78
|
+
},
|
79
|
+
|
80
|
+
add_error: function(name, error) {
|
81
|
+
var $field = this.rails_form('fields').filter('[name*=' + name + ']');
|
82
|
+
$field.filter('.field_with_errors > *').unwrap();
|
83
|
+
$field.wrap('<div class="field_with_errors">');
|
84
|
+
|
85
|
+
// if there is a field, and it has a label, show the error after it
|
86
|
+
if ($field.length) {
|
87
|
+
var id = $field.attr('id');
|
88
|
+
var $label = this.rails_form('label_for', $field);
|
89
|
+
var $error = $label.parent().next('.formError');
|
90
|
+
|
91
|
+
if ($error.length) {
|
92
|
+
$error.text(function(i, text) {
|
93
|
+
return text + ', ' + error;
|
94
|
+
});
|
95
|
+
} else {
|
96
|
+
$label.after('<div class="formError">' + error + '</div>')
|
97
|
+
.wrap('<div class="field_with_errors">');
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
// if there is an .errors list, show the error there
|
102
|
+
var $errors = this.find('ul.errors');
|
103
|
+
if ($errors.length) {
|
104
|
+
var message = name.charAt(0).toUpperCase() + name.substring(1).replace('_', ' ') +
|
105
|
+
' ' + error;
|
106
|
+
|
107
|
+
var $li = $('<li>' + message + '</li>');
|
108
|
+
|
109
|
+
if ($field.length) {
|
110
|
+
$li.attr('data-for', $field.attr('id'));
|
111
|
+
}
|
112
|
+
|
113
|
+
$errors.append($li);
|
114
|
+
}
|
115
|
+
|
71
116
|
return this;
|
72
117
|
}
|
73
118
|
};
|
@@ -7,14 +7,14 @@
|
|
7
7
|
// if data is html, we replace this content of the form with the content
|
8
8
|
// of the form that we've just received back
|
9
9
|
var contentType = request.getResponseHeader('Content-Type');
|
10
|
-
if (
|
10
|
+
if (/html/.test(contentType)) {
|
11
11
|
// wrap in a div incase there are a bunch of floating elements, pull the form out
|
12
12
|
var $new_form = $('<div>' + request.responseText + '</div>').find('#' + $(this).attr('id'));
|
13
13
|
$(this).html($new_form.html());
|
14
14
|
|
15
|
-
} else if (
|
15
|
+
} else if (/json/.test(contentType)) {
|
16
16
|
// we will be receiving an error object back, we can pass it straight into rails_form.js
|
17
|
-
this.rails_form('set_errors', $.parseJSON(request.responseText));
|
17
|
+
$(this).rails_form('set_errors', $.parseJSON(request.responseText));
|
18
18
|
} else {
|
19
19
|
throw "ujs-form/show-errors: Don't know how to handle dataType " + request.dataType;
|
20
20
|
}
|
data/lib/rails-ujs-form.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
module RailsUjsForm
|
2
|
+
module Helper
|
3
|
+
# not sure why there isn't something like this in rails
|
4
|
+
#
|
5
|
+
# render the errors on an object in a fairly standard way:
|
6
|
+
#
|
7
|
+
# <ul class="errors">
|
8
|
+
# <li data-for="name">Name cannot be blank</li>
|
9
|
+
# </ul>
|
10
|
+
def render_errors(builder)
|
11
|
+
errors = builder.object.errors
|
12
|
+
messages = errors.full_messages
|
13
|
+
content_tag :ul, :class => :errors do
|
14
|
+
output = ''
|
15
|
+
errors.each_with_index do |error, i|
|
16
|
+
output << content_tag(:li, :'data-for' => "#{builder.object_name}_#{error[0]}") { messages[i] }
|
17
|
+
end
|
18
|
+
output.html_safe
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-ujs-form
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tom Coleman
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-12-02 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: jquery-rails
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- app/assets/javascripts/ujs-form/rails_form.js
|
47
47
|
- app/assets/javascripts/ujs-form/show-errors.js
|
48
48
|
- lib/rails-ujs-form.rb
|
49
|
+
- lib/rails-ujs-form/helper.rb
|
49
50
|
- lib/rails-ujs-form/version.rb
|
50
51
|
- rails-ujs-form.gemspec
|
51
52
|
homepage:
|