Skip to content

Commit

Permalink
select.html form snippet and choices
Browse files Browse the repository at this point in the history
  • Loading branch information
wardi committed Oct 6, 2014
1 parent 8a796ab commit ebc29d9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ you added to your that aren't handled by this extension.
This extension includes the following form snippets:

* [text.html](ckanext/scheming/templates/scheming/form_snippets/text.html) -
a simple text field for free-form text or numbers (default)
a simple text field for free-form text or numbers (default when no
choices list is given)
* [large_text.html](ckanext/scheming/templates/scheming/form_snippets/large_text.html) -
a larger text field, typically used for the title
* [slug.html](ckanext/scheming/templates/scheming/form_snippets/slug.html) -
Expand All @@ -160,6 +161,8 @@ This extension includes the following form snippets:
an organization selection field for datasets
* [upload.html](ckanext/scheming/templates/scheming/form_snippets/upload.html) -
an upload field for resource files
* [select.html](ckanext/scheming/templates/scheming/form_snippets/select.html) -
a select box (default when choices list is given)


### `display_snippet`
Expand Down
26 changes: 26 additions & 0 deletions ckanext/scheming/camel_photos.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@
"validators": "ignore_missing int_validator",
"form_placeholder": "eg. 2"
},
{
"field_name": "class",
"label": "Class",
"choices": [
{
"value": "bactrian",
"label": "Bactrian Camel"
},
{
"value": "hybrid",
"label": "Hybrid Camel"
},
{
"value": "f2hybrid",
"label": "F2 Hybrid Camel"
},
{
"value": "snowwhite",
"label": "Snow-white Dromedary"
},
{
"value": "black",
"label": "Black Camel"
}
]
},
{
"field_name": "other",
"label": {"en": "Other information"},
Expand Down
20 changes: 20 additions & 0 deletions ckanext/scheming/templates/scheming/form_snippets/select.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{% import 'macros/form.html' as form %}

{%- set options=[] -%}
{%- for c in field.choices -%}
{% do options.append({
'value': c.value,
'text': h.scheming_language_text(c.label)}) %}
{%- endfor -%}

{{ form.select(
field.field_name,
id='field-' + field.field_name,
label=h.scheming_language_text(field.label),
options=options,
selected=data[field.field_name],
error=errors[field.field_name],
classes=['control-medium'],
attrs=field.form_attrs if 'form_attrs' in field else {},
is_required=h.scheming_field_required(field)
) }}
6 changes: 5 additions & 1 deletion ckanext/scheming/templates/scheming/snippets/form_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
{%- set form_snippet = field.form_snippet -%}

{%- if not form_snippet -%}
{%- set form_snippet = 'text.html' -%}
{%- if 'choices' in field -%}
{%- set form_snippet = 'select.html' -%}
{%- else -%}
{%- set form_snippet = 'text.html' -%}
{%- endif -%}
{%- endif -%}

{%- if '/' not in form_snippet -%}
Expand Down

0 comments on commit ebc29d9

Please sign in to comment.