How to add class to input field on error

谁说我不能喝 提交于 2020-01-04 05:52:05

问题


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...

Thanks Regards


回答1:


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' : '')) }}


来源:https://stackoverflow.com/questions/20351737/how-to-add-class-to-input-field-on-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!