# 2) so calls to form_field_error can set the field label
BLOCK form_field_errors;
fields_with_errors = [];
FOR f = field_list;
myfield = form.field( f );
fields_with_errors.push( myfield ) IF myfield.has_error;
END;
IF fields_with_errors.size;
# Give a target for jumping here from error summary at top
# of page.
'
';
'
'; # to style just the error messages.
FOR f = fields_with_errors;
field_label = loc( f.label ) | html;
FOR field_error = f.errors;
'
' IF field_error.match("\n");
IF field_label;
field_label; ': ';
END;
field_error | html; # This has been localized by Form::Processor
' ' IF field_error.match("\n");
'
';
END;
END;
'';
content;
'
';
ELSE;
content;
END;
END;
#----------------------------------------------------------------------------%][%
# Wraps up an entire field, with a
and displays error messages for the field
# and any comments.
# Stash:
# field - current field
# comment - optional comment to display above the field.
BLOCK form_field_wrapper;
WRAPPER form_field_div + form_field_errors field_list = [ field.name ];
IF comment;
'';
END;
content;
END;
END;
#----------------------------------------------------------------------------%][%
# Wrap content in a div for the given field.
# messages next to the field
# Stash:
# field - current field
BLOCK form_field_div;
%]
[% content %]
[%
END;
#----------------------------------------------------------------------------%][%
# BLOCK to do the "Select" magic
# Looks at "form_options_limit" stash to see if should
# show a radio or an option list
BLOCK options_field;
IF !field.options;
loc('* No selection available *');
RETURN;
END;
widget = field.select_widget; # looks at the auto_widget_size
IF widget == 'select';
form_field_label( field ) IF label;
show_heading = add_heading.defined ? add_heading : ! field.multiple;
html_element_select( field, show_heading );
ELSE;
IF label;
'
';
form_legend( label );
END;
IF widget == 'radio';
html_element_radio( field );
ELSIF widget == 'checkbox';
html_element_checkbox_group( field );
ELSE;
THROW "Don't know how to handle widget type [$widget] for field [$field.name]";
END;
' ' IF label;
END;
END;
#----------------------------------------------------------------------------%][%
# Macro to generate a generic HTML element.
# Pass in:
# field - field to display (field object or field name)
# show_label - if true then will include field.label in display
MACRO form_field_element( field, label ) BLOCK;
# Allow passing a field name or a field object;
SET field = form.field( field ) UNLESS field.name;
# Store passed in label in the field
CALL field.label(label) IF label;
# First deal with specific field type
SWITCH field.type;
# Some custom fields
CASE 'Boolean';
IF label;
'
';
form_legend( label );
END;
html_element_yes_no( field );
' ' IF label;
CASE 'OneToTen';
PROCESS one_to_ten;
CASE 'DateTimeDMYHM';
IF label;
'
'; loc( label ) | html; ': ';
END;
html_element_DateTimeDMYHM( field );
CASE 'DateTimeDMYHM2';
IF label;
'
'; loc( label ) | html; ': ';
END;
html_element_DateTimeDMYHM2( field );
# For general widget types
CASE;
# Does the field have options?
IF field.can('options');
PROCESS options_field;
ELSE;
SWITCH field.widget;
CASE 'textarea';
form_field_label( field ) IF label;
html_element_textarea( field );
CASE 'checkbox';
CALL field.style('rcheck') IF label;
html_element_input( field );
form_field_label( field ) IF label;
# Assume a generic
CASE;
form_field_label( field ) IF label;
html_element_input( field );
END;
END;
END; # Switch
END;
#----------------------------------------------------------------------------%][%
#------------------- Basic HTML widgets ------------------------------------
MACRO field_attributes( field_type, id_suffix, name_suffix ) BLOCK;
SET field_id = field.id | uri | html;
SET field_id = field_id _ id_suffix IF id_suffix.defined;
SET field_name = field.full_name | uri | html;
SET field_name = field_name _ name_suffix IF name_suffix.defined;
" id='$field_id' name='$field_name'";
" type='$field_type'" IF field_type;
' readonly="readonly"' IF field.readonly;
' disabled="disabled"' IF field.disabled;
attribute_classes = [ field.type ];
attribute_classes.push( 'required' ) IF field.required;
attribute_classes.push( 'error' ) IF field.has_error;
' class="'; attribute_classes.join(' ' ); '"';
END;
#----------------------------------------------------------------------------%][%
# Generic
widget
MACRO html_element_input( field, input_field_type ) BLOCK;
# Allow passing a field name or a field object;
SET field = form.field( field ) UNLESS field.name;
DEFAULT input_field_type = field.widget;
'
';
END;
#----------------------------------------------------------------------------%][%
# Drop down
# Parameters:
# field - field object
# add_heading - if true adds "Select Foo" to the top of the list
MACRO html_element_select( field, add_heading ) BLOCK;
# Allow passing a field name or a field object;
SET field = form.field( field ) UNLESS field.name;
IF field.multiple;
multi = 1;
SET size = field.size || 5;
num_options = field.options.size;
SET size = field.options.size IF field.options.size < size;
END;
%]
[%- IF add_heading %]
[% loc('Select') %] [% loc(label) | html %]
------------
[% END %]
[% FOR i = as_list( field.options ) -%]
[% loc(i.label) | html %]
[% END;
" \n";
END;
#----------------------------------------------------------------------------%][%
# Radio input with labels to right
# If field is not required then an empty "none" option is include at the top.
MACRO html_element_radio( field ) BLOCK;
# Allow passing a field name or a field object;
SET field = form.field( field ) UNLESS field.name;
CALL field.style('rradio');
%]
[% UNLESS field.required %]
[% form_field_label( field, 'none', 'none' ) %]
[% END %]
[%
cnt = 0;
FOR i = as_list(field.options); cnt = cnt + 1; %]
[% form_field_label( field, i.label, cnt ) %]
[% END %]
[% END;
#----------------------------------------------------------------------------%][%
# Generates a checkbox group
MACRO html_element_checkbox_group( field ) BLOCK;
# Allow passing a field name or a field object;
SET field = form.field( field ) UNLESS field.name;
CALL field.style('rcheck'); # Label is on the right
FOR i = as_list(field.options) %]
[% form_field_label( field, i.label, i.value ) %]
[% END %]
[% END;
#----------------------------------------------------------------------------%][%
#