我有一张表格。当用户提交表单并收到错误时,我会这样显示:
注册控制器
return View::make('theme-admin.user_add')->with('error_msg', validation->errors->first());
register.blade.php
@if($error_msg !== null)
<div class="alert red hideit max">
<div class="left">
<span class="red-icon"></span>
<span class="alert-text">{{ $error_msg }}</span> <-- Error message is visible here.
</div>
<div class="right">
<a class="close-red"></a>
</div>
</div>
@endif
//
Actual HTML Form
//
但是,我想将该错误div移动到刀片文件中。 (error.blade.php)我想在出现错误时用参数调用它。
看起来像这样。
NEW register.blade.php
{{ MESSAGE_CONTENT }} //This should be replaced with error.blade.php dynamically
//
Actual HTML Form
//
MESSAGE_CONTENT将通过error.blade.php
包含在内error.blade.php
<div class="alert red hideit max">
<div class="left">
<span class="red-icon"></span>
<span class="alert-text">{{ $message }}</span> <-- Error message is visible here.
</div>
<div class="right">
<a class="close-red"></a>
</div>
</div>
假设表单失败,我遇到了一些错误。我将加载error.blade.php,以便消息获得RED背景等。
像这样;
return View::make('theme-admin.user_add')->with(message_content', (Load error.blade.php here))->with('message', $validation->errors->first();
如果表单成功,我只需在消息区域加载success.blade.php,消息将以绿色背景显示。
return View::make('theme-admin.user_add')->with(message_content', (Load success.blade.php here))->with('message', 'You successfully registered');
你可能有逻辑。
我该怎么做?
聚苯乙烯。图片示例:http://i.imgur.com/QExAiuA.png
答案 0 :(得分:1)
一个干净的解决方案可能是拥有一个带有类型和信息的简单警报对象。
//in controller
$alert->type = 'error'; // or 'success'
$alert->class = 'red'; // or 'green'
$alert->msg = $validation->errors->first(); // or 'You successfully registered'
return View::make('theme-admin.user_add')->with('alert', $alert);
//register.blade.php
@include('alert')
//Actual HTML Form
//alert.blade.php
@if(isset($alert))
<div class="alert {{$alert->class}} hideit max">
<div class="left">
<span class="red-icon"></span>
<span class="alert-text">{{ $alert->msg }}</span>
</div>
<div class="right">
<a class="close-{{$alert->class}}"></a>
</div>
</div>
@endif
答案 1 :(得分:1)
您应该在为GET定义的路由中创建视图(View::make()
),然后在POST路径中处理表单输入:
//routes.php
Route::get('admin/adduser', array('as' => 'adduser', 'do' => function()
{
return View::make('theme-admin.user_add');
}));
//route for handling form input
Route::post('register', array('before' => 'csrf', function()
{
$rules = array(
//your vailidation rules here..
);
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails())
{
//re-populate form if invalid values, so add flashing input by with_input()
//also pass errors by using with_errors
return Redirect::to('adduser')->with_input()->with_errors($validation);
}
else {
//use the named route we defined in Route:get above
return Redirect:to('adduser')->with('message', 'Successfully added the new user!');
}
}));
无需创建新视图来消除错误和成功消息。如果您想将您的成功和错误分离到他们自己的刀片模板中,那么您可以在adduser.blade.php中使用以下内容:
//adduser.blade.php
@if($errors->has())
@include('errormsg'); //use {{ $errors->first() }} or @foreach($errors->all() as $message) to print error message(s)
@else
@include('success'); //use {{ $message }} to print success
@endif
但是,您也可以在视图中使用部分,而是将成功和错误消息放在同一视图中:
//feedback.blade.php
@section('error')
<em class="error">{{ $errors->first() }}</em>
@endsection
@section('success')
<em class="success">{{ $message }}</em>
@endsection
//adduser.blade.php
@include('feedback');
@if($errors->has())
@yield('error');
@else
@yield('success');
@endif
希望有所帮助。
答案 2 :(得分:1)
我自己找到了解决方案。我需要嵌套视图功能。
$view = View::make('home');
$view->nest('content', 'orders', array('orders' => $orders));
有关详细信息,请参阅Laravel文档。