Skip to content
Advertisement

Codeigniter form helper setting input class on edit form

I am using ion auth to edit some user data.This is the code

<h1><?php echo lang('edit_user_heading');?></h1>
<p><?php echo lang('edit_user_subheading');?></p>

<div id="infoMessage"><?php echo $message;?></div>

<?php echo form_open(uri_string());?>

      <p>
            <?php echo lang('edit_user_fname_label', 'first_name');?> <br />
            <?php 
            $data = array(
              'name'        => 'first_name',
              'value'          => set_value('first_name', $first_name),
              'class'       => 'form-control'
            );
            
            echo form_input($data);?>
      </p>

This produces the input plus the value but without the input class

echo form_input($first_name);?>

My first code snippet is an attempt at adding input class and populating the input field. This gives me a conversion error Message: Array to string conversion

How should i add form class inside the edit form field?

Advertisement

Answer

The error is not coming from trying to add a class to your input field, but from set_value(), another CI helper function. The variable $first_name is sent as an array to the view, hence the error message.

As we don’t know, which array value you need, you could try to access the first one with set_value('first_name', $first_name[0]), but this depends how your array (or array of objects) is structured, a echo '<pre>';print_r($first_name); die(); helps you to debug this.

CI set_value() is defined as described here:

set_value($field[, $default = ''[, $html_escape = TRUE]])

Parameters:

  • $field (string) – Field name
  • $default (string) – Default value
  • $html_escape (bool) – Whether to turn off HTML escaping of the value
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement