doufei5315 2015-06-09 13:23
浏览 90
已采纳

仅在laravel 5中进行编辑时,才在表单模型绑定中添加readonly属性

I have 2 forms for editing and inserting in my project and I am using form model binding.

During insertion, there is a field that is required called code. This code is associated with the image of the product. So I want that I during the editing, the code field should become readOnly ?

How do I achieve this ?

insertion:

{!! Form::open(['url' => '/admin/products', 'autocomplete' => 'off', 'id' => 'formAddProduct', 'files' => true, 'name' => 'formAddProduct']) !!}
    <div class="errors"></div>

    @include('admin.products.form', ['submitButtonText' => 'Add Product', 'submitButtonId' => 'btnAddProduct'])

{!! Form::close() !!}

editing:

{!! Form::model($product, ['method' => 'PATCH', 'action' => ['AdminProductsController@update', $product->id], 'autocomplete' => 'off', 'id' => 'formEditProduct', 'files' => true]) !!}
    <div class="errors"></div>

    @include('admin.products.form', ['submitButtonText' => 'Edit Product', 'submitButtonId' => 'btnEditProduct'])
{!! Form::close() !!}

form.blade.php:

<div class="form-group">
    {!! Form::label('code', 'Code') !!}
    {!! Form::text('code', null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
    {!! Form::label('name', 'Name:') !!}
    {!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>

<div class="form-group">
    {!! Form::label('category_id', 'Category:') !!}
    {!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
  • 写回答

3条回答 默认 最新

  • dragoninasia2014 2015-06-09 13:31
    关注

    While including the form in the editing view, you can pass another parameter as follows:

    editing:

    {!! Form::model($product, ['method' => 'PATCH', 'action' => ['AdminProductsController@update', $product->id], 'autocomplete' => 'off', 'id' => 'formEditProduct', 'files' => true]) !!}
        <div class="errors"></div>
        @include('admin.products.form', ['submitButtonText' => 'Edit Product', 'submitButtonId' => 'btnEditProduct', 'editMode' => true])
    {!! Form::close() !!}
    

    Then in the included form you can do check for this parameter and render the input field accordingly,

    form.blade.php

        <div class="form-group">
            {!! Form::label('code', 'Code') !!}
        @if(isset($editMode))
            {!! Form::text('code', null, ['class' => 'form-control', 'readonly' => true]) !!}
        @else
            {!! Form::text('code', null, ['class' => 'form-control']) !!}
        </div>
    

    This way, you have the input field being readOnly if editing, else writable.

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效