Laravel 4:无法在编辑表单上使用表单宏获取模型数据

问题描述:

On edit forms I have a problem to populate form fields with data stored in DB.

When I use the code snippet from the doc everything's fine. However I need to append a css class to the form field, and this cannot be passed as argument for:

{{ Form::text('email') }}

So I've created the below Form::macro

Form::macro('textclass', function($name, $class = null)
{
    return '<input type="text" name="'.$name.'" id="'.$name.'" class="'.$class.'"/>';
});

So I can use this on my views:

{{ Form::textclass('email', 'm-wrap span12') }}

But then the field doesn't get populated with the model data.

Any idea on how to fix this?

For reference if you want to check what the Form generates you can look up vendor/laravel/framework/src/Illuminate/Html/FormBuilder.

In the FormBuilder class you'll find methods for the inputs. The method for the text input looks like this

 public function text($name, $value = null, $options = array())
 {
    return $this->input('text', $name, $value, $options);
 } 

As you can see the second parameter is the value and the third is the options array.

So for your form it would look like this

Form::text('email', null, array('class'=>'m-wrap span12'))