![]() |
|
Snippets |
|
Sometimes you may want to give the user the possibility to select a certain range of numbers in a numeric field in the admin generator. An example would be a rating between 1 and 5. This is where this snippet comes in handy.
Add the helper code below to one of your helper groups, that preferably is included in your standard_helpers.
Then you can use it like in your generator.yml like follows:
generator:
...
param:
...
edit:
display: [..., percentage, ...]
fields:
...
percentage: { type: select_range_tag, params: min=0 max=100 step=5 }
Supplying a step is optional, it defaults to 1.
Helper code:
function select_range_tag($name, $selected, $options) { $options = _parse_attributes($options); $select_options = range(_get_option($options, 'min', 1), _get_option($options, 'max', 5), _get_option($options, 'step', 1)); return select_tag($name, options_for_select(array_combine($select_options, $select_options), $selected)); } function object_select_range_tag($object, $method, $options = array(), $default_value = null) { $options = _parse_attributes($options); $value = _get_object_value($object, $method, $default_value); return select_range_tag(_convert_method_to_name($method, $options), $value, $options); }