charset) . '"';
}
$form = '
' . $extra;
}
}
//--------------------------------------------------------------------
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
*
* @param string $field Field name
* @param string $default Default value
* @param boolean $html_escape Whether to escape HTML special characters or not
*
* @return string
*/
function set_value(string $field, string $default = '', bool $html_escape = true): string
{
$request = Services::request();
// Try any old input data we may have first
$value = $request->getOldInput($field);
if ($value === null)
{
$value = $request->getPost($field) ?? $default;
}
return ($html_escape) ? esc($value, 'html') : $value;
}
}
//--------------------------------------------------------------------
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 $field
* @param string $value
* @param boolean $default
*
* @return string
*/
function set_select(string $field, string $value = '', bool $default = false): string
{
$request = Services::request();
// Try any old input data we may have first
$input = $request->getOldInput($field);
if ($input === null)
{
$input = $request->getPost($field);
}
if ($input === null)
{
return ($default === true) ? ' selected="selected"' : '';
}
if (is_array($input))
{
// Note: in_array('', array(0)) returns TRUE, do not use it
foreach ($input as &$v)
{
if ($value === $v)
{
return ' selected="selected"';
}
}
return '';
}
return ($input === $value) ? ' selected="selected"' : '';
}
}
//--------------------------------------------------------------------
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 $field
* @param string $value
* @param boolean $default
*
* @return string
*/
function set_checkbox(string $field, string $value = '', bool $default = false): string
{
$request = Services::request();
// Try any old input data we may have first
$input = $request->getOldInput($field);
if ($input === null)
{
$input = $request->getPost($field);
}
if (is_array($input))
{
// Note: in_array('', array(0)) returns TRUE, do not use it
foreach ($input as &$v)
{
if ($value === $v)
{
return ' checked="checked"';
}
}
return '';
}
// Unchecked checkbox and radio inputs are not even submitted by browsers ...
if (! empty($request->getPost()) || ! empty(old($field)))
{
return ($input === $value) ? ' checked="checked"' : '';
}
return ($default === true) ? ' checked="checked"' : '';
}
}
//--------------------------------------------------------------------
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 $field
* @param string $value
* @param boolean $default
*
* @return string
*/
function set_radio(string $field, string $value = '', bool $default = false): string
{
$request = Services::request();
// Try any old input data we may have first
$input = $request->getOldInput($field);
if ($input === null)
{
$input = $request->getPost($field) ?? $default;
}
if (is_array($input))
{
// Note: in_array('', array(0)) returns TRUE, do not use it
foreach ($input as &$v)
{
if ($value === $v)
{
return ' checked="checked"';
}
}
return '';
}
// Unchecked checkbox and radio inputs are not even submitted by browsers ...
$result = '';
if (! empty($input = $request->getPost($field)) || ! empty($input = old($field)))
{
$result = ($input === $value) ? ' checked="checked"' : '';
}
if (empty($result))
{
$result = ($default === true) ? ' checked="checked"' : '';
}
return $result;
}
}
//--------------------------------------------------------------------
if (! function_exists('parse_form_attributes'))
{
/**
* Parse the form attributes
*
* Helper function used by some of the form helpers
*
* @param string|array $attributes List of attributes
* @param array $default Default values
*
* @return string
*/
function parse_form_attributes($attributes, array $default): string
{
if (is_array($attributes))
{
foreach ($default as $key => $val)
{
if (isset($attributes[$key]))
{
$default[$key] = $attributes[$key];
unset($attributes[$key]);
}
}
if (! empty($attributes))
{
$default = array_merge($default, $attributes);
}
}
$att = '';
foreach ($default as $key => $val)
{
if (! is_bool($val))
{
if ($key === 'value')
{
$val = esc($val, 'html');
}
elseif ($key === 'name' && ! strlen($default['name']))
{
continue;
}
$att .= $key . '="' . $val . '" ';
}
else
{
$att .= $key . ' ';
}
}
return $att;
}
//--------------------------------------------------------------------
}