问题
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