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

glasstream2000's avatar

Min max

Trying to add a minimum and a maximum numeric size to my validation. I only want 4 numbers in each of the two fields, year_born and last_four. They are to only have a numeric 4 digit number. I have tried min:4|max:4 and size:4 and it throws an error on every submit like the below. If the user enter any 4 digit number I want it to go into the database, but 3 or fewer or 5 or more numbers throw an error.

The year born may not be greater than 4. I have even changed max:4 to max:5 and the error says The year born may not be greater than 5

In my controller, I have tried different things and just can't seem to get the min and max to work, saw in another post about using size, but the same result:

Any idea why or what I'm doing wrong?

public function store(Request $request)
    {
        // $this->validate($request, [
        //     'first_name' => 'required',
        //     'last_name' => 'required',
        //     'year_born' => 'required',
        //     'last_four' => 'required',
        //     'location' => '',
        //     'email' => 'email'
        // ]);
        $request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|min:4|max:4',
            'last_four' => 'required|numeric|min:4|max:4',
            'location' => 'required',
            'email' => 'required|email',
        // ], [
        //     'first_name.required' => 'Name is required',
        //     'first_name.min' => 'Name must be at least 2 characters.',
        //     'first_name.max' => 'Name should not be greater than 50 characters.',
        //     'year_born.size' => 'Year born and Last four should only be 4 numbers',
            
        ]);
        
        // create record
        $listing = new Listing;
        $listing->first_name = $request->input('first_name');
        $listing->last_name = $request->input('last_name');
        $listing->year_born = $request->input('year_born');
        $listing->last_four = $request->input('last_four');
        $listing->location = $request->input('location');
        $listing->email = $request->input('email');
        $listing->module = $request->input('module');
        $listing->user_id = auth()->user()->id;
        
        $listing->save();
        
        return redirect('/dashboard')->with('success', 'Module Completed');
        
    }
0 likes
4 replies
Tray2's avatar

I did this when I wanted to validate a year

'released' => ['required', 'integer', 'between:1800,' . Carbon::now()->addYear(1)->year]

So nothing older than 1800 and nothing never than 2020 will be stored.

glasstream2000's avatar

Tried the between like below, from reading the docs: between:min,max The field under validation must have a size between the given min and max. Strings, numerics, and files are evaluated in the same fashion as the size rule. Using a four digit number it throws the error: The year born must be between 3 and 5. I got to be missing something here.

$request->validate([
            'first_name' => 'required|min:2|max:50',
            'last_name' => 'required|min:2|max:50',
            'year_born' => 'required|numeric|between:3,5',
            'last_four' => 'required|numeric|min:4',
            'location' => 'required',
            'email' => 'required|email',
        // ], [
        //     'first_name.required' => 'Name is required',
        //     'first_name.min' => 'Name must be at least 2 characters.',
        //     'first_name.max' => 'Name should not be greater than 50 characters.',
        //     'year_born.size' => 'Year born and Last four should only be 4 numbers',
            
        ]);
glasstream2000's avatar

That might work for my year_born field but for the last 4 it will be different. I just want to validate that the field only has 4 numbers of any combination.

glasstream2000's avatar
glasstream2000
OP
Best Answer
Level 4

Ok looks like using digits:4 achieves what I need, in case someone else is looking for the same!

Please or to participate in this conversation.