Date field in Codegniter form

Asked

Viewed 453 times

2

I am developing a form with date field.

How I use the Codeigniter gift form resource for a date field?

In the example below I am using text area. There is a function form_ 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>";

1 answer

2

In Codeigniter, through the documentation for its latest version 2.2.0, there is none helper for forms that generate fields for new types introduced with HTML5.

Documentation for Form Helper (English)

However, since it is very necessary nowadays, others have already experienced this problem and have created solutions.

An example can be found in the Codeigniter forum with the name Extended form helper to support HTML5 form Elements (English) which follows 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 file above:

/**
 * 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 = '');
    }
}
  • Thanks for the return! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.