2

I am validating number field below code is working fine. It is checking the characters should not be more then 2 or less, which is fine for me.

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'id'    => 'required|min:2|max:2',
        'title' => 'required'
    ];
}

But when I add more validation rule like (numeric), min and max validaiton rules get changed, now it is being checked by the numeric number should not be greater than 2... WHY? IS IT BUG?

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'id'    => 'required|min:2|max:2|numeric',
        'title' => 'required'
    ];
}
0

1 Answer 1

3

the min and max rule depend on the type of the field. If it is a string it will check the length. If it is a number it will check the value.

So if you want to have numbers only with two chars set use

'id'    => 'required|min:10|max:99|numeric',

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.

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

2 Comments

What if they try to enter the id like 01, 02 or 03?
I think they will be casted to int (so its 1, 2, 3) and then they will check the min and max.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.