【问题标题】:Add readonly attribute in form model binding only when editing in laravel 5仅在 laravel 5 中编辑时在表单模型绑定中添加只读属性
【发布时间】:2015-06-09 13:23:12
【问题描述】:

我的项目中有 2 个用于编辑和插入的表单,并且我正在使用表单模型绑定。

在插入过程中,有一个名为code 的必填字段。此代码与产品的image 关联。所以我希望我在编辑过程中,code 字段应该变成readOnly?

我如何做到这一点?

插入:

{!! 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() !!}

编辑:

{!! 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>

【问题讨论】:

    标签: php jquery html laravel laravel-5


    【解决方案1】:

    在编辑视图中包含表单时,您可以传递另一个参数,如下所示:

    编辑:

    {!! 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() !!}
    

    然后在包含的表单中,您可以检查此参数并相应地呈现输入字段,

    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>
    

    这样,输入字段在编辑时为只读,否则为可写。

    【讨论】:

    • @user3514160 很高兴这对您有用。你能接受答案吗?谢谢。 :)
    【解决方案2】:

    您可以通过传递参数或手动禁用编辑视图中的输入字段来实现此目的:

    {!! Form::text('code', null, ['class' => 'form-control', 'disabled' => 'true']) !!}
    

    【讨论】:

      【解决方案3】:

      而不是重复许多元素。 在控制器上

      $readonly = [];
      
      if($edit)
       $readonly = ['readonly' => true];
      

      正在观看

      {!! Form::text('code', null, (['class' => 'form-control'] + $readonly)) !!}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-14
        • 2019-02-24
        • 2013-06-18
        • 2016-12-13
        • 1970-01-01
        相关资源
        最近更新 更多