user3752276
user3752276

Reputation: 231

laravel 5 assign variables to forms fields

I want to set the placeholder value for my form element. Here is my controller:

class MyController extends Controller {
 public function index(Request $request){
   $start_time = $request->get('start_date');
   return view('showChart',compact('start_time'));
 }
}

The controller will send response to showChart.blade.php. Part of the showChart.blade.php is:

{!! Form::open(array('url' => '/', 'class' => 'form')) !!}
{!! Form::label('START DATE') !!}
            {!! Form::text('start_date', null, 
                      array('required',  
                        'placeholder'=>{{start_time or 'default value'}}
                      ))
            !!}
{!! Form::close() !!}

It does not work as expected, it will output: , it seems that the code does not execute.

Upvotes: 0

Views: 1880

Answers (2)

Martin Bean
Martin Bean

Reputation: 39389

You can’t use template delimiters once inside an already-open set. You’ll have to do something like this instead:

{!! Form::text('start_date', null, ['placeholder' => empty($start_date) ? 'default value' : $start_date, 'required' ]) !!}

Upvotes: 2

hemir
hemir

Reputation: 93

I'm not sure if i understand every thing but did you have a white screen ? That means a blade error.

{!! Form::text('start_date', null, 
                      array('required',  
                        'placeholder'=>{{start_time or 'default value'}}
                      ))
            !!}

Remove {{ }} and replace start_time by $start_time. And about your default value, you can put it into $start_time in your controller or before in your view.

Upvotes: 0

Related Questions