Barret Wallace
Barret Wallace

Reputation: 155

Pass optional parameters to Laravel Form

This is an excerpt from my partials/textfield.blade.php snippet:

{{ Form::text('mylabel', 'mytext', ['class' =>'form-control']) }}

I know that it's possible to include this snippet and pass some variables to it:

@INCLUDE('partials/textfield', ['required' => 'required', 'autofocus' => 'autofocus'])

I want to be able to call the snippet with optional parameters for required, autofocusand readonly.

My question now is: How can I pass these variables to the form element?

I tried:

{{ Form::text('label',null,
    [
    'class' =>'form-control', 
    'required'  => (isset($required)  ? $required  : 'false'),
    'readonly'  => (isset($readonly)  ? $readonly  : 'false'), 
    'autofocus' => (isset($autofocus) ? $autofocus : 'false') 
    ])
}}

This leads to HTML code like ... readonly="false".... The presence of readonly, however, leads internet browsers to render the fields as readonly.

Replacing 'false'with ''doesn't help either. Does anyone know, how to pass these optional parameters to the snippet instead?

Any help is greatly appreciated!

Upvotes: 1

Views: 1325

Answers (2)

Dimitri Mostrey
Dimitri Mostrey

Reputation: 2355

{!! Form::input('text', 'email', null, [$required ? 'required' : '', $readonly ? 'readonly' : '', $autofocus? 'autofocus' : '']) !!}

Had to change my answer a bit. I'm used to kris/laravel-form-builder. I tried it, should work.

Upvotes: 1

FULL STACK DEV
FULL STACK DEV

Reputation: 15951

{{ Form::text('label',null,
    [
    'class' =>'form-control', 
    'required'  => (isset($required)  ? 'true'  : 'false'),
    'readonly'  => (isset($readonly)  ? 'true'  : 'false'), 
    'autofocus' => (isset($autofocus) ? 'true' : 'false') 
    ])
}}

You can set these values to true or false rather.

You will achieve the results.

another way is to

@INCLUDE('partials/textfield', [ 'options' => [   'class' =>'form-control', 'required' => 'required', 'autofocus' => 'autofocus']]);

Now you can simply

{{ Form::text('label',null,
    $options
    )
}}

by this way you dont have to check each and every field.

Upvotes: 0

Related Questions