1

I am a beginner in programming especially using CodeIgniter Framework. and I have an issue, may be you guys could help me.

Here is the code

<div class="form-group">
    <?php
        echo form_label('Email', 'email');
        echo form_input('email', '', [
            'type'  => 'email',
            'id'    => 'email', 
            'class' => 'form-control'
        ]);
    ?>
</div>

when I inspect in browser it shows like this

<input type="text" name="email" value="" id="email" class="form-control">

but when I change the code to this

<div class="form-group">
    <?php
        echo form_label('Email', 'email');
        echo form_input([
            'type'  => 'email',
            'name'  => 'email',
            'id'    => 'email', 
            'class' => 'form-control'
        ]);
    ?>
</div>

the inspect element shows

<input type="email" name="email" value="" id="email" class="form-control">

and I am wondering, why I can't use <input type="email"> with my first code? Any explanations would be helpful to me, thank you.

4 Answers 4

2

When you look at form_helper file below is the function for form_input. Here you can see when you did not pass an array data by default it takes $defaults array so you are not getting email type in your first code. Look into below code of form_input. More Details

if ( ! function_exists('form_input'))
{
    /**
     * Text Input Field
     *
     * @param   mixed
     * @param   string
     * @param   mixed
     * @return  string
     */
    function form_input($data = '', $value = '', $extra = '')
    {
        $defaults = array(
            'type' => 'text',
            'name' => is_array($data) ? '' : $data,
            'value' => $value
        );
        return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks man, so helpful and make me understand how form_input works.
2

I think it is the behavior of the form_input of CodeIgniter framework.

for the method form_input(a,b,c) or form_input(a,b) , c is extra parameter object and it is optional. They can only create a text field for you whatever any type specified in the extraParams.

However, as you mentioned you can still create type=email input by applying the overloadded form_input, which is form_input(extraParameters),

I think these two are not the same implementation in CodeIgniter, and the one which accepting an array is the master function thus it provides more feasility for you.

for example you can create the same output of the first code by using the second one. but you can't do it reversely.

<div class="form-group">
    <?php
        echo form_label('Email', 'email');
        echo form_input([
            'type'  => 'text',
            'name'  => 'email',
            'value'  => '',
            'id'    => 'email', 
            'class' => 'form-control'
        ]);
    ?>
</div>

https://www.codeigniter.com/userguide3/helpers/form_helper.html

Comments

0

For the 1st example, by entering all parameters as value, it will default to text input, by assuming 1st parameter as name, 2nd parameter as value, and 3rd parameter as extra information.

For the 2nd example, you well defined them in array that produce the result your want.

Comments

0

I resolved the issue by editing the form_helper.php under system>helpers, and adding a new function called form_input_email() like this;

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.