Search code examples
codeignitercodeigniter-3ion-auth

Codeigniter 3 application bug: adding atributes to Ion-Auth form elements fails


I am working on an application that requires authentication, in Codeigniter 3. Rather then making my own authentication, I decided to use the Ion-Auth library.

Because I also use Bootstrap 4, I tried to add form-related classes to the form controls. I tried to replace:

<p>
  <label for="new_password"><?php echo sprintf(lang('change_password_new_password_label'), $min_password_length);?></label> <br />
    <?php echo form_input($new_password);?>
</p>

with:

<div class="form-group">
    <?php $attributes = array(
      'class'       => 'form-control',
      'placeholder' => 'New password',
      'value'       => $new_password
    ); ?>
    <label for="new_password"><?php echo sprintf(lang('change_password_new_password_label'), $min_password_length);?></label> <br />
    <?php echo form_input($attributes);?>
  </div>

The code above throws the error message Array to string conversion.

What am I doing wrong?


Solution

  • You haven't specified what function you're calling but after looking Ion-Auth library, I'm guessing it's reset_password(). Regardless,

    In your controller, the data is passed as -

    $this->data['new_password'] = [
                                    'name' => 'new',
                                    'id' => 'new',
                                    'type' => 'password',
                                    'pattern' => '^.{' . $this->data['min_password_length'] . '}.*$',
                                  ];
    

    Now $new_password is an array in view. So, in order to give it value and pass extra attributes, you'll have to write -

    <?php 
        $new_password['class'] = 'form-control';
        $new_password['placeholder'] = 'New password'; 
        $new_password['value'] = $_POST['new'] ?? ''; // if post request then submitted value else empty
    
        echo form_input($new_password);
    ?>
    

    This will produce -

    <input type="password" name="new" value="123456" id="new" pattern="^.{4}.*$" class="form-control" placeholder="New password">
    

    Edit

    For form_submit, you can write -

    echo form_submit('submit', 'submit', 'class="btn btn-block btn-md btn-success"');
    

    or as an array -

    echo form_submit(array(
                             'name'=>'submit',
                             'value' => 'submit', 
                             'class' => 'btn btn-block btn-md btn-success'
                          )
                     );
    

    This will produce -

    <input type="submit" name="submit" value="submit" class="btn btn-block btn-md btn-success">
    

    See if this helps you.