Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: username input form shows root as a default value #4062

Closed
joancomaz opened this issue Jan 4, 2021 · 3 comments
Closed

Bug: username input form shows root as a default value #4062

joancomaz opened this issue Jan 4, 2021 · 3 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@joancomaz
Copy link

Describe the bug
By default, it displays the user and password values ​​from the Database.php database configuration file in the user input and password fields of any custom view.

CodeIgniter 4 version
4.0.4

Expected behavior, and steps to reproduce if appropriate
View code:
<input class="form-control" type="text" name="username" id="username" value="<?= $data->username ?>" />
The input field show root when the data is empty, because the username of my database config is root!

Context

  • OS: Windows 10
  • Apache 2.4.43
  • PHP version 7.4.6
@joancomaz joancomaz added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 4, 2021
@MGatner
Copy link
Member

MGatner commented Jan 5, 2021

If I understand correctly, you are saying that $data is somehow receiving values from your database config? Please include code samples of your controller (or whatever is calling this view). Also please test the same code against the latest develop branch of the framework as there have been hundreds of bug fixes and changes. I would also recommend you try using var_dump() to confirm the actual type and contents of $data.

@joancomaz
Copy link
Author

Yes, it is. I post code snippets below:

Controller:

public function new() {
  $view = 'new';
  $data = [
    'user' => new UserModel(),
  ];

  echo view("user/$view", $data);
}

Model:

class UserModel extends Model {

    protected $table = 'users';
    protected $primaryKey = 'id';

    protected $allowedFields = [
        'id',
        'username',
        'password'
    ];
}

View:

<form action="create" method="post" enctype="multipart/form-data">
    <?= csrf_field() ?>
    <div class="row">
        <div class="col">
            <div class="form-group">
                <label for="name">Username</label>
                <input class="form-control" type="text" name="username" id="username" value="<?= old('username', $user->username) ?>" /> 
            </div>
        </div>
        <div class="col">
            <div class="form-group">
                <label for="name">Password</label>
                <input class="form-control" type="password" name="password" id="password" value="<?= old('password', $user->password) ?>" /> 
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col">
            <button type="submit" class="btn btn-primary" name="submit">
                <i class="fas fa-save"></i>
                Save
            </button>
        </div>
    </div>
</form>

@MGatner
Copy link
Member

MGatner commented Jan 7, 2021

You are using a Model where you want a result or Entity. Your model connects to the database and retrieves the rows. It also has magic get function to cascade into its database connection, so when you ask for "UserModel->password" it is actually retrieving your database connection password, just like you asked.

Check the User Guide section on using Models.

@MGatner MGatner closed this as completed Jan 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

2 participants