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

97 Views Asked by At

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?

3

There are 3 best solutions below

0
sauhardnc On BEST ANSWER

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.

1
Shafeeque TP On
<div class="form-group">
    <?php $attributes = [
      '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>

try this

0
Razvan Zamfir On

Here is the working login form, for whoever needs an example:

<?php echo form_open("auth/login");?>

    <div class="form-group">
      <?php $identity['class'] = 'form-control';
            echo lang('login_identity_label', 'identity');
            echo form_input($identity);?>
    </div>

    <div class="form-group">
      <?php $password['class'] = 'form-control';
            echo lang('login_password_label', 'password');
            echo form_input($password);?>
    </div>

    <div class="form-group">
      <?php $remember['class'] = 'form-control';
            echo lang('login_remember_label', 'remember');
            echo form_checkbox('remember', '1', FALSE, 'id="remember" class="ml-2"');?>
    </div>

    <div class="form-group">
      <?php echo form_submit('submit', lang('login_submit_btn'), 'class="btn btn-block btn-md btn-primary"');?>        
    </div>

<?php echo form_close();?>