Created
April 19, 2012 15:02
-
-
Save csasbach/2421513 to your computer and use it in GitHub Desktop.
This is a custom helper file I created for use in CodeIgniter. It generates form elements with semantic and accessible HTML and CSS with the hooks needed to utilize error reporting in CodeIgniter and in jQuery.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* SEMANTIC AND ACCESSIBLE FORMS HELPER FUNCTIONS | |
* | |
* Some more accesible and semantic form helpers. I utilize the default form helpers as | |
* much as possible to preserve the original syntax. | |
* | |
*/ | |
/* FORM OPEN */ | |
/** | |
* RETURNS html to open a form | |
* | |
* @param string the URI segments of the form destination | |
* @param array a key/value pair of attributes | |
* @param array a key/value pair hidden data | |
* @param string The displayed title of the form. | |
* @param string A longer description or instructions for the form. | |
* @param boolean Choose if errors will be displayed at the top of the form. | |
* | |
* @return string | |
*/ | |
function sa_form_open($action, $atts, $hidden, $title="", $desc="", $top_errors=TRUE) | |
{ | |
// prepend sa-form class to class(es) in $atts['class'] | |
$class = 'sa-form '.(isset($atts['class']) ? $atts['class'] : ''); | |
// update $atts['class'] | |
$atts['class'] = $class; | |
// call the original form_open() helper function | |
$sa_form_open = form_open($action, $atts, $hidden); | |
// write form title | |
$sa_form_open .= ($title != "" ? '<h1>'.$title.'</h1>' : ''); | |
// write form description | |
$sa_form_open .= ($desc != "" ? '<p>'.$desc.'</p>' : ''); | |
// display errors | |
if ($top_errors) { | |
$errors = validation_errors('<li>','</li>'); | |
$sa_form_open .= (!empty($errors) ? '<ul class="error">'.$errors.'</ul>' : ''); | |
} | |
// write form instructions | |
$sa_form_open .= '<small>'; | |
$sa_form_open .= '<span style="float:right">'; | |
$sa_form_open .= '<abbr class="requiredField" title="Fields marked with this symbol are required.">*</abbr>'; | |
$sa_form_open .= ' denotes a required field.</span>'; | |
$sa_form_open .= '<abbr title="Use these symbols to help you correctly fill out the form.">[?]</abbr>'; | |
$sa_form_open .= ' for help. '; | |
$sa_form_open .= '<div style="clear:both"></div></small>'; | |
return $sa_form_open; | |
} | |
/* FORM SECTION (fieldset) */ | |
/** | |
* RETURNS html to open a section in a form | |
* | |
* @param string The id attribute of the fieldset tag and the legend title if no title is given. | |
* @param string The legend title and if NOT "" also the displayed title of the section. | |
* @param string A longer description or instructions for the section. | |
* @param array a key/value pair of attributes | |
* @param string additional data for the field (i.e. javascript) | |
* | |
* @return string | |
*/ | |
function sa_form_section($id, $title="", $desc="", $atts=array(),$extra="") { | |
// declare default attributes | |
$default_atts = array(); | |
// open fieldset tag and write attributes | |
$sa_form_section = '<fieldset id="'.$id.'" '._parse_form_attributes($atts, $default_atts).' '.$extra.'>'; | |
// write section title | |
$sa_form_section .= ($title != "" ? '<h2>'.$title.'</h2>' : ''); | |
if ($title == "") $title= $id; | |
// write legend | |
$sa_form_section .= '<legend class="screen-reader-text" >'.$title.'</legend>'; | |
// write description | |
$sa_form_section .= ($desc != "" ? '<p>'.$desc.'</p>' : ''); | |
return $sa_form_section; | |
} | |
/* LABEL WRAPPER */ | |
/** | |
* RETURNS field wrapped in a label tag with optional title and description | |
* | |
* @param string a field | |
* @param string the id of the field | |
* @param bool is it a required field? | |
* @param string The displayed title of the form. | |
* @param string A longer description or instructions for the form. | |
* @param array a key/value pair of attributes | |
* | |
* @return string | |
*/ | |
function sa_label_wrapper($field, $id, $req, $title="", $desc="", $atts=array()) | |
{ | |
// if required field add requiredField class to label | |
$atts['class'] = ($req ? 'requiredField ' : '').(isset($atts['class']) ? $atts['class'] : ''); | |
// write errors | |
$label_text = form_error($id); | |
// open .field-info span tag | |
$label_text .= '<span class="field-info">'; | |
// write required field abbreviation | |
$label_text .= ($req ? '<abbr class="requiredField" title="This is a required field.">*</abbr>' : ''); | |
if ($title == "") $title = $id; | |
// write field title | |
$label_text .= $title.' '; | |
// write description abbreviation | |
$label_text .= ($desc != "" ? '<abbr title="'.$desc.'">[?]</abbr> ' : ''); | |
// close .field-info span tag | |
$label_text .= '</span>'; | |
// write the field | |
$label_text .= $field; | |
return form_label($label_text, $id, $atts); | |
} | |
/* TEXT INPUT */ | |
/** | |
* RETURNS a text input field wrapped in a label | |
* | |
* @param array a key/value pair of attributes for the field | |
* @param string a default value for the field | |
* @param string additional data for the field (i.e. javascript) | |
* @param bool is the string required? | |
* @param string The title displayed in the label for the field. | |
* @param string A longer description or instructions for the field. | |
* @param array a key/value pair of attributes for the fields label | |
* | |
* @return string | |
*/ | |
function sa_form_input($atts, $value="", $extra="", $req=FALSE, $title="", $desc="", $label_atts=array()) | |
{ | |
// set value | |
$value = set_value($atts['id'],$value); | |
//add a default tabindex of 1 for accessibility | |
if (!isset($atts['tabindex'])) $atts['tabindex'] = 1; | |
// call the original form_input() helper function | |
$sa_form_input = form_input($atts, $value, $extra); | |
// wrap field with label_wrapper() | |
$sa_form_input = sa_label_wrapper($sa_form_input, $atts['id'], $req, $title, $desc, $label_atts); | |
return $sa_form_input; | |
} | |
/* PASSWORD INPUT */ | |
/** | |
* RETURNS a password input field wrapped in a label | |
* | |
* @param array a key/value pair of attributes for the field | |
* @param string a default value for the field | |
* @param string additional data for the field (i.e. javascript) | |
* @param bool is the string required? | |
* @param string The title displayed in the label for the field. | |
* @param string A longer description or instructions for the field. | |
* @param array a key/value pair of attributes for the fields label | |
* | |
* @return string | |
*/ | |
function sa_form_password($atts, $value="", $extra="", $req=TRUE, $title="", $desc="", $label_atts=array()) | |
{ | |
// set value | |
$value = set_value($atts['id'],$value); | |
//add a default tabindex of 1 for accessibility | |
if (!isset($atts['tabindex'])) $atts['tabindex'] = 1; | |
// call the original form_password() helper function | |
$sa_form_password = form_password($atts, $value, $extra); | |
// wrap field with label_wrapper() | |
$sa_form_password = sa_label_wrapper($sa_form_password, $atts['id'], $req, $title, $desc, $label_atts); | |
return $sa_form_password; | |
} | |
/* FILE INPUT */ | |
/** | |
* RETURNS a file input field wrapped in a label | |
* | |
* @param array a key/value pair of attributes for the field | |
* @param string a default value for the field | |
* @param string additional data for the field (i.e. javascript) | |
* @param bool is the string required? | |
* @param string The title displayed in the label for the field. | |
* @param string A longer description or instructions for the field. | |
* @param array a key/value pair of attributes for the fields label | |
* | |
* @return string | |
*/ | |
function sa_form_upload($atts, $value="", $extra="", $req=FALSE, $title="", $desc="", $label_atts=array()) | |
{ | |
//add a default tabindex of 1 for accessibility | |
if (!isset($atts['tabindex'])) $atts['tabindex'] = 1; | |
// call the original form_upload() helper function | |
$sa_form_upload = form_upload($atts, $value, $extra); | |
// wrap field with label_wrapper() | |
$sa_form_upload = sa_label_wrapper($sa_form_upload, $atts['id'], $req, $title, $desc, $label_atts); | |
return $sa_form_upload; | |
} | |
/* TEXTAREA */ | |
/** | |
* RETURNS a textarea field wrapped in a label | |
* | |
* @param array a key/value pair of attributes for the field | |
* @param string a default value for the field | |
* @param string additional data for the field (i.e. javascript) | |
* @param bool is the string required? | |
* @param string The title displayed in the label for the field. | |
* @param string A longer description or instructions for the field. | |
* @param array a key/value pair of attributes for the fields label | |
* | |
* @return string | |
*/ | |
function sa_form_textarea($atts, $value="", $extra="", $req=FALSE, $title="", $desc="", $label_atts=array()) | |
{ | |
// set value | |
$value = set_value($atts['id'],$value); | |
//add a default rows of 5 | |
if (!isset($atts['rows'])) $atts['rows'] = 5; | |
//add a default tabindex of 1 for accessibility | |
if (!isset($atts['tabindex'])) $atts['tabindex'] = 1; | |
// call the original form_textarea() helper function | |
$sa_form_textarea = form_textarea($atts, $value, $extra); | |
// wrap field with label_wrapper() | |
$sa_form_textarea = sa_label_wrapper($sa_form_textarea, $atts['id'], $req, $title, $desc, $label_atts); | |
return $sa_form_textarea; | |
} | |
/* SUBMIT INPUT */ | |
/** | |
* RETURNS a text input field wrapped in a label | |
* | |
* @param array a key/value pair of attributes for the field | |
* @param string a default value for the field | |
* @param string additional data for the field (i.e. javascript) | |
* | |
* @return string | |
*/ | |
function sa_form_submit($atts, $value="", $extra="") | |
{ | |
//add a default tabindex of 1 for accessibility | |
if (!isset($atts['tabindex'])) $atts['tabindex'] = 1; | |
// call the original form_input() helper function | |
$sa_form_submit = form_submit($atts, $value, $extra); | |
return $sa_form_submit; | |
} | |
/* End of file MY_form_helper.php */ | |
/* Location: ./application/helpers/MY_form_helper.php */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment