phplaravellaravel-validation

How to create custom validation rule that only accepts specific numbers in Laravel?


How to set custom validation rule that accept only 1.00, 1.25, 1.50, 1.75, 2.00, 2.25, 2.50, 2.75 and 3.00 in input text? The grades and grades1 are my inputs.

1.35 should be invalid (incase you're thinking the solution of divisible by 5)
1.26 should be invalid

FormRequest

public function rules()
{
    return [
        'user_id' => 'required',
        'app_id' => 'nullable',
        'subjects.*' => 'required|string',
        'subjects1.*' => 'required|string',
        'school_year' => 'required',
        'grades.*' => ['required','numeric','lt:2.50'],
        'grades1.*' => 'required|lt:2.50',
        'units.*' => 'required|integer|min:1',
        'units1.*' => 'required|integer|min:1',
        'term' => 'required',
        'term1' => 'required',
        'total.*' => 'nullable',
        'total1.*' => 'nullable',
        'gwa_1st' => 'required|lte:1.75',
        'gwa_2nd' => 'required|lte:1.75',
        'year_level' => 'required|string',
        'image' => 'required|mimes:jpeg,png,jpg',
        'award_applied' => 'required|string',
        'course_id' => 'required|string'
    ];
}

Controller

public function store(AchieversAwardRequest $request)
{

    $data = $request->validated();
    $award = new StudentApplicants();

    $award->user_id = $data['user_id'];
    $award->school_year = $data['school_year'];
    $award->gwa_1st = $data['gwa_1st'];
    $award->gwa_2nd = $data['gwa_2nd'];
    $award->year_level = $data['year_level'];

    if ($request->hasfile('image')) {
        $file = $request->file('image');
        $filename = time() . '.' . $file->getClientOriginalExtension();
        $file->move('uploads/', $filename);
        $award->image = $filename;
    }

    $award->award_applied = $data['award_applied'];
    $award->course_id = $data['course_id'];
    $award->save();
    $lastid = $award->id;
  }

Solution

  • One possible solution would be to use the in validation rule, something like the following:

    $acceptable = [1.00, 1.25, 1.5, 2.75, 3.00];
    
    $validator = Validator::make(['numbers_field' => 2.75], [
        'numbers_field' => [Rule::in($acceptable)]
    ]);
    

    Update

    Add the rule to your AchieversAwardRequest custom FormRequest rules() method along with all your other rules:

    public function rules()
    {
        return [
            'user_id' => 'required',
            'app_id' => 'nullable',
            'subjects.*' => 'required|string',
            'subjects1.*' => 'required|string',
            'school_year' => 'required',
            'grades.*' => ['required','numeric','lt:2.50'],
            'grades1.*' => 'required|lt:2.50',
            'units.*' => 'required|integer|min:1',
            'units1.*' => 'required|integer|min:1',
            'term' => 'required',
            'term1' => 'required',
            'total.*' => 'nullable',
            'total1.*' => 'nullable',
            'gwa_1st' => 'required|lte:1.75',
            'gwa_2nd' => 'required|lte:1.75',
            'year_level' => 'required|string',
            'image' => 'required|mimes:jpeg,png,jpg',
            'award_applied' => 'required|string',
            'course_id' => 'required|string'
    
            'field_name' => [Rule::in([1.00, 1.25, 1.5, 2.75, 3.00])]
        ];
    }
    

    Obviously, replace field_name with the name of your input field. You will also need to include use Illuminate\Validation\Rule; at the top of your AchieversAwardRequest.