如何在出错时将类添加到输入字段

时间:2013-12-03 12:51:40

标签: php laravel laravel-4

Laravel 4中是否有机会简单地添加一个类,例如验证错误后,表单字段“错误”? 我认为Form Helper会这样做......

由于 此致

1 个答案:

答案 0 :(得分:2)

是的,您只需在输出表单字段时检查字段上某个错误的错误,如果有错误,请为其指定error类。我不确定你的变量名,但希望你能得到这个想法......

// Some validation
$validator = Validate::make($input, $rules);

// If it fails, pass errors into view.  
// This could be confusing, you should check http://laravel.com/docs/validation#error-messages-and-views for more info
if($validator->fails()) {
    return View::make('someform')->withErrors($validator);
}

// withErrors() will flash the validation messages to an errors variable.  
//This is just some shorthand syntax that's checking for an error on email and if there is something, it will give the class of error else it will give a blank class
{{ Form::text('email', array('class' => $errors->has('email') ? 'error' : '')) }}