Date type field in Codegniter form

2

I'm developing a form with date field.

How do I use the present form resource of Codeigniter for a date field?

In the example below I'm using text area. Is there a form_ function for dates?

 echo " <div class='form-group'>    ";
            $atributos = array(
                "class" =>  "col-sm-2 control-label"
            );          
            echo form_label("Data de início:","DATA_INICIO",$atributos);
            echo "<div class='col-sm-10'>";
            $atributos = array(
                "name"  =>  "DATA_INICIO",
                "id"    =>  "DATA_INICIO",
                "placeholder" => "Descreva o diagnóstico por dia realizado",
                "class" => "form-control",
                "value" =>  set_value('DATA_INICIO')
            );
            $formdate
            echo form_textarea($atributos);
            echo " </div>";
        echo "</div>";
    
asked by anonymous 20.06.2014 / 05:07

1 answer

2

In CodeIgniter, through documentation for your latest version 2.2.0, there is no helper for forms that allows you to generate fields for new types introduced with HTML5.

Documentation for Form Helper (English)

However, since this is very necessary in the present day, others have gone through this problem and created solutions.

An example can be found in the CodeIgniter forum with the name Extended form helper to support HTML5 form elements (English) whose same goes below:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * Common Input Field
 *
 * @access    public
 * @param    string
 * @param    mixed
 * @param    string
 * @param    string
 * @return    string
 */
if ( ! function_exists('form_common'))
{
    function form_common($type = 'text', $data = '', $value = '', $extra = '')
    {
        $defaults = array('type' => $type, 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);

        return "<input "._parse_form_attributes($data, $defaults).$extra." />";
    }
}

/**
 * Email Input Field
 *
 * @access    public
 * @param    mixed
 * @param    string
 * @param    string
 * @return    string
 */
if ( ! function_exists('form_email'))
{
    function form_email($data = '', $value = '', $extra = '')
    {
        return form_common($type = 'email', $data = '', $value = '', $extra = '');
    }
}

/**
 * Url Input Field
 *
 * @access    public
 * @param    mixed
 * @param    string
 * @param    string
 * @return    string
 */
if ( ! function_exists('form_url'))
{
    function form_url($data = '', $value = '', $extra = '')
    {
        return form_common($type = 'url', $data = '', $value = '', $extra = '');
    }
}

/**
 * Number Input Field
 *
 * @access    public
 * @param    mixed
 * @param    string
 * @param    string
 * @return    string
 */
if ( ! function_exists('form_number'))
{
    function form_number($data = '', $value = '', $extra = '')
    {
        return form_common($type = 'number', $data = '', $value = '', $extra = '');
    }
}

/**
 * Number Input Field
 *
 * @access    public
 * @param    mixed
 * @param    string
 * @param    string
 * @return    string
 */
if ( ! function_exists('form_range'))
{
    function form_range($data = '', $value = '', $extra = '')
    {
        return form_common($type = 'range', $data = '', $value = '', $extra = '');
    }
}


/* End of file MY_form_helper.php */
/* Location: ./application/helpers/MY_form_helper.php */  

For your particular case in order to create a input[type=data] you should add to the above referenced file:

/**
 * Date Input Field
 *
 * @access    public
 * @param    mixed
 * @param    string
 * @param    string
 * @return    string
 */
if ( ! function_exists('form_date'))
{
    function form_date($data = '', $value = '', $extra = '')
    {
        return form_common($type = 'date', $data = '', $value = '', $extra = '');
    }
}
    
20.06.2014 / 13:53