Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update validation.md #244

Open
wants to merge 2 commits into
base: v4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions features/csrf.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ You can get also get the token that is generated. This is useful for JS frontend
<p> Token: {{ csrf_token }} </p>
```

{% hint style="info" %}
If you encounter an 'Invalid CSRF Token' error while running the development server, ensure that the domains in your .env file and config/application.py match the domain used by the development server (e.g., both should use either 127.0.0.1 or localhost).
{% endhint %}


## AJAX / Vue / Axios

For ajax calls, the best way to pass CSRF tokens is by setting the token inside a parent template inside a `meta` tag like this:
Expand Down
42 changes: 21 additions & 21 deletions features/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ If you want to handle errors in views specifically you will need to add the `Sha
route middlewares. `errors` will be injected to views as a [MessageBag](#message-bag) instance allowing to handle errors easily:

```html
@if errors.any():
@if bag().any():
<div class="bg-yellow-400">
<div class="bg-yellow-200 text-yellow-800 px-4 py-2">
<ul>
@for key, message in errors.all().items()
@for key, message in bag().all().items()
<li>{{ message }}</li>
@endfor
</ul>
Expand All @@ -81,22 +81,22 @@ route middlewares. `errors` will be injected to views as a [MessageBag](#message
<div>
<label for="name">Name</label>
<input type="text" name="name" placeholder="Name">
@if errors.has('name')
<span>{{ errors.get('name')[0] }}</span>
@if bag().has('name')
<span>{{ bag().get('name')[0] }}</span>
@endif
</div>
<div>
<label for="email">Email</label>
<input type="email" name="email" placeholder="Email">
@if errors.has('email')
<span>{{ errors.get('email')[0] }}</span>
@if bag().has('email')
<span>{{ bag().get('email')[0] }}</span>
@endif
</div>
<div>
<label for="message">Message</label>
<textarea name="message" placeholder="Message"></textarea>
@if errors.has('message')
<span>{{ errors.get('message')[0] }}</span>
@if bag().has('message')
<span>{{ bag().get('message')[0] }}</span>
@endif
</div>
<button type="submit">Send</button>
Expand Down Expand Up @@ -448,7 +448,7 @@ def show(self, request: Request):
You can easily get all errors using the `all()` method:

```python
errors.all()
bag().all()
"""
{
'email': ['Your email is required'],
Expand All @@ -460,34 +460,34 @@ errors.all()
## Checking for any errors

```python
errors.any() #== True
bag().any() #== True
```

## Checking if the bag is Empty

This is just the opposite of the `any()` method.

```python
errors.empty() #== False
bag().empty() #== False
```

## Checking For a Specific Error

```python
errors.has('email') #== True
bag().has('email') #== True
```

## Getting the first Key:

```python
errors.all()
bag().all()
"""
{
'email': ['Your email is required'],
'name': ['Your name is required']
}
"""
errors.first()
bag().first()
"""
{
'email': ['Your email is required']
Expand All @@ -498,13 +498,13 @@ errors.first()
## Getting the Number of Errors:

```python
errors.count() #== 2
bag().count() #== 2
```

## Converting to JSON

```python
errors.json()
bag().json()
"""
'{"email": ["Your email is required"],"name": ["Your name is required"]}'
"""
Expand All @@ -513,13 +513,13 @@ errors.json()
## Get the Amount of Messages:

```python
errors.amount('email') #== 1
bag().amount('email') #== 1
```

## Get the Messages:

```python
errors.get('email')
bag().get('email')
"""
['Your email is required']
"""
Expand All @@ -528,7 +528,7 @@ errors.get('email')
## Get the Errors

```python
errors.errors()
bag().errors()
"""
['email', 'name']
"""
Expand All @@ -537,7 +537,7 @@ errors.errors()
## Get all the Messages:

```python
errors.messages()
bag().messages()
"""
['Your email is required', 'Your name is required']
"""
Expand All @@ -548,7 +548,7 @@ errors.messages()
You can also merge an existing dictionary into the bag with the errors:

```python
errors.merge({'key': 'value'})
bag().merge({'key': 'value'})
```

# Nested Validations
Expand Down