如何在错误时将类添加到输入字段 [英] How to add class to input field on error
本文介绍了如何在错误时将类添加到输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Laravel 4是否有机会简单地添加一个类,例如验证错误后错误"到表单字段? 我以为Form Helper会做到这一点...
is there any chance in Laravel 4 to simply add a class e.g. "error" to a form field after validation error? I thought the Form Helper will do that...
谢谢 问候
推荐答案
是的,您只需要在输出表单字段时检查针对某个字段的错误即可,如果有错误,请给它一个类error
.我不确定您的变量名,但希望您能理解...
Yeah, you just have to check your errors for a certain error on a field when outputting the form field, and if it has an error, give it the class of error
. I'm not sure of your variable names, but hopefully you get the idea...
// 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' : '')) }}
这篇关于如何在错误时将类添加到输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文