This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

JimRed
JimRed

Hello Guys,

I've made a controller for a model and there is a password field in the form. The password column is filled in the database table but when I try to update the record in the form the field is empty.
How can I set to retreive information from password fields in the update form page?

Thank you for your help!

Shahiem
Shahiem

Can I see your code? Maybe I can help you :)

JimRed
JimRed

Hi,

Of course you can, but I'm not sure which part of my code you would like to see but here is my model:

<?php namespace Codebugs\Auth\Models;

use Model;
use Codebugs\Master\Models\Site;

/**
 * User Model
 */
class User extends Model
{
    use \October\Rain\Database\Traits\Validation;
    use \October\Rain\Database\Traits\Hashable;

    /**
     * @var string The database table used by the model.
     */
    public $table = 'codebugs_auth_users';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Hashable fields
     */
    protected $hashable = ['password'];

    /**
     * Validate model fields by these rules
     * @var array
     */
    protected $rules = [
        'full_name' => 'required',
        'username'  => 'required|unique:codebugs_auth_users,username',
        'password'  => 'required',
        'email'     => 'required|email',
        'site_id'   => 'required'
    ];

    /**
     * @var array Relations
     */
    public $hasOne = [];
    public $hasMany = [];
    public $belongsTo = [
        'site' => ['Codebugs\Master\Models\Site', 'primaryKey' => 'id']
    ];
    public $belongsToMany = [];
    public $morphTo = [];
    public $morphOne = [];
    public $morphMany = [];
    public $attachOne = [];
    public $attachMany = [];

    public function getSiteIdOptions($keyValue = null)
    {
        return Site::where('status','Live')->orderby('name')->lists('name','id');
    }

}

...and here is my fields.yaml

# ===================================
#  Form Field Definitions
# ===================================

fields:
    full_name:
        label: Full name
        type: text
        span: auto

    site_id:
        label: Site
        type: dropdown
        span: auto

    username:
        label: Username
        type: text
        span: auto

    password:
        label: Password
        type: password
        span: auto

    email:
        label: E-mail address
        type: text
        span: auto

    phone:
        label: Phone number
        label: text
        span: auto

    is_enabled:
        label: Enabled
        type: checkbox
        default: true

If you should see more please just let me know.
Thank you!

alxy
alxy

You cant display passwords, they are by default excluded from beeing prefilled. This has good reasons: Primarily, it is hashed, so it wouldnt make any sense to display it (you can't recreate the password from the hash!). On top of that, for security reasons passwords should never be displayed in plain text ;)

JimRed
JimRed

I understand but what is the best practice in such case?
I mean I should see the password field in the form (it does not matter if I see it empty) and if I give it some value it should update the database field but if I leave it empty it should do not update the password database field but update any other field I changed on form.
How can I change the behavior of the form to make it?

Last updated

dmitry20410
dmitry20410

@alxy, sorry for bumping this post, but I've just come across the same issue. Is there a solution to it? Basically, the goal here is to allow updating the form without re-entering the password every time.

PolloZen
PolloZen

to avoid the password is updated every time you save the form, you can verify if the password field has a change before you update de model

public function beforeUpdate()
{
    if($this->attributes['password']=='') {
        unset($this->attributes['password']);
    }
}

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.