rylacdang / formcast
Cast your Laravel FormRequest rules to typed Zod schemas, TypeScript types, and Laravel-identical error messages.
Requires
- php: ^8.3
- illuminate/console: ^12.0 || ^13.0
- illuminate/routing: ^12.0 || ^13.0
- illuminate/support: ^12.0 || ^13.0
- illuminate/validation: ^12.0 || ^13.0
- spatie/laravel-package-tools: ^1.93
- symfony/finder: ^7.0 || ^8.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.20
- orchestra/testbench: ^10.0 || ^11.0
- pestphp/pest: ^3.0 || ^4.0
- pestphp/pest-plugin-arch: ^3.0 || ^4.0
- pestphp/pest-plugin-laravel: ^3.0 || ^4.0
- spatie/pest-plugin-snapshots: ^2.2
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Cast your Laravel FormRequest rules to the client.
Formcast reads your Laravel FormRequest classes and generates typed Zod schemas, TypeScript types, and Laravel-identical error messages for your Inertia (Vue / React) and standalone (Expo / Axios) apps. Client-safe rules validate instantly in the browser (0 ms), while server-only rules (unique, exists, closures) are routed through Inertia's built-in Precognition only after client checks pass.
Caution
Client-side validation is for user experience only. Laravel validation on the server is never optional.
Installation
composer require rylacdang/formcast npm install -D @rylacdang/formcast zod php artisan formcast:install
Add the Vite plugin to vite.config.ts:
import { formcast } from '@rylacdang/formcast/vite'; export default defineConfig({ plugins: [ laravel({ input: ['resources/js/app.ts'], refresh: true }), vue(), // or react() wayfinder({ formVariants: true }), formcast(), ], });
60-Second Example
1. Write your normal Laravel FormRequest
class StoreOrderRequest extends FormRequest { public function rules(): array { return [ 'customer_email' => ['required', 'email', 'max:255', 'unique:orders,customer_email'], 'payment_method' => ['required', Rule::enum(PaymentMethod::class)], 'gcash_reference' => ['nullable', 'required_if:payment_method,gcash', 'string', 'size:13'], 'items' => ['required', 'array', 'min:1'], 'items.*.product_id' => ['required', 'integer', 'exists:products,id'], 'items.*.quantity' => ['required', 'integer', 'min:1', 'max:99'], 'notes' => ['nullable', 'string', 'max:500'], ]; } public function attributes(): array { return ['gcash_reference' => 'GCash reference number']; } }
2. Generate typed casts (or let the Vite plugin do it automatically)
php artisan formcast:generate
3. Use in Vue or React with Inertia + Wayfinder
<script setup lang="ts"> import { useFormcast } from '@rylacdang/formcast/vue'; import StoreOrderRequest from '@/formcast/App/Http/Requests/StoreOrderRequest'; import { store } from '@/actions/App/Http/Controllers/OrderController'; const form = useFormcast(StoreOrderRequest, { customer_email: '', payment_method: 'cash', gcash_reference: null, items: [{ product_id: 1, quantity: 1 }], notes: null, }); </script> <template> <form @submit.prevent="form.submit(store())"> <input v-model="form.customer_email" @blur="form.touch('customer_email')" /> <p v-if="form.invalid('customer_email')">{{ form.errors.customer_email }}</p> <span v-if="form.validating">Checking…</span> <button :disabled="form.processing || form.hasErrors">Place order</button> </form> </template>
Why Formcast?
| Feature | Other Zod generators | Precognition only | Formcast |
|---|---|---|---|
FormRequest → Zod + TS types |
✅ | ❌ | ✅ |
Laravel-identical messages (messages(), attributes(), lang/) |
❌ / partial | ✅ | ✅ |
| Hybrid validation (Zod first, Precognition only for server rules) | ❌ | ❌ (every check hits server) | ✅ |
First-class Inertia useFormcast() (Vue & React) |
❌ | ✅ | ✅ |
| Vite plugin with debounced auto-regeneration | ❌ | N/A | ✅ |
Controller/route mapping (usedBy, Wayfinder-ready) |
❌ | N/A | ✅ |
Laravel semantics (TrimStrings, "" = missing, "5" int) |
partial | ✅ | ✅ |
| Automated Laravel-vs-Zod parity suite | ❌ | N/A | ✅ |
| Framework-agnostic core (Expo / React Native / Axios) | ❌ | ❌ | ✅ |
| Ships Laravel Boost guidelines & AI skill | ❌ | ❌ | ✅ |
Documentation
- Installation & Configuration
- Generating Casts & CI Checks
- Vue + Inertia Guide
- React + Inertia Guide
- Standalone Usage (Expo / React Native / Axios)
- Hybrid Validation & Precognition
- Messages & Locales
- Laravel Wayfinder Integration
- Rule Support Matrix
- Custom Client-Side Rules
- AI Skill & Laravel Boost
- Troubleshooting
License
Formcast is open-sourced software licensed under the MIT license.