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.
Related
I have a master view and depending on the URL and controller, it will load in another subview to a variable called $content, that's the idea.
Currently I am trying with:
return view("master")->with(["content" => view("pages.group")]);
So for example, if the URL is https://example.com/group/1 I am trying to get the subview included on my master template. Currently, it just gets escaped for XSS but I feel like this isn't the right way to do this?
I assume you are trying to display the sub-view content in the follwing way:
{{ $content }}
Change your syntax from {{ }} (Escaped output) to {!! !!} (Non escaped output).
{!! $content !!}
In the end after #lagbox mentioned it, using Laravel sections enabled me to use a master view and extend it when needed. https://laravel.com/docs/6.x/blade#extending-a-layout
I am using Laravel 5.1. In form I am generating drop down as:
{!! Form::select('ptype', $p_types,null,['class' => 'form-control text-capitalize']) !!}
While in controller $p_types is set as:
$p_types = PType::lists('name', 'id');
I want to show an option as Select here on top of drop down. How do I do tat?
There is no simple solution to doing what you're asking, as mentioned here. Form:recipes add placeholder attribute What you can do is in the lists method insert a record into the collection before it's passed to the blade template. Checkout this link Collection Methods. That is by far the easiest solution.
$p_types->push("Select Here");
You can also do like this in blade template to add the Select here option on top of drop down
{!! Form::select('ptype',([''=>'Select here']+$p_types->toArray()) ,null,['class' => 'form-control text-capitalize']) !!}
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'])
On a dashboard page, I've created a select list in a form that lists the names of components; the value that's passed from the select list is obviously the component id. On pressing submit, the user is routed to a page that displays the data about that component. Should be dirt simple...
Controller:
public function showDashboard()
{
$components = Component::lists('name','id'); ...
return View::make('dashboard', array('components'=>$components, ...))
}
dashboard.blade.php:
{{ Form::open(array('route' => array('components.show', $components->id), 'method'=>'get')) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::submit('Show Component', array('class'=>'button')) }}
{{ Form::close() }}
I've tried various ways of doing this, and get a different error every time. The above code doesn't even let me display the dashboard page -- I get a "Trying to get property of non-object" error. Clearly, it's not liking $components because that was passed as a list array and not an object. As I said, I'm sure this is dirt simple, I just can't figure out the proper syntax, and Laravel docs aren't giving me the answer. Thanks!
The problem isn't the dropdown, or the lists method, but rather in your form opening. Here, you have $components->id as an argument to the route, but $components is an array and you can't access an id property on it.
Finally figured this out. I had posted a similar question here subsequent to this one, and rather than repeat the answer, it is here:
How to pass id value from select list to controller when controller expects an object? laravel-4
The very short version: change Route::get to Route::post. Details with code in the link above. Problem solved!
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