Skip to content

Commit

Permalink
Merge branch '1089-required-fields'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jul 23, 2013
2 parents 0a339db + 1c3604d commit ef5f8da
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 22 deletions.
3 changes: 2 additions & 1 deletion ckan/public/base/javascript/plugins/jquery.slug-preview.js
Expand Up @@ -31,13 +31,14 @@
var field = element.find('input');
var preview = $(options.template);
var value = preview.find('.slug-preview-value');
var required = $('<div>').append($('.control-required', element).clone()).html();

function setValue() {
var val = escape(field.val()) || options.placeholder;
value.text(val);
}

preview.find('strong').text(options.i18n['URL'] + ':');
preview.find('strong').html(required + ' ' + options.i18n['URL'] + ':');
preview.find('.slug-preview-prefix').text(options.prefix);
preview.find('button').text(options.i18n['Edit']).click(function (event) {
event.preventDefault();
Expand Down
8 changes: 8 additions & 0 deletions ckan/public/base/less/forms.less
Expand Up @@ -79,6 +79,14 @@ textarea {
height: (@baseLineHeight*2)+1;
}

.control-required {
color: @errorBorder;
}

.form-actions .control-required-message {
float: left;
}

.form-actions {
background: none;
margin-left: -@gutterX;
Expand Down
3 changes: 2 additions & 1 deletion ckan/templates/group/snippets/group_form.html
Expand Up @@ -15,7 +15,7 @@
{% set domain = domain|replace("http://", "")|replace("https://", "") %}
{% set attrs = {'data-module': 'slug-preview-slug', 'data-module-prefix': domain, 'data-module-placeholder': '<group>'} %}

{{ form.prepend('name', label=_('URL'), prepend=prefix, id='field-url', placeholder=_('my-group'), value=data.name, error=errors.name, attrs=attrs) }}
{{ form.prepend('name', label=_('URL'), prepend=prefix, id='field-url', placeholder=_('my-group'), value=data.name, error=errors.name, attrs=attrs, is_required=true) }}

{{ form.markdown('description', label=_('Description'), id='field-description', placeholder=_('A little information about my group...'), value=data.description, error=errors.description) }}

Expand Down Expand Up @@ -70,6 +70,7 @@
#}

<div class="form-actions">
{{ form.required_message() }}
{% block delete_button %}
{% if h.check_access('group_delete', {'id': data.id}) %}
{% set locale = h.dump_json({'content': _('Are you sure you want to delete this Group?')}) %}
Expand Down
54 changes: 39 additions & 15 deletions ckan/templates/macros/form.html
Expand Up @@ -10,17 +10,18 @@
type - The type of input eg. email, url, date (default: text).
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Examples:

{% import 'macros/form.html' as form %}
{{ form.input('title', label=_('Title'), value=data.title, error=errors.title) }}

#}
{% macro input(name, id='', label='', value='', placeholder='', type='text', error="", classes=[], attrs={}) %}
{% macro input(name, id='', label='', value='', placeholder='', type='text', error="", classes=[], attrs={}, is_required=false) %}
{%- set extra_html = caller() if caller -%}

{% call input_block(id or name, label or name, error, classes, extra_html=extra_html) %}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html, is_required=is_required) %}
<input id="{{ id or name }}" type="{{ type }}" name="{{ name }}" value="{{ value | empty_and_escape }}" placeholder="{{ placeholder }}" {{ attributes(attrs) }} />
{% endcall %}
{% endmacro %}
Expand All @@ -35,20 +36,22 @@
checked - If true the checkbox will be checked
error - An error string for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Example:

{% import 'macros/form.html' as form %}
{{ form.checkbox('remember', checked=true) }}

#}
{% macro checkbox(name, id='', label='', value='', checked=false, placeholder='', error="", classes=[], attrs={}) %}
{% macro checkbox(name, id='', label='', value='', checked=false, placeholder='', error="", classes=[], attrs={}, is_required=false) %}
{%- set extra_html = caller() if caller -%}
<div class="control-group{{ " " ~ classes | join(" ") }}{% if error %} error{% endif %}">
<div class="controls">
<label class="checkbox" for="{{ id or name }}">
<input id="{{ id or name }}" type="checkbox" name="{{ name }}" value="{{ value | empty_and_escape }}" {{ "checked " if checked }} {{ attributes(attrs) }} />
{{ label or name }}
{% if is_required %}{{ input_required() }}{% endif %}
{% if error and error is iterable %}<strong class="error-inline">{{ error|join(', ') }}</strong>{% endif %}
</label>
{{ extra_html }}
Expand All @@ -71,19 +74,20 @@
selected - The value of the selected <option>.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Examples:

{% import 'macros/form.html' as form %}
{{ form.select('year', label=_('Year'), options={2010: 2010, 2011: 2011}, selected=2011, error=errors.year) }}

#}
{% macro select(name, id='', label='', options='', selected='', error='', classes=[], attrs={}) %}
{% macro select(name, id='', label='', options='', selected='', error='', classes=[], attrs={}, is_required=false) %}
{% set classes = (classes|list) %}
{% do classes.append('control-select') %}

{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html) %}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html, is_required=is_required) %}
<select id="{{ id or name }}" name="{{ name }}" {{ attributes(attrs) }}>
{% for option in options %}
<option value="{{ option.value }}"{% if option.value == selected %} selected{% endif %}>{{ option.text or option.value }}</option>
Expand All @@ -103,19 +107,20 @@
placeholder - Some placeholder text.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Examples:

{% import 'macros/form.html' as form %}
{{ form.markdown('desc', id='field-description', label=_('Description'), value=data.desc, error=errors.desc) }}

#}
{% macro markdown(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={}) %}
{% macro markdown(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={}, is_required=false) %}
{% set classes = (classes|list) %}
{% do classes.append('control-full') %}

{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error, classes, control_classes=["editor"], extra_html=extra_html) %}
{% call input_block(id or name, label or name, error, classes, control_classes=["editor"], extra_html=extra_html, is_required=is_required) %}
<textarea id="{{ id or name }}" name="{{ name }}" cols="20" rows="5" placeholder="{{ placeholder }}" {{ attributes(attrs) }}>{{ value | empty_and_escape }}</textarea>
<span class="editor-info-block">{% trans %}You can use <a href="http://daringfireball.net/projects/markdown/syntax" target="_blank">Markdown formatting</a> here{% endtrans %}</span>
{% endcall %}
Expand All @@ -132,19 +137,20 @@
placeholder - Some placeholder text.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Examples:

{% import 'macros/form.html' as form %}
{{ form.textarea('desc', id='field-description', label=_('Description'), value=data.desc, error=errors.desc) }}

#}
{% macro textarea(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={}) %}
{% macro textarea(name, id='', label='', value='', placeholder='', error="", classes=[], attrs={}, is_required=false) %}
{% set classes = (classes|list) %}
{% do classes.append('control-full') %}

{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html) %}
{% call input_block(id or name, label or name, error, classes, extra_html=extra_html, is_required=is_required) %}
<textarea id="{{ id or name }}" name="{{ name }}" cols="20" rows="5" placeholder="{{ placeholder }}" {{ attributes(attrs) }}>{{ value | empty_and_escape }}</textarea>
{% endcall %}
{% endmacro %}
Expand All @@ -163,19 +169,20 @@
placeholder - Some placeholder text.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Examples:

{% import 'macros/form.html' as form %}
{{ form.prepend('slug', id='field-slug', prepend='/dataset/', label=_('Slug'), value=data.slug, error=errors.slug) }}

#}
{% macro prepend(name, id='', label='', prepend='', value='', placeholder='', type='text', error="", classes=[], attrs={}) %}
{% macro prepend(name, id='', label='', prepend='', value='', placeholder='', type='text', error="", classes=[], attrs={}, is_required=false) %}
{# We manually append the error here as it needs to be inside the .input-prepend block #}
{% set classes = (classes|list) %}
{% do classes.append('error') if error %}
{%- set extra_html = caller() if caller -%}
{% call input_block(id or name, label or name, error='', classes=classes, extra_html=extra_html) %}
{% call input_block(id or name, label or name, error='', classes=classes, extra_html=extra_html, is_required=is_required) %}
<div class="input-prepend">
{% if prepend %}<span class="add-on">{{ prepend }}</span>{%- endif -%}
<input id="{{ id or name }}" type="{{ type }}" name="{{ name }}" value="{{ value | empty_and_escape }}" placeholder="{{ placeholder }}" {{ attributes(attrs) }} />
Expand All @@ -199,6 +206,7 @@
placeholder - A tuple of placeholder text for the (key, value) fields.
error - A list of error strings for the field or just true to highlight the field.
classes - An array of classes to apply to the control-group.
is_required - Boolean of whether this input is requred for the form to validate

Examples:

Expand All @@ -211,13 +219,13 @@
error=''
) }}
#}
{% macro custom(names=(), id="", label="", values=(), placeholders=(), error="", classes=[], attrs={}) %}
{% macro custom(names=(), id="", label="", values=(), placeholders=(), error="", classes=[], attrs={}, is_required=false) %}
{%- set classes = (classes|list) -%}
{%- set label_id = (id or names[0]) ~ "-key" -%}
{%- set extra_html = caller() if caller -%}
{%- do classes.append('control-custom') -%}

{% call input_block(label_id, label or name, error, classes, control_classes=["editor"], extra_html=extra_html) %}
{% call input_block(label_id, label or name, error, classes, control_classes=["editor"], extra_html=extra_html, is_required=is_required) %}
<div class="input-prepend" {{ attributes(attrs) }}>
<label for="{{ label_id }}" class="add-on">Key</label><input id="{{ id or names[0] }}-key" type="text" name="{{ names[0] }}" value="{{ values[0] | empty_and_escape }}" placeholder="{{ placeholders[0] }}" />
<label for="{{ id or names[1] }}-value" class="add-on">Value</label><input id="{{ id or names[1] }}-value" type="text" name="{{ names[1] }}" value="{{ values[1] | empty_and_escape }}" placeholder="{{ placeholders[1] }}" />
Expand All @@ -241,6 +249,7 @@
classes - An array of custom classes for the outer element.
control_classes - An array of custom classes for the .control wrapper.
extra_html - An html string to be inserted after the errors eg. info text.
is_required - Boolean of whether this input is requred for the form to validate

Example:

Expand All @@ -250,9 +259,9 @@
{% endcall %}

#}
{% macro input_block(for, label="", error="", classes=[], control_classes=[], extra_html="") %}
{% macro input_block(for, label="", error="", classes=[], control_classes=[], extra_html="", is_required=false) %}
<div class="control-group{{ " error" if error }}{{ " " ~ classes | join(' ') }}">
<label class="control-label" for="{{ for }}">{{ label or _('Custom') }}</label>
<label class="control-label" for="{{ for }}">{% if is_required %}<span title="{{ _("This field is required") }}" class="control-required">*</span> {% endif %}{{ label or _('Custom') }}</label>
<div class="controls{{ " " ~ control_classes | join(' ') }}">
{{ caller() }}
{% if error and error is iterable %}<span class="error-block">{{ error|join(', ') }}</span>{% endif %}
Expand Down Expand Up @@ -371,3 +380,18 @@
{{ " " }}{{ key }}{% if value != "" %}="{{ value }}"{% endif %}
{%- endfor -%}
{%- endmacro -%}

{#
Outputs the "* Required field" message for the bottom of formss

Example
{% import 'macros/form.html' as form %}
{{ form.required_message() }}

#}
{% macro required_message() %}
<p class="control-required-message">
<span class="control-required">*</span> {{ _("Required field") }}
</p>
{% endmacro %}

3 changes: 2 additions & 1 deletion ckan/templates/organization/snippets/organization_form.html
Expand Up @@ -15,7 +15,7 @@
{% set domain = domain|replace("http://", "")|replace("https://", "") %}
{% set attrs = {'data-module': 'slug-preview-slug', 'data-module-prefix': domain, 'data-module-placeholder': '<organization>'} %}

{{ form.prepend('name', label=_('URL'), prepend=prefix, id='field-url', placeholder=_('my-organization'), value=data.name, error=errors.name, attrs=attrs) }}
{{ form.prepend('name', label=_('URL'), prepend=prefix, id='field-url', placeholder=_('my-organization'), value=data.name, error=errors.name, attrs=attrs, is_required=true) }}

{{ form.markdown('description', label=_('Description'), id='field-description', placeholder=_('A little information about my organization...'), value=data.description, error=errors.description) }}

Expand Down Expand Up @@ -72,6 +72,7 @@
#}

<div class="form-actions">
{{ form.required_message() }}
{% block delete_button %}
{% if h.check_access('organization_delete', {'id': data.id}) %}
{% set locale = h.dump_json({'content': _('Are you sure you want to delete this Organization? This will delete all the public and private datasets belonging to this organization.')}) %}
Expand Down
4 changes: 3 additions & 1 deletion ckan/templates/package/snippets/package_basic_fields.html
Expand Up @@ -10,7 +10,7 @@
{% set domain = domain|replace("http://", "")|replace("https://", "") %}
{% set attrs = {'data-module': 'slug-preview-slug', 'data-module-prefix': domain, 'data-module-placeholder': '<dataset>'} %}

{{ form.prepend('name', id='field-name', label=_('URL'), prepend=prefix, placeholder=_('eg. my-dataset'), value=data.name, error=errors.name, attrs=attrs) }}
{{ form.prepend('name', id='field-name', label=_('URL'), prepend=prefix, placeholder=_('eg. my-dataset'), value=data.name, error=errors.name, attrs=attrs, is_required=true) }}
{% endblock %}

{% block package_basic_fields_custom %}
Expand Down Expand Up @@ -99,4 +99,6 @@
</div>
{% endif %}

{{ form.required_message() }}

{% endblock %}
5 changes: 4 additions & 1 deletion ckan/templates/package/snippets/resource_form.html
Expand Up @@ -38,7 +38,7 @@
{% endblock %}

{% block basic_fields_url %}
{{ form.input('url', id='field-url', label=_('Resource'), placeholder=_('eg. http://example.com/gold-prices-jan-2011.json'), value=data.url, error=errors.url, classes=['control-full', 'control-large']) }}
{{ form.input('url', id='field-url', label=_('Resource'), placeholder=_('eg. http://example.com/gold-prices-jan-2011.json'), value=data.url, error=errors.url, classes=['control-full', 'control-large'], is_required=true) }}
{% endblock %}

{% block basic_fields_name %}
Expand All @@ -58,6 +58,9 @@
</span>
{% endcall %}
{% endblock %}

{{ form.required_message() }}

{% endblock %}

{% block metadata_fields %}
Expand Down
6 changes: 4 additions & 2 deletions ckan/templates/user/edit_user_form.html
Expand Up @@ -6,11 +6,11 @@
<fieldset>
<legend>{{ _('Change your details') }}</legend>

{{ form.input('name', label=_('Username'), id='field-username', value=data.name, error=errors.name, classes=['control-medium']) }}
{{ form.input('name', label=_('Username'), id='field-username', value=data.name, error=errors.name, classes=['control-medium'], is_required=true) }}

{{ form.input('fullname', label=_('Full name'), id='field-fullname', value=data.fullname, error=errors.fullname, placeholder=_('eg. Joe Bloggs'), classes=['control-medium']) }}

{{ form.input('email', label=_('Email'), id='field-email', type='email', value=data.email, error=errors.email, placeholder=_('eg. joe@example.com'), classes=['control-medium']) }}
{{ form.input('email', label=_('Email'), id='field-email', type='email', value=data.email, error=errors.email, placeholder=_('eg. joe@example.com'), classes=['control-medium'], is_required=true) }}

{{ form.markdown('about', label=_('About'), id='field-about', value=data.about, error=errors.about, placeholder=_('A little information about yourself')) }}

Expand All @@ -20,6 +20,8 @@
{% endcall %}
{% endif %}

{{ form.required_message() }}

</fieldset>

<fieldset>
Expand Down

0 comments on commit ef5f8da

Please sign in to comment.