[Proposal] Make FormBuilder class error-aware
It would be great if the FormBuilder was somehow error aware. Labels and input fields would automatically get a class attribute added (class="error") so that the user can then style the label and field error as he wishes. (Red color / background / ...)
This is not possible to do at the moment and I think it would be a great addition since so much time of development is spent in forms.
I have no code for this unfortunately, it is merely a suggestion and a point of discussion.
I think we can still do it by
echo Form::label('email', 'E-Mail Address', array('class' => 'awesome'));
echo Form::label('password', 'Password', array('class' => 'error'));
Pls correct me if I am not getting what you are meaning.
I think what he's getting at is automatically giving the Form element an error class when you redirect back to a form with errors. Kind of like how you don't have to have to refill old input. There are unfortunately a lot of approaches to actually displaying form errors so implementing this in a way that made everyone happy would be impossible.
-1 for the reasons @WMeldon pointed out. There's too much variation. I'd look at extending it yourself or using another extensible Form builder that uses Laravel like Former.
On the note of doing it yourself as opposed to using a bundle, View Composers would probably be your best bet.
They are great for this kind of thing.
@navnit - yes you can do it that way, but you'll have to have more code than that - you'll also have to check if that field has an error or not. This results in a lot of duplication.
Yes, you could probably extend the formbuilder yourself, but shouldn't there be a built-in way to handle this? We already have the error object. We can add the class and if the user does not want it, he simply does not implement the CSS rule. Almost all forms on the web have a visual representation of an invalid field, it's pretty standardized. I believe Rails has this too.
@wesleyh You are correct. Rails does ship with this. Rails also defines a standard for how form elements should be marked up.
By forcing an error class into the elements associated and telling people not to use it if they don't want to, Laravel has now crept into the way developers write style sheets.
Do you not agree that form handling is the most important thing a framework should handle? If yes, then shouldn't properly displaying those errors be something the framework handles too?
i second @wesleyh , we should make errors be easier to plugin to forms. for example passing the errors we would like to listen to as an array to Form::open()
im thinking of a suggestion to implement it.
Yeah would rather just leave this up to a package or something.
Ran across this issue - I also agree there should be an option to add an error class to forms (via the form builder) on validation errors but in the meantime, here is a quick and dirty javascript/blade snippet that should accomplish the same thing.
@if($errors)
$(document).ready(function() {
@foreach($errors->getMessages() as $field => $messages)
$('input[name="{{$field}}"]').addClass('error');
@endforeach
});
@endif