douluanji8752 2018-04-17 10:01
浏览 79
已采纳

将可选参数传递给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!

  • 写回答

2条回答 默认 最新

  • dongwei3120 2018-04-17 10:45
    关注
    {!! 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验
  • ¥15 canal如何实现将mysql多张表(月表)采集入库到目标表中(一张表)?
  • ¥15 wpf ScrollViewer实现冻结左侧宽度w范围内的视图
  • ¥15 栅极驱动低侧烧毁MOSFET
  • ¥30 写segy数据时出错3
  • ¥100 linux下qt运行QCefView demo报错
  • ¥50 F1C100S下的红外解码IR_RX驱动问题