2

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 Answer 1

2

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' : '')) }}
Sign up to request clarification or add additional context in comments.

2 Comments

at the moment I make the inputs like that: code{{Form::text('username', '', array('class' => 'form-control')) }}code so how can I extend my class="form-control" or is there any other way to add error class? Thanks
You can have multiple classes by having a space between them. So if you need form-control class, just add that to both scenarios. {{ Form::text('username', array('class' => $errors->has('username') ? 'form-control error' : 'form-control')) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.