MasoniteFramework / masonite

The Modern And Developer Centric Python Web Framework. Be sure to read the documentation and join the Discord channel for questions: https://discord.gg/TwKeFahmPZ
http://docs.masoniteproject.com
MIT License
2.21k stars 126 forks source link

Session errors and View 'errors()' helper always empty #816

Closed circulon closed 1 month ago

circulon commented 3 months ago

Describe the bug

The docs say that you can get any errors in 2 different ways.

{{ session().get("errors") }}
# or
{{ errors().all() }}

Neither of these currently work as the SessionMiddleware does the following

gets the session 'errors' (if any) which exist in the session flash and so are erased after the 'get' adds a helper to the View called 'bag()'. which is not documented or intuitive Additionally: This impacts the `SharedErrorsInSessionMiddleware' because the session flash 'errors' key has already been removed by the SessionMiddleware

Current behaviour: snippet from template: index.html this assumes you have added errors in the conrroller and expect to display with them in the View template

...
session errors: {{ session().get('errors') }} {# always returns None #}                  
<br>                
session bag: {{ session().get('bag') }} {# always returns None #}
{# just proving there is no session var called 'bag' #}                  
<br>                  
view helper errors(): {# {{ errors().all() }} #} {# throws "errors() is undefined when uncommented #}                 
<br>                  
view helper bag(): {{ bag().all() }} {# prints the messages expected to be in errors() helper #}
 ...

Expected behaviour

As per the docs https://docs.masoniteproject.com/features/validation#displaying-errors-in-views I would expect the following

...
session errors: {{ session().get('errors') }} {# shows  whatever messages I put in there #}                  
<br>                
session bag: {{ session().get('bag') }} {# always returns None #}
{# just proving there is no session var called 'bag' #}                  
<br>                  
view helper errors(): {{ errors().all() }}  {# prints all the messages in the message bag #}                 
<br>                  
view helper bag(): {# {{ bag().all() }} #} {# throws "bag()" is undefined when uncommented #}
 ...

Additionally: 
if `SharedErrorsInSessionMiddleware` is used
```python
# in template index.html
...
shared session errors: {{ errors.all() }} {# should show the same messages as the errors() view helper #}
...

Steps to reproduce the bug

as noted issue description above for a View template

Screenshots

No response

OS

macOS

OS version

Monterey 12.6

Browser

Chrome 127.0.6533.120

Masonite Version

4.20.0 with additional fixes from 4.0 branch

Anything else ?

Just a thought here but as we have this errors() helper in the View, isnt the SharedErrorsInSessionMiddleware providing a var in the View calles errors kind of redundant as ther provide the same content in a MessageBag?