Skip to content

Commit

Permalink
Merge pull request #31 from midnite81/feature/validation-wrapping
Browse files Browse the repository at this point in the history
feature/validation-wrapping
  • Loading branch information
midnite81 authored Sep 4, 2024
2 parents e24fad9 + b239d7c commit 33ab8d9
Show file tree
Hide file tree
Showing 6 changed files with 604 additions and 2 deletions.
146 changes: 146 additions & 0 deletions docs/Validation/ValidationExceptionBuilder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# ValidationExceptionBuilder

`ValidationExceptionBuilder` is a fluent interface for building and throwing validation exceptions in Laravel
applications. It provides a convenient way to create custom validation exceptions with redirects, error messages, and
additional parameters.

## Basic Usage

Here's a quick example of how to use the `ValidationExceptionBuilder`:

```php
use Midnite81\Core\Validation\ValidationExceptionBuilder;

ValidationExceptionBuilder::message('Invalid input')
->redirectTo('/form')
->flash('Please correct the errors and try again.')
->throwException();
```

This will throw a `ValidationException` with the message "Invalid input", redirect the user to '/form', and flash a
message to the session.

## API Reference

### Static Methods

#### `message(string $message): self`

Create a new ValidationExceptionBuilder instance with the specified error message.

### Instance Methods

#### `redirectTo(string $url): self`

Set the URL to redirect to after validation failure.

#### `redirectBack(): self`

Set the redirect to go back to the previous URL.

#### `redirectRoute(string $name, array $parameters = []): self`

Set a named route to redirect to after validation failure.

#### `fragment(string $fragment): self`

Set the URL fragment (hash) to append to the redirect URL.

#### `withQueryParameters(array $params): self`

Set query parameters to append to the redirect URL.

#### `errorBag(string $errorBag): self`

Set the error bag name for the validation exception.

#### `flash(?string $message = null, string $key = 'error'): self`

Enable session flashing with an optional custom message and key.

#### `withException(string $exceptionClass): self`

Set the exception class to be thrown.

#### `withExceptionCallback(callable $callback): self`

Set a callback for creating the exception.

#### `throwException(): void`

Throw the configured exception.

#### `throwExceptionIf($condition): void`

Throw the configured exception if the given condition is true.

#### `throwExceptionUnless($condition): void`

Throw the configured exception unless the given condition is true.

## Examples

### Basic Validation Exception

```php
ValidationExceptionBuilder::message('The email is invalid')
->redirectBack()
->throwException();
```

### Custom Redirect with Query Parameters

```php
ValidationExceptionBuilder::message('Invalid input')
->redirectTo('/users')
->withQueryParameters(['sort' => 'name', 'order' => 'asc'])
->throwException();
```

### Using Named Routes

```php
ValidationExceptionBuilder::message('Access denied')
->redirectRoute('dashboard', ['user' => $userId])
->throwException();
```

### Flashing Messages

```php
ValidationExceptionBuilder::message('Form submission failed')
->redirectBack()
->flash('Please correct the errors and try again.', 'warning')
->throwException();
```

### Conditional Exception Throwing

```php
$someCondition = true;

ValidationExceptionBuilder::message('Conditional error')
->redirectBack()
->throwExceptionIf($someCondition);
```

### Custom Exception Class

```php
class MyCustomException extends Exception {}

ValidationExceptionBuilder::message('Something went wrong')
->withException(MyCustomException::class)
->throwException();
```

### Using Exception Callback

```php
ValidationExceptionBuilder::message('Custom handling required')
->withExceptionCallback(function ($message, $url, $errorBag) {
// Custom logic here
return new MyCustomException($message);
})
->throwException();
```
File renamed without changes.
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ This package contains;
- [Eloquent Helpers](docs/EloquentHelpers.md)
- [Entities, Requests and Responses](docs/Entities_Requests_Responses.md)
- [Exceptions](docs/Exceptions.md)
- Handlers
- [ValidationHandler](docs/Handlers/ValidationHandler.md)
- [Helper Functions](docs/HelperFunctions.md)
- [first](docs/HelperFunctions.md#first-value)
- [uuid](docs/HelperFunctions.md#uuid)
Expand All @@ -38,6 +36,9 @@ This package contains;
- Transformers
- [FileLimiter](docs/Transformers/FileLimiter.md)
- [HumanReadableNumber](docs/Transformers/HumanReadableNumber.md)
- Validation
- [ValidationHandler](docs/Handlers/ValidationHandler.md)
- [ValidationExceptionBuilder](docs/Validation/ValidationExceptionBuilder.md))

## Installation

Expand Down
Loading

0 comments on commit 33ab8d9

Please sign in to comment.