Home > Blockchain >  Validate field from an api post request
Validate field from an api post request

Time:03-22

So what I want to do is send an axios request with a field and data to a Laravel controller called VerifyController.php

The function I started to write is:

    public function checkUsername(Request $request) {
        Validator::make($request, [
            'name' => ['required', 'string', 'max:255', 'unique:users'],
        ])->validate();

        return true;
    }

All I want to do is send an axios request to /api/verifyusername with the field name and its corresponding data and see if it comes back as available or taken

What does the function need to actually look like? Ive NEVER done this before as I am more of a frontend only guy

CodePudding user response:

public function checkUsername(Request $request) {
    $request->validate([
        'name' => ['required', 'string', 'max:255', 'unique:users'],
    ]);

    return true;
}

Or

public function checkUsername(Request $request) {
    Validator::make($request->all(), [
        'name' => ['required', 'string', 'max:255', 'unique:users'],
    ])->validate();

    return true;
}
  • Related