Skip to content

Instantly share code, notes, and snippets.

@Juhlinus
Last active July 7, 2020 09:40
Show Gist options
  • Save Juhlinus/73176e11f5a65e61422831e9d71e0002 to your computer and use it in GitHub Desktop.
Save Juhlinus/73176e11f5a65e61422831e9d71e0002 to your computer and use it in GitHub Desktop.
A more elegant way to handle validation in Inertia.js
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreOrganization;
use App\Organization;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Inertia\Inertia;
class OrganizationsController extends Controller
{
public function index()
{
return Inertia::render('Organizations/Index', [
'filters' => Request::all('search', 'trashed'),
'organizations' => Auth::user()->account->organizations()
->orderBy('name')
->filter(Request::only('search', 'trashed'))
->paginate()
->only('id', 'name', 'phone', 'city', 'deleted_at'),
]);
}
public function create()
{
return Inertia::render('Organizations/Create');
}
public function store(StoreOrganization $request)
{
Auth::user()->account->organizations()->create(
$request->validated()
);
return Redirect::route('organizations')->with('success', 'Organization created.');
}
public function edit(Organization $organization)
{
return Inertia::render('Organizations/Edit', [
'organization' => [
'id' => $organization->id,
'name' => $organization->name,
'email' => $organization->email,
'phone' => $organization->phone,
'address' => $organization->address,
'city' => $organization->city,
'region' => $organization->region,
'country' => $organization->country,
'postal_code' => $organization->postal_code,
'deleted_at' => $organization->deleted_at,
'contacts' => $organization->contacts()->orderByName()->get()->map->only('id', 'name', 'city', 'phone'),
],
]);
}
public function update(Organization $organization)
{
$organization->update(
Request::validate([
'name' => ['required', 'max:100'],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
])
);
return Redirect::back()->with('success', 'Organization updated.');
}
public function destroy(Organization $organization)
{
$organization->delete();
return Redirect::back()->with('success', 'Organization deleted.');
}
public function restore(Organization $organization)
{
$organization->restore();
return Redirect::back()->with('success', 'Organization restored.');
}
}
<?php
namespace App\Http\Requests;
use App\Exceptions\ValidatedException;
use Illuminate\Foundation\Http\FormRequest;
class StoreOrganization extends FormRequest
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => ['required', 'max:100'],
'email' => ['nullable', 'max:50', 'email'],
'phone' => ['nullable', 'max:50'],
'address' => ['nullable', 'max:150'],
'city' => ['nullable', 'max:50'],
'region' => ['nullable', 'max:50'],
'country' => ['nullable', 'max:2'],
'postal_code' => ['nullable', 'max:25'],
];
}
/**
* Handle a passed validation attempt.
*
* @return void
*/
public function passedValidation()
{
if ($this->request->get('validate', false)) {
throw new ValidatedException();
}
}
}
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class ValidatedException extends Exception
{
/**
* Render the exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function render(Request $request)
{
return Redirect::back();
}
}
@Juhlinus
Copy link
Author

Juhlinus commented Jul 2, 2020

After some docs-diving I realized that we don't need to modify anything in the handler.

The render method is called on each exception by the handler, so all we have to do is define it and add the redirect.

I've updated the files accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment