Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mhery's avatar
Level 1

validate alphanumeric parameter on route of api

I created a resource in my API to check if an given nickname exists on database, like this:

Route::get('checknickname/{nickname}', 'AuthController@check_nickname');

But I want to vallidate if the parameter nickname is alpha_dash, just like we use on an POST method, for example. How can I do this?

I found out that on check_nickname function at AuthController I can't check like this:

$request->validate([
        'nickname' => 'required|alpha_dash|string',
    ]);
0 likes
2 replies
rameezisrar's avatar
Level 18

@mhery createa a custom Validation rule i.e AlphaDash and then use it on the validation

Validator::make($data, [
            'AlphaDash' => [
                'required',
                new AlphaDash
            ]
        ]);

Snapey's avatar

If you want to validate the route parameter then you need to create a manual validator and pass in the nickname as part of an array to the validator

https://laravel.com/docs/5.8/validation#manually-creating-validators

But ideally, you should pass the nickname the first time as post data or query string - not as part of the route.

eg GET /checknickname?nickname='snapey'

Please or to participate in this conversation.