Skip to content

Commit 124526b

Browse files
author
Nils Henning
committed
[FEATURE] error configuration on a per form and per field basis
1 parent a1f6b0f commit 124526b

File tree

8 files changed

+438
-70
lines changed

8 files changed

+438
-70
lines changed

.byebug_history

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,63 @@
11
continue
2+
page.html
3+
continue
4+
!(@included_config[:errors] == false && (errors == false || errors.nil?) || errors == false)
5+
continue
6+
!(@included_config[:errors] == false && (errors == false || errors.nil?) || errors == false)
7+
@included_config[:errors] == false && (errors == false || errors.nil?) || errors == false
8+
continue
9+
@included_config[:errors] == false && (errors == false || errors.nil?) || errors == false
10+
@included_config[:errors] == false && errors == false || errors == false
11+
@included_config[:errors] == false || errors == false
12+
@included_config[:errors] == false
13+
@included_config[:errors]
14+
@included_config[:errors] != false && errors.present?
15+
errors != false
16+
false.present?
17+
errors != false
18+
|| errors != false
19+
@included_config[:errors] != false && errors.present?
20+
@included_config[:errors] != false && errors
21+
@included_config[:errors] != false
22+
(@included_config[:errors])
23+
errors != false
24+
errors
25+
(@included_config[:errors] != false && errors.present?)
26+
continue
27+
(@included_config[:errors] != false && errors.present?)
28+
@included_config[:errors]
29+
errors
30+
(@included_config[:errors] != false && errors.present?) || errors != false
31+
continue
32+
(@included_config[:errors] != false && errors.present?) || errors != false
33+
continue
34+
nil != false
35+
(@included_config[:errors] == false && !errors.present?)
36+
(@included_config[:errors])
37+
(@included_config[:errors] == false && !errors.present?)
38+
continue
39+
page.html
40+
continue
41+
hash[:errors]
42+
hash.is_a?(Hash)
43+
hash.keys
44+
hash
45+
keys
46+
continue
47+
save_screenshot
48+
page.html
49+
continue
50+
save_screenshot
51+
page.html
52+
continue
53+
save_screenshot
54+
page.html
55+
continue
56+
page.methods
57+
page.errors
58+
page.html
59+
save_screenshot
60+
continue
261
save_screenshot
362
page.html
463
continue
@@ -195,62 +254,3 @@ self
195254
continue
196255
self
197256
continue
198-
self
199-
continue
200-
self
201-
continue
202-
page.html
203-
continue
204-
save_screenshot
205-
continue
206-
save_screenshot
207-
continue
208-
path
209-
::Rails.application.routes.url_helpers.send(property_path, params)
210-
property_path.is_a? Symbol
211-
property_path
212-
continue
213-
save_screenshot
214-
continue
215-
save_screenshot
216-
continue
217-
page.html
218-
continue
219-
save_screenshot
220-
page.html
221-
continue
222-
save_screenshot
223-
page.html
224-
continue
225-
page.html
226-
continue
227-
save_screenshot
228-
continue
229-
save_screenshot
230-
continue
231-
save_screenshot
232-
continue
233-
prop.flatten
234-
prop
235-
continue
236-
prop
237-
continue
238-
prop.flatten
239-
prop.flatten!
240-
prop.flatten
241-
prop.keys.first
242-
prop.keys
243-
prop
244-
prop.flatten
245-
prop
246-
prop.key
247-
prop.values.first[:alias]
248-
prop.values.first[:alias
249-
prop.values.first
250-
prop.values
251-
prop.first
252-
prop[:alias]
253-
prop.is_a? Hash
254-
self.class.requires_properties
255-
continue
256-
self.class.requires_properties

app/concepts/matestack/ui/core/form/has_errors.rb

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def error_key
1313
# error partial
1414
# overwrite render_errors to customize error rendering
1515
def render_errors
16-
unless errors == false
16+
unless @included_config[:errors] == false && (errors == false || errors.nil?) || errors == false
1717
self.send(wrapper_tag, class: wrapper_class, attributes: { 'v-if': error_key }) do
1818
self.send(error_tag, class: error_class, attributes: { 'v-for': "error in #{error_key}" }) do
1919
plain '{{ error }}'
@@ -23,23 +23,32 @@ def render_errors
2323
end
2424

2525
def wrapper_tag
26-
tag = errors.is_a?(Hash) ? errors.dig(:wrapper, :tag) : :span
27-
tag || :span
26+
tag = get_error_config(@included_config[:errors], :wrapper, :tag) || :span
27+
tag = get_error_config(errors, :wrapper, :tag) || tag
2828
end
2929

3030
def wrapper_class
31-
klaas = errors.is_a?(Hash) ? errors.dig(:wrapper, :class) : 'errors'
32-
klaas || 'errors'
31+
klass = get_error_config(@included_config[:errors], :wrapper, :class) || 'errors'
32+
klass = get_error_config(errors, :wrapper, :class) || klass
3333
end
3434

3535
def error_tag
36-
tag = errors.is_a?(Hash) ? errors.dig(:tag) : :span
37-
tag || :span
36+
tag = get_error_config(@included_config[:errors], :tag) || :span
37+
tag = get_error_config(errors, :tag) || tag
3838
end
3939

4040
def error_class
41-
tag = errors.is_a?(Hash) ? errors.dig(:class) : 'error'
42-
tag || 'error'
41+
klass = get_error_config(@included_config[:errors], :class) || 'error'
42+
klass = get_error_config(errors, :class) || klass
43+
end
44+
45+
def input_error_class
46+
klass = get_error_config(@included_config[:errors], :input, :class) || 'error'
47+
klass = get_error_config(errors, :input, :class) || klass
48+
end
49+
50+
def get_error_config(hash, *keys)
51+
hash.dig(*keys) if hash.is_a?(Hash) && hash.dig(*keys)
4352
end
4453

4554
end

app/concepts/matestack/ui/core/form/input/input.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def vue_attributes
2222
(options[:attributes] || {}).merge({
2323
"@change": change_event,
2424
ref: "input.#{attr_key}",
25-
'init-value': init_value
25+
'init-value': init_value,
26+
'v-bind:class': "{ '#{input_error_class}': #{error_key} }"
2627
}).merge(
2728
type != :file ? { "#{v_model_type}": input_key } : {}
2829
) # file inputs are readonly, no v-model possible

app/concepts/matestack/ui/core/form/textarea/textarea.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def vue_attributes
1818
(options[:attributes] || {}).merge({
1919
'v-model': input_key,
2020
'@change': "inputChanged('#{attr_key}')",
21+
'v-bind:class': "{ '#{input_error_class}': #{error_key} }",
2122
"init-value": init_value,
2223
ref: "input.#{attr_key}",
2324
})

docs/components/form.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,10 +842,12 @@ form_input key: :foo, type: :text, errors: false
842842

843843
##### Customizing default rendering
844844

845-
You can customize errors by passing `errors` with a hash.
845+
You can customize errors by passing `errors` with a hash to a input, textarea, select component or to a form component.
846846
Customize the error tag by including a `tag` key with a symbol representing a html tag and the error class by including a `class`
847847
key with a string.
848848

849+
Configuring errors on a per form field basis
850+
849851
```ruby
850852
form_input key: :foo, type: :text, errors: { tag: :div, class: 'my-error' }
851853
```
@@ -868,6 +870,38 @@ Outputs errors as:
868870
</div>
869871
```
870872

873+
Configuring errors on a per form basis. Per form field configs take precedence over the form config.
874+
875+
```ruby
876+
def response
877+
form form_config do
878+
form_input key: :foo, type: :text
879+
form_input key: :bar, type: :text, errors: false
880+
end
881+
end
882+
883+
def form_config
884+
{
885+
for: :my_model,
886+
#[...]
887+
errors: {
888+
wrapper: { tag: :div, class: 'my-errors' },
889+
input: { class: 'my-error' }
890+
tag: :div,
891+
class: 'my-field-error'
892+
}
893+
}
894+
```
895+
Outputs errors as:
896+
```html
897+
<input type="text" class="my-field-error" />
898+
<div class="my-errors">
899+
<div class="my-error">seems to be invalid</span>
900+
</div>
901+
<input type="text" class="my-field-error" /> <!-- without any errors, because its config takes precedence over the form config -->
902+
```
903+
904+
871905
### Example 9: Mapping the form to an Active Record Model
872906

873907
To test the mapping of our form to an Active Record Model, we make sure our `TestModel`'s description can't be empty:

0 commit comments

Comments
 (0)