config->site_url($action);
}
elseif ( ! $action)
{
// If no action is provided then set to the current url
$action = $CI->config->site_url($CI->uri->uri_string());
}
$form = '
'.$extra;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('form_prep'))
{
/**
* Form Prep
*
* Formats text so that it can be safely placed in a form field in the event it has HTML tags.
*
* @param string|string[] $str Value to escape
* @param bool $is_textarea Whether we're escaping for a textarea element
* @return string|string[] Escaped values
*/
function form_prep($str = '', $is_textarea = FALSE)
{
if (is_array($str))
{
foreach (array_keys($str) as $key)
{
$str[$key] = form_prep($str[$key], $is_textarea);
}
return $str;
}
if ($is_textarea === TRUE)
{
return str_replace(array('<', '>'), array('<', '>'), stripslashes($str));
}
return str_replace(array("'", '"'), array(''', '"'), stripslashes($str));
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_value'))
{
/**
* Form Value
*
* Grabs a value from the POST array for the specified field so you can
* re-populate an input field or textarea. If Form Validation
* is active it retrieves the info from the validation class
*
* @param string $field Field name
* @param string $default Default value
* @param bool $is_textarea Whether the field is a textarea element
* @return string
*/
function set_value($field = '', $default = '', $is_textarea = FALSE)
{
$CI =& get_instance();
$value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
? $CI->form_validation->set_value($field, $default)
: $CI->input->post($field, FALSE);
return form_prep($value === NULL ? $default : $value, $is_textarea);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_select'))
{
/**
* Set Select
*
* Let's you set the selected value of a menu via data in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
* @param string
* @param string
* @param bool
* @return string
*/
function set_select($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default === TRUE)
{
return ' selected="selected"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
elseif (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
return ' selected="selected"';
}
return $OBJ->set_select($field, $value, $default);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_checkbox'))
{
/**
* Set Checkbox
*
* Let's you set the selected value of a checkbox via the value in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
* @param string
* @param string
* @param bool
* @return string
*/
function set_checkbox($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default === TRUE)
{
return ' checked="checked"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
elseif (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
return ' checked="checked"';
}
return $OBJ->set_checkbox($field, $value, $default);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('set_radio'))
{
/**
* Set Radio
*
* Let's you set the selected value of a radio field via info in the POST array.
* If Form Validation is active it retrieves the info from the validation class
*
* @param string
* @param string
* @param bool
* @return string
*/
function set_radio($field = '', $value = '', $default = FALSE)
{
$OBJ =& _get_validation_object();
if ($OBJ === FALSE)
{
if ( ! isset($_POST[$field]))
{
if (count($_POST) === 0 && $default === TRUE)
{
return ' checked="checked"';
}
return '';
}
$field = $_POST[$field];
if (is_array($field))
{
if ( ! in_array($value, $field))
{
return '';
}
}
else
{
if (($field == '' OR $value == '') OR $field !== $value)
{
return '';
}
}
return ' checked="checked"';
}
return $OBJ->set_radio($field, $value, $default);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('form_error'))
{
/**
* Form Error
*
* Returns the error for a specific form field. This is a helper for the
* form validation class.
*
* @param string
* @param string
* @param string
* @return string
*/
function form_error($field = '', $prefix = '', $suffix = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
return $OBJ->error($field, $prefix, $suffix);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('validation_errors'))
{
/**
* Validation Error String
*
* Returns all the errors associated with a form submission. This is a helper
* function for the form validation class.
*
* @param string
* @param string
* @return string
*/
function validation_errors($prefix = '', $suffix = '')
{
if (FALSE === ($OBJ =& _get_validation_object()))
{
return '';
}
return $OBJ->error_string($prefix, $suffix);
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_parse_form_attributes'))
{
/**
* Parse the form attributes
*
* Helper function used by some of the form helpers
*
* @param array $attributes List of attributes
* @param array $default Default values
* @return string
*/
function _parse_form_attributes($attributes, $default)
{
if (is_array($attributes))
{
foreach ($default as $key => $val)
{
if (isset($attributes[$key]))
{
$default[$key] = $attributes[$key];
unset($attributes[$key]);
}
}
if (count($attributes) > 0)
{
$default = array_merge($default, $attributes);
}
}
$att = '';
foreach ($default as $key => $val)
{
if ($key === 'value')
{
$val = form_prep($val);
}
elseif ($key === 'name' && ! strlen($default['name']))
{
continue;
}
$att .= $key.'="'.$val.'" ';
}
return $att;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_attributes_to_string'))
{
/**
* Attributes To String
*
* Helper function used by some of the form helpers
*
* @param mixed
* @param bool
* @return string
*/
function _attributes_to_string($attributes, $formtag = FALSE)
{
if (is_string($attributes) && strlen($attributes) > 0)
{
if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE)
{
$attributes .= ' method="post"';
}
if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE)
{
$attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
}
return ' '.$attributes;
}
if (is_object($attributes) && count($attributes) > 0)
{
$attributes = (array) $attributes;
}
if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0))
{
$atts = '';
if ( ! isset($attributes['method']) && $formtag === TRUE)
{
$atts .= ' method="post"';
}
if ( ! isset($attributes['accept-charset']) && $formtag === TRUE)
{
$atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
}
foreach ($attributes as $key => $val)
{
$atts .= ' '.$key.'="'.$val.'"';
}
return $atts;
}
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('_get_validation_object'))
{
/**
* Validation Object
*
* Determines what the form validation class was instantiated as, fetches
* the object and returns it.
*
* @return mixed
*/
function &_get_validation_object()
{
$CI =& get_instance();
// We set this as a variable since we're returning by reference.
$return = FALSE;
if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
{
if ( ! isset($CI->$object) OR ! is_object($CI->$object))
{
return $return;
}
return $CI->$object;
}
return $return;
}
}
/* End of file form_helper.php */
/* Location: ./system/helpers/form_helper.php */