【问题标题】:Laravel form model binding and html input fieldsLaravel 表单模型绑定和 html 输入字段
【发布时间】:2013-09-13 05:59:28
【问题描述】:

我在 Laravel 中使用表单模型绑定来更新视图以实现优先级 1. 会话闪存数据(旧输入) 2. 显式传递值 3.模型属性数据

{{ Form::model($model, array('url' => $route.'/update/'.$model->id)) }}
{{ Form::label('price', trans_choice('app.price', 1), array('id' => 'price_label')) }}
{{ Form::text('price', null, array('id' => 'price')) }}

这很好,但我想在不使用刀片表示法输入字段的情况下做同样的事情,也就是说我想替换

{{ Form::text('price', null, array('id' => 'price')) }} 

<input type="text" name="price" id="price" value="">

但仍然获得上述优先级,这可能吗?

【问题讨论】:

    标签: php laravel blade


    【解决方案1】:

    你可以试试这个:

    <input id="price" name="price" type="text" value="{{{ Form::getValueAttribute('price', null) }}}">
    

    这是Form::text调用的替换值的函数。

    Laravel 4 Form::getValueAttribute 函数:

    /**
     * Get the value that should be assigned to the field.
     *
     * @param  string  $name
     * @param  string  $value
     * @return string
     */
    public function getValueAttribute($name, $value = null)
    {
        if (is_null($name)) return $value;
    
        if ( ! is_null($this->old($name)))
        {
            return $this->old($name);
        }
    
        if ( ! is_null($value)) return $value;
    
        if (isset($this->model))
        {
            return $this->getModelValueAttribute($name);
        }
    }
    

    【讨论】:

    • 感谢您的建议,但这实际上会覆盖模型的默认值
    • 不会覆盖模型的默认值……其实就是模型用来设置值的函数。
    猜你喜欢
    • 2015-06-07
    • 1970-01-01
    • 2021-12-14
    • 2013-05-28
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多