Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ Then use fields as tags in your views:
<:fields:text value="{{ model.first_name }}"/>
```

or

```
<:fields:text value="{{ model.first_name }}" confirm_empty_fields="false" />
```

If you use the latter, it affects behavior when a user leaves the field (`blur` event). Any value other than `false`
for `confirm_empty_fields` puts a check mark in a field that validates even if it is empty. If you use
`confirm_empty_fields="false"`, no check mark will be placed in text fields with no entry on blur.

### Select
Select fields accept either an array of options, or an array of {label: '', value: ''} hashes.

```
<:fields:select value="{{ model.role }}" options="{{ ['User', 'Admin', 'Something Else']}}"/>
```

### Radio
For radio buttons, pass an options array of {label: '', value: ''} hashes.

Expand All @@ -53,4 +63,4 @@ For checkboxes, use 'checked' instead of 'value' to bind the checkbox to a boole
<:fields:checkbox checked="{{ model.active }}"/>
```

For inline radio buttons, use ```:fields:radio:inline```.
For inline radio buttons, use ```:fields:radio:inline```.
14 changes: 13 additions & 1 deletion app/fields/controllers/main_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ class MainController < Volt::ModelController
before_action :setup_field

def setup_field
@options ||= {}

# Default to text fields
if attrs.respond_to?(:type)
@type = attrs.type
else
@type = 'text'
end

if attrs.respond_to?(:confirm_empty_fields)
@options ||= {confirm_empty_fields: 'true'} # confirm fields by default
@options[:confirm_empty_fields] = attrs.confirm_empty_fields == "false"
end

unless attrs.value_last_method
field_type = self.class.to_s.underscore.gsub(/[_]Controller$/, '')
raise "a <:fields:#{field_type} tag was used without passing a value attribute"
Expand All @@ -36,7 +43,12 @@ def errors

# When a field goes out of focus, then we want to start checking a field
def blur
model_inst.mark_field!(@field_name)
# Only place a check mark if a if :confirm_empty_fields option is present and false and
# if the length of the field data is non-zero. The use of try here is to prevent spurious
# errors from cropping up on the client side.
if @options[:confirm_empty_fields] != false
model_inst.mark_field!(@field_name) unless attrs.value.nil? || attrs.value.try(:strip).try(:length) == 0
end
end

def marked
Expand Down