0

I have a selectbox(api_balance => have two item : manual, auto) and a input(balance) in view. I want to check if the value of api_balance is equal to manual, then it is necessary to enter the value for the input balance, otherwise it does not have a value, it does not show an error

protected $rules = [
    'api_balance' => 'required',
    'balance' => $this->api_balance === 'manual' ? 'required' : 'r',
];

I wrote the following code but it gives an error. What is the solution?

1 Answer 1

3

You can use required_if

'credit_card_number' => 'required_if:api_balance,manual'

Even you can do the following

'credit_card_number' => Rule::requiredIf(function (){
        return ($this->api_balance == 'manual');
    }),

Ref:https://laravel.com/docs/8.x/validation#rule-required-if

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

1 Comment

No problem :) Keep it up with the great answers!

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.