protonemedia / laravel-splade

💫 The magic of Inertia.js with the simplicity of Blade 💫 - Splade provides a super easy way to build Single Page Applications (SPA) using standard Laravel Blade templates, and sparkle it to make it interactive. All without ever leaving Blade.

Home Page:https://splade.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reuse laravel form request validation rules to client side form validation.

dhsont opened this issue · comments

commented

Thank you for providing this excellent package.

I have been actively exploring Splade over the past week and I must say that I am quite impressed with it. The features it offers are quite powerful and promising.

One feature i am missing while using Splade, and that is the ability to reuse form request validation rules for client-side form validation.
A similar approach is implemented in the Laravel JsValidation library (https://github.com/proengsoft/laravel-jsvalidation).
where you can just pass formrequest or laravel validator instance to blade file and it will handle client side validation.

{!! JsValidator::formRequest('App\Http\Requests\MyFormRequest') !!}

or Use Validator instance

$validator = Validator::make(['name_field'=>'required']);
$jsValidator = JsValidator::validator($validator)

I believe that implementing such a feature in Splade would have several advantages. Firstly, it would promote the concept of DRY (Don't Repeat Yourself) coding, making the codebase cleaner and more maintainable. Additionally, this approach would enable instant display of validation errors as users fill out the form, even before submitting it. This real-time feedback can greatly enhance the user experience, especially for larger forms where navigating back and forth between the form and error messages can be cumbersome.

Incorporating this feature into Splade would not only simplify the development process but also contribute to creating more user-friendly and efficient web applications. I'm excited about the potential benefits this enhancement could bring and look forward to seeing how Splade continues to evolve.

If you use the Splade Form Builder you can set the rules on the fields.

I think you can do something like this:

class YourFormRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => 'required',
            'email' => 'required|email|rfc',
        ];
    }
}
$rules = (new YourFormRequest)->rules();
$form = SpladeForm::make()->fields([
    Input::make('name')->rules($rules['name']),
    Email::make('email')->rules($rules['email']),
]);

Edit: But as you mentioned, I don't think the validation is done client side.