How to pass value to Password field in Laravel - php

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.

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')

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'])

Related

How to programatically display text in a symfony page without an input field

I want to display different messages to a page built by Symfony depending on the situation. I initially set it up by creating a hidden field and setting the label to whatever message I want:
$builder->add('pageTopMsg', 'hidden', array(
'label' => $this->getPageTopMsg(),
'required' => false,
))
That works, but it doesn't feel right. Plus Symfony says I have to create getters and setters in an entity. The messages and the hidden field don't have any relationship with an entity. Is there a better way to display messages on a form dynamically.
To display a form field's value with twig:
{{ form.vars.value.pageTopMsg }}
"form" is the name of your form, and pageTopMsg is your field
But if you just want to show a text not related to you entity, you can pass it from the Controller and show it with {{ pageTopMsg }}
If the field does not have any relation with an entity, you can add this field directly in your Twig form and pass values through out your controller
$this -> render('entity/edit.hmtl.twig', array('someParameters' => array('val1', 'val2')));

Load empty value from model to Label in Laravel 5 Blade template

Trying to display a empty string from Model to html control in Laravel 5 Blade template.
{!!
Form::label('labelOccupation',$mastermodel->occupation,['style'=>'background-color:#BCBCBC'])
!!}
{!!
Form::text('textOccupation',$mastermodel->occupation,['style'=>'background-color:#BCBCBC'])
!!}
Both the text and label control can display value of occupation field correctly. But when the value is an empty string, the label control will display the wording "occupation", while the text control still able to show as empty.
Does it means I have to check empty string exists in Model every time when loading the value into label? Any other easier methods to handle such case?
You can do this way on your controller before passing the label on the form:
if(empty($mastermodel->occupation) {
$occupation_label = 'occupation';
} else {
$occupation_label = $mastermodel->occupation;
}
Then pass it to use in your blade:
{!! Form::label('labelOccupation',$occupation_label,['style'=>'background-color:#BCBCBC']) !!}
I think it's the easiest way to achieve this.

Using Form button/Eloquent to set an entire column to null

I am having a real issue getting a button to scan and clear out an entire column using an eloquent model. I have two columns in my SQLite DB, "States" and "Totals"... I want the States to stay in their own order, but I want the totals to be cleared out upon the user selecting a button. The character type for 'totals' is BigInt... After the user selects the button, I want them redirected to the home page (with the values cleared so they can start over).
Here are my routes:
Route::resource('states', 'StateController');
Route::get('/', 'StateController#index');
Route::post('create', 'StateController#store');
Route::post('states.update', 'StateController#update');
Here is my controller:
public function update()
{
Distributors::update(['total' => null]);
return View::make('states');
}
Here is my form:
{{ Form::open(['route' => 'states.update']) }}
{{ Form::submit('Destroy and Start Anew') }}
{{ Form::close() }}
The error I get is:
MethodNotAllowedHttpException
Is there a simple issue with my routes? I can't figure it out.
You did not specify a method attribute on your form, so it will automatically execute a GET request. Your state.update route is only setup to accept POST requests.
Change your form to this:
{{ Form::open(['route' => 'states.update', 'method' => 'post']) }}
Please delete
Route::resource('states', 'StateController');
in your route and try again.

Laravel 4.1 - passing resource ID from form <select> to controller method?

Sorry if this is a stupid question. I'm very new to laravel / MVC, and haven't had enough coffee today, so I wouldn't be surprised if the answer is sitting right in front of me. :)
background: I have a form with a select. The form is used to delete a "user" and all their associated resources from the database. The <select> is populated with unique ID's. on form submit, I would like to send a DELETE request to my Controller class, passing in the selected ID for deletion.
I can't figure out how to pass in the ID from the select, into the form. How do I make it so that when you select (for example) ID 1 in the drop down, that is passed into my resource routing on the form?
Here's some code:
{{ Form::open(array(
'url' =>'/clients',
'method'=>'delete',
'name' =>'delClient',
'role' =>'form',
'class' =>'form-horizontal')) }}
<h4>Please select the client you would like to delete:</h4>
{{
Form::selectField('delClientSelect', 'Client: ', array(
0=>'-- Select Client --')+$clientsList)
}}
{{
Form::submit('Delete Selected', array(
'class' => 'btn btn-danger confDelClient',
'data-role'=> 'delete'
))
}}
{{ Form::close() }}
Everything I read online says that you pass the id into the Form 'url' attribute, i.e:
{{
Form::open(array('url'=>'/clients/{id}'))
}}
but, as the ID is coming from the <select>, I'm not sure what the proper method is for getting the ID into my routing.
Thanks for any help!
If you are using a resource controller where you must use the DELETE verb to delete a record, then I would use jQuery to update the form URL/action to 'url'=>'/clients/{id}' as you stated.
Basically, on form submit (with jQuery that's $('form').submit()) you can append the ID from the select field to the forms url/action.
If you aren't using a resource controller, I would use the POST method, grab the ID from the select using Input::all() or Input::get() and then make your database call to delete the records from the database.

laravel have both Input::old('') and value of text input on form

I have a form that will pull data from the database as well as submit new data to overwrite the old all in the same fields. For example:
{{ Form::text('date', Input::old('date'), array('id' => 'date'))}}
Where the second parameter includes both the value from the database $i->date and also the input:old validator to ensure it wasn't left blank by accident.
Is there a way to do this? I already tried using an array as the second parameter.
Yes, you should consider form model binding Form::model instead of Form::open.
Also you can leave your input value alone:
{{ Form::text('date', null, array('id' => 'date'))}}
Controller side example:
$model = new Model;
return View::make('layout', compact('model'));
Way of opening the form:
{{ Form::model($model) }}
Not exactly sure what you are trying to do but will this work?
Form model binding
{{ Form::model($yourmodel, array('route' => array('yourmodel.update', $yourmodel->id))) }}
{{ Form::text('date', Input::old('date'), null, array('id' => 'date')) }}
from the docs
If there is an item in the Session flash data matching the input
name, that will take precedence over the model's value. So, the
priority looks like this:
Session Flash Data (Old Input) Explicitly Passed Value Model Attribute
Data

Categories