1

I want to validate field if present and will fail if another field not present How to do it ? I have create custom validation to get record based id on storage

I have field product_id and amount and the amount field validate between max and min based on product_id, if i present the amount = 100000 and not fill the product_id field the validating for amount field is passed and display error because i am not fill the product_id i want check before the custom validation execute will check if product_id empty or not..

I have read all validation method on laravel docs still didnt find the solution. How to do it ?

1 Answer 1

1

Finally i found a solution...

on Form Request class i just add sometimes rule on validate instance so when the product_id field not present on amount field will not validate and when present it will validate using validation rules like this:

class StoreRequest extend FormRequest
{
    protected function getValidatorInstance()
    {
        $validator = parent::getValidatorInstance();

        $validator->sometimes('amount', 'required|numeric|max_amount:product_id', function ($input) {
           return Arr::has($input, 'product_id');
        });

        return $validator;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.