marvinlabs / laravel-html-bootstrap-4

Bootstrap 4 fluent HTML builder
MIT License
42 stars 15 forks source link

Validation Errors do not show up when form is opened inside a blade layout #25

Closed JamesPoel closed 6 years ago

JamesPoel commented 6 years ago

Hi This is great, but how do you show validation errors against the fields? (.is-invalid, .invalid-feedback etc?) I see references to it within the package, but I can't find any example of implementing it. Thanks

vpratfr commented 6 years ago

You should not have anything special to do. Errors will be shown automatically when returning to the form (package checks old input and errors).

You have sample usage on my package workbench project:

JamesPoel commented 6 years ago

Ah, I've found the issue.

The errors don't show up if you open the form in one blade file and then extend this file and include the fields using @yield/@section.

This is because, when the form is built this way, @this->formState (ln 38 of Input.php, for example), is set to null: https://github.com/marvinlabs/laravel-html-bootstrap-4/blob/master/src/Bootstrap/Elements/Input.php#L38

Which causes ln 77-78 to be falsey: https://github.com/marvinlabs/laravel-html-bootstrap-4/blob/master/src/Bootstrap/Elements/Input.php#L77

E.g.

page1.blade.php:

{{ bs()->openForm('post', '/' ) }}
@yield( 'form' )
{{ bs()->submit( 'Submit' ) }}
{{ bs()->closeForm() }}

page2.blade.php:

@extends( 'page1' )
@section( 'form' )
        {{ bs()->formGroup( bs()->text( 'test' ) )->label( 'Test' ) }}
@endsection

Could this perhaps be looked into?

vpratfr commented 6 years ago

Hi,

Looks like in that case, closeForm is called before the section content is even inserted.

Not sure if that can be fixed as it seems to be how layouts work in Blade.

Would be happy to accept a PR if you find a fix.

vpratfr commented 6 years ago

Closing this for now, if you have a PR to fix that, I'd review it happily.

JamesPoel commented 6 years ago

A rubbish workaround, but this works. If you call bs()->openForm( ... ) in the parent view, but call bs()->closeForm() in the child view, the validation works.