在laravel 4.2中显示特定的错误消息

时间:2015-09-30 09:16:28

标签: laravel laravel-4 laravel-validation

你好,我有这个表格,我有它的验证。我已经完成了在我的控制器内部的验证,我已经可以在我的视图中显示错误消息,但我希望错误消息位于它来自的输入区域旁边 这是我在视图中的代码

{{ Form::open(array('url' => 'addParentAccnt')) }}
    <div class="form-group">
        {{ Form::label('username', 'Username') }}
        {{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
    </div>
    <div class="form-group">
        {{ Form::label('fName', 'First Name') }}
        {{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
    </div>
    <div class="form-group">
        {{ Form::label('lName', 'Last Name') }}
        {{ Form::text('lName', Input::old('lName'), array('class' => 'form-control','placeholder' => 'Insert Last Name')) }}
    </div> {{ Form::submit('Proceed to Next Step', array('class' => 'btn btn-primary')) }}

{{ Form::close()}}

在我的视图底部,我添加了此代码以显示错误消息

@if ($errors->any())
<ul>
   {{ implode('', $errors->all('<p style="color:red"              class="error">:message</p>')) }}
</ul>
@endif

我的控制器中的代码就是这个

$rules = array
(
'username'     => 'required|min:10|max:50',
'fName'        => 'required|alpha|min:1|max:80',
'lName'        => 'required|alpha|min:1|max:80', 
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails()) 
{   
  return Redirect::to('createPa')
  ->withErrors($validator)
  ->withInput(Input::except('password'));
} 
else
{
   //do something
}

1 个答案:

答案 0 :(得分:2)

将视图更改为:

 <div class="form-group">
            {{ Form::label('username', 'Username') }}
            {{ Form::text('username', Input::old('username'), array('class' => 'form-control','placeholder' => 'Insert username')) }}
            {{ $errors->first('username', ':message') }}
        </div>
        <div class="form-group">
            {{ Form::label('fName', 'First Name') }}
            {{ Form::text('fName', Input::old('fName'), array('class' => 'form-control','placeholder' => 'Insert First Name')) }}
            {{ $errors->first('fName', ':message') }}
        </div>