# Validation

## Introduction

Laravel Bash provides a single function to validate your application's incoming data, and it uses Bash builtins only: `${#value}` for lengths and `[[ =~ ]]` for patterns.

## Validation Quickstart

```bash
PostController::store() {
  if ! validate title  "required|min:3|max:80" \
                body   "required|min:20" \
                status "required|in:published,draft"; then
    status 422
    view posts.create title="New post"
    return
  fi

  local id
  id=$(Post::create title="$(request title)" body="$(request body)" status="$(request status)")
  redirect "/posts/$id"
}
```

`validate` takes pairs of a field and its rules. It returns 0 if everything passes, and 1 if anything fails, which is what `if !` is for.

### Displaying the errors

Messages are put in the `ERRORS` associative array, keyed by field, and `error` prints the first one in a `<p class="error">`. `old` prints the submitted value back, escaped:

```bash
cat <<HTML
<input name="title" value="$(old title)">
$(error title)
HTML
```

## Form Requests

For anything bigger than a field or two, move the rules into a form request in `app/Http/Requests`:

```bash
# app/Http/Requests/StorePostRequest.sh
StorePostRequest::rules() {
  cat <<'RULES'
title   required|min:3|max:80
body    required|min:20
status  required|in:published,draft
RULES
}
```

One field per line, then its rules. Generate one with `./sail artisan make:request StorePostRequest`. Then in the controller:

```bash
PostController::store() {
  if ! form_request StorePostRequest; then
    status 422
    view posts.create title="New post"
    return
  fi

  local id
  local -a data
  validated data
  id=$(Post::create "${data[@]}")
  redirect "/posts/$id"
}
```

`validated data` fills the array `data` with `field=value` for every field that was validated, like `$request->validated()`. Anything else in the request is left out, so a form that also posts `votes=1000000` doesn't get to set the votes. The model's [`fillable`](/docs/eloquent#mass-assignment) list is the second line of defence.

## Available Validation Rules

| Rule | Passes when | Built with |
|---|---|---|
| `required` | It isn't empty or only whitespace | `${value//[[:space:]]/}` |
| `min:n` | It's at least *n* characters | `${#value}` |
| `max:n` | It's at most *n* characters | `${#value}` |
| `numeric` | It's a number | `[[ =~ ^-?[0-9]+(\.[0-9]+)?$ ]]` |
| `email` | It has an `@` and a dot after it | `[[ =~ ]]` |
| `alpha_dash` | Letters, numbers, `-` and `_` only | `[[ =~ ]]` |
| `in:a,b` | It's one of the listed values | `[[ ",a,b," == *",$value,"* ]]` |
| `regex:pattern` | It matches an extended regex | `[[ =~ ]]` |

> [!NOTE]
> `min` and `max` count characters, not bytes, because Bash's `${#value}` is locale-aware. `max:3` accepts `🐚🐚🐚`.

## Why not grep?

`grep -E` could check every rule, but it's a separate program, and each one costs a `fork` and an `exec`. A form with ten fields and three rules each would start thirty processes. `[[ =~ ]]` uses the same POSIX extended regular expressions without leaving Bash.

That also means no `\d`: extended regexes don't have it. Write `[0-9]`.
