skip to Main Content

In some page I have an input that I need to match against some query result and make sure that the input from the user is not greater than the result of the DB query (I will do SUM of some column).

For such validation rule, what should I use in the Form Request? Should I create a new rule and call it something like ValidCustomInputRule and do the DB query there, or there is a better option for that inside the Form Request?

2

Answers


  1. You can create a custom validation rule in the Form Request and perform the DB query in the rule itself.

    You can create a custom validation rule and use it in rules

    public function rules()
    {
        return [
            'input_value' => ['required', new ValidCustomInputRule]
        ];
    }
    

    You can use php artisan make:rule ValidCustomInputRule to make it

    use IlluminateContractsValidationRule;
    use IlluminateSupportFacadesDB;
    
        class ValidCustomInputRule implements Rule
        {
            public function passes($attribute, $value)
            {
                $sum = DB::table('table_name')->sum('column_name');
        
                return $value <= $sum;
            }
        
            public function message()
            {
                return 'The input value is greater than the sum of the column in the database.';
            }
        }
    
    Login or Signup to reply.
  2. I think you can use a Closure if you want to keep it all inside your form request.

    class YourFormRequest extends FormRequest
    {
        public function rules(): array
        {
            return [
                'input_name' => [
                    'required',
                    'numeric',
                    function (string $attribute, mixed $value, Closure $fail) {
                        $max = DB::table('table-table')->sum('column');
    
                        if ($value > $max) {
                            $fail("$attribute cannot be more than $max.");
                        }
                    },
                ],
            ];
        }
    }
    

    If that doesn’t work, I think you can add it to the after hook.

    use IlluminateValidationValidator;
    
    class YourFormRequest extends FormRequest
    {
        public function rules(): array
        {
            return [
                'input_name' => [
                    'required',
                    'numeric',
                ],
            ];
        }
    
        public function withValidator(Validator $validator): void
        {
            $validator->after(function (Validator $validator) {
                $value = $validator->getData()['input_name'];
                $max = DB::table('table-table')->sum('column');
    
                if ($value > $max) {
                    $validator->errors()->add('input_name', "input_name cannot be more than $max");
                }
            });
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search