S. Haddad
S. Haddad

Reputation: 15

Array to string conversion php form_helper error

A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: helpers/form_helper.php

Line Number: 522

Here is my view that contains form

        <?php
        // open the form using CI form helper
        echo form_open('loginController', array('id' => 'login'));
        // first input field
        echo "<div>";
        echo form_label('Username', array('for' => 'login_username'));
        echo form_input(array(
            'type' => 'text',
            'name' => 'username',
            'id' => 'login_username',
            'value' => set_value('username')
        ));
        echo "</div>";
        // second input field
        echo "<div>";
        echo form_label('Password', array('for' => 'login_password'));
        echo form_password(array('type' => 'password', 'name' => 'password', 'id' => 'login_password'));
        echo "</div>";
        // submit button
        echo "<div class='submit'>";
        echo form_button(array('type' => 'submit', 'content' => 'Log in'));
        echo "</div>";

        echo "<div class='errors'>";
        // display if incorrect username/password
        if ($this->session->flashdata('login_error'))
            echo 'Incorrect username/password';
        // display if incorrect input data
        echo validation_errors();
        echo "</div>";
        // close the form
        echo form_close();
        ?>

Here is form_helper.php (line 513 to 537) that is mentioned in PHP Error

if ( ! function_exists('form_label'))
{
function form_label($label_text = '', $id = '', $attributes = array())
{

    $label = '<label';

    if ($id != '')
    {
        $label .= " for=\"$id\"";
    }

    if (is_array($attributes) AND count($attributes) > 0)
    {
        foreach ($attributes as $key => $val)
        {
            $label .= ' '.$key.'="'.$val.'"';
        }
    }

    $label .= ">$label_text</label>";

    return $label;
}
}

Upvotes: 1

Views: 4247

Answers (2)

Orangepill
Orangepill

Reputation: 24645

Here

echo form_label('Username', array('for' => 'login_username'));

and here

echo form_label('Password', array('for' => 'login_password'));

You are passing an array for the id parameter of the function that should be a string try

echo form_label('Username', "lblUsername", array('for' => 'login_username'));

and

echo form_label('Username', "lblPassword", array('for' => 'login_username'));

Or simply pass null in as the value of the id.

Upvotes: 1

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

You are calling function in wrong way. The 3rd parameter is array but you are passing 2nd parameter as array.

Use below kind of function call for all form_label.

echo form_label('Username', 'Username',array('for' => 'login_username'));
                          ^^^^^^^^^^^^

Function definition

function form_label($label_text = '', $id = '', $attributes = array())

Upvotes: 0

Related Questions