1

I'm doing a dashboard frontend for a Laravel back-end, and there are a lot of ajax requests from many sources, my end goal is to have a file like api.js that encapsulates all my api logic, I wanted to be able to reuse Laravel validation rules to validate all my api requests.

example form request:

<?php

namespace App\Http\Requests\DoctorProfile;

use App\Http\Requests\ApiRequest;

class GetAppointments extends ApiRequest
{
...
    public function rules()
    {
        return [
            'doctor_id' => 'required|integer|min:0',
            'date' => 'date_format:Y-m-d',
        ];
    }
...
}

I searched and found these two libraries:

  1. Laravel Javascript Validation
  2. Jquery Validation

these two are good but the problem is they both focus on Form elements but there are many requests that still need validation but the values don't exist in a single form!

I thought about it and a possible hack is to have a hidden Form and i can add values to it via js then use Laravel Javascript Validation to send the request.

I don't have that much front-end experience so i wanted to ask here and make sure im not doing anything terribly wrong bfore resorting to such hacks:D

4
  • 1
    What's the need for this? If you do an Ajax call to a controller in the backend, then you can validate there? The frontend validation as you call it would be something like required and type="number" on a field or something, or we're misunderstanding each other. Commented May 31, 2023 at 6:34
  • @geertjanknapen there could be multiple scenarios, the main problem is the dependency to Form element,e.g. lets say i have a profile and i want to be able to update each field separately so if user wishes to update a single input i can just update that instead of submitting all fields, or i want to be able to fetch some on mouse click based on mouse cursor position on a graph. this data obviously don't exist in a form but i can access them and send a request on click. i know its not that common but i'm looking for a solution that works in all situations instead of just in forms. Commented May 31, 2023 at 6:55
  • The updating case you describe above could easily be done in a form with the sometimes Laravel validation. Fetching data on mouse-hover or click has nothing to do with validation, if you make sure you return the data in a certain way to the graph, why would you need the 'frontend validation' for anything? Commented May 31, 2023 at 7:48
  • You are, to a certain degree, in charge of the API calls. You decide how you want to validate it for sure but the calls are initiated on the frontend but will need to hit a controller to return any sort of data. That's where you validate any inputs or whatever before doing anything else with the API call. Commented May 31, 2023 at 7:50

1 Answer 1

3

You can use Laravel Precognition. This will allow you to use your backend validation rules in the frontend without having to rewrite any of the rules in the frontend.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.