要预填充表单字段,我们可以将“值”添加到 create.blade.php 中的表单字段:
{{ Form::text('title', 'Some default title') }}
有没有办法在其他地方(也许在模型或控制器中?)完成该任务。我希望在创建和编辑视图中具有相同的表单字段代码。谢谢!
好的,我们到了...我在示例中使用了 Laravel 的表单模型绑定。 (我使用用户模型/数据库表)。
如果您不清楚这个主题,请看一下这个http://laravel.com/docs/html#form-model-binding http://laravel.com/docs/html#form-model-binding
// Controller
class UsersController extends BaseController
{
...
// Method to show 'create' form & initialize 'blank' user's object
public function create()
{
$user = new User;
return View::make('users.form', compact('user'));
}
// This method should store data sent form form (for new user)
public function store()
{
print_r(Input::all());
}
// Retrieve user's data from DB by given ID & show 'edit' form
public function edit($id)
{
$user = User::find($id);
return View::make('users.form', compact('user'));
}
// Here you should process form data for user that exists already.
// Modify/convert some input data maybe, save it then...
public function update($id)
{
$user = User::find($id);
print_r($user->toArray());
}
...
}
这里是控制器提供的视图文件。
// The view file - self describing I think
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
@if(!$user->id)
{{ Form::model($user, ['route' => 'admin.users.store']) }}
@else
{{ Form::model($user, ['route' => ['admin.users.update', $user->id], 'method' => 'put']) }}
@endif
{{ Form::text('firstName') }}
{{ Form::text('lastName') }}
{{ Form::submit() }}
{{ Form::close() }}
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)