1

I have a Printer model which has a page_count field..

the user will be able to input the current page_count...

the new page_count must be greater than the existing data in the database... How can I do that?

3
  • 4
    Get the data from db and use in Validator 'page_count' => 'required|min:'.$myValue) ... . see laravel.com/docs/5.2/validation Commented Aug 12, 2016 at 13:38
  • thanks.. I'll temporarily use this. Commented Aug 12, 2016 at 14:38
  • I just changed customized the error message. Commented Aug 12, 2016 at 14:39

2 Answers 2

2

I had the same issue solved like this, though someone already gave the solution in the comments section.

/**
     * @param  array  $data
     * validates and Stores the application data
     *
     */
    public function sendMoney(Request $request)
    {
      //get the value to be validated against
        $balance = Auth::user()->balance;
        $validator = Validator::make($request->all(), [
            'send_to_address' => 'required',
            'amount_to_send' => 'required|max:'.$balance.'|min:0.01|numeric',
        ]);
     //some logic goes here
    }

Depending on your use case you could modify...

Happy Coding

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

Comments

0

Assuming you have Printer model which contains the page_count column.
You can define a custom validation rule in your AppServiceProvider's boot() method.

public function boot()
    {
        //your other code

        Validator::extend('page_count', function($attribute, $value, $parameters, $validator) {
                    $page_count = Printer::find(1)->first()->value('page_count'); //replace this with your method of getting page count.
                    //If it depends on any extra parameter you can pass it as a parameter in the validation rule and extract it here using $parameter variable.
                    return $value >= $page_count;
                });

        //your other code
    }

Then, you can use it in your validation rule like below

'page_count' => 'required|page_count'

Reference: Laravel Custom Validation

1 Comment

If you get any issue, let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.