Welcome Guest, Not a member yet? Register   Sign In
Extended form helper to support HTML5 form elements.
#1

[eluser]Bramme[/eluser]
Hi everybody

I was playing around with HTML5 and CodeIgniter and noticed that there's no support yet for the new HTML5 input fields.

Here's some code I quickly whipped up, stuff goes into application/helpers/MY_form_helper.php

I did not add date, search and color inputs because they have really (and I do mean really) limited browser support. Search is only supported in Safari for Mac and Date and Color are only support by Opera as far as I know.

(Actually, Chrome renders the date input as a number field, rendering up and down arrows and increasing/decreasing the date from today).

Hope this helps someone, it might be interesting to add it to the Core form_helper.

I simply created a form_common function that's called by the other dummy functions, I only change one parameter.

Code:
<?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 */
#2

[eluser]meigwilym[/eluser]
I changed the code so the default arguments are not set
Code:
<?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 */
#3

[eluser]HunterJoe1977[/eluser]
Great helper! Is exactly what I need for my form, but I am having an issue.

When I tested this inside my localhost environment, it worked 100%.

I have moved it to a remote environment on a testing server prior to deployment, and I get an error:

Code:
An Error Was Encountered

Unable to load the requested file: helpers/my_form_helper.php

I don't know enough about CI to even begin figuring out where the problem is, if anyone can help out here, it would be great.



RESOLVED : was loading the helper incorrectly, but it is odd how it worked on localhost but not in remote. This thread helped -> http://ellislab.com/forums/viewthread/111371/#562122
#4

[eluser]InsiteFX[/eluser]
There not in CI yet because they have not been standarized yet.




Theme © iAndrew 2016 - Forum software by © MyBB