How to pass value to Password field in Laravel

2020-07-27 05:22发布

问题:

I am trying to fetch value of the password field and display it in current form for one of the update pages

{{ Form::label('Password')}} 

{{ Form::password('password',array('class' => 'form-control') }}

Laravel blade's password field syntax does not allow parameter for input like it does for text fields for example,

{{ Form::label('Email or Username')}} 

{{ Form::text('email',$useremaildata,array('class' => 'form-control')) }} 

Now I found one solution of using placeholders like that but it is not the rite way of doing it. Sharing just incase.

{{ Form::label('Password')}} 

{{ Form::password('password',array('class' => 'form-control','placeholder' => $userpassworddata)) }}

Any help will be great.

回答1:

The simple answer is that you don't do this. Password fields should rarely be pre-filled and so because it's not a common occurrence Laravel doesn't support it.

However, if you really want to do this you can use Form::input():

Form::input('password', 'name', 'value')


回答2:

I think there are valid use cases for this. In my case, I am storing the password for another system, and when the user edits, I want to load the prior value from the database and fill it, so it doesn't get cleared when the user saves.

Here's a custom HTML macro that allows you to set a default value in the password field.

In start/global.php do:

/**
 * Custom macro for a masked password field with a default value
 */
 HTML::macro('passwordWithDefault', function($name, $defaultValue = "", $optionsArray) {    
    return Form::input('password', $name, $defaultValue, $optionsArray);
 });

Then in your HTML or template, you can use it like so:

HTML::passwordWithDefault('pwdField', Input::old('pwdField', $pwd_value),['class' => 'form-control'])