0

I'm using laravel and i have a request form with rule like this:

    'postal_code1' => 'required',
    'postal_code2' => 'required_with:postal_code1|numeric',

and some html

<input type="text" name="postal_code1" />
<input type="text" name="postal_code2" />

The trouble i met that when i don't fill the postal_code1, I receive the error

Postal_code2 must be a number

How do numeric rule run only if the input is filled?

I tried the sometimes rule:

'postal_code2' =>'sometimes|required_with:postal_code1|numeric'

But not working

6
  • here you add postal_code1 required then empty is not allowd for postal_code1 Commented Jul 5, 2018 at 6:50
  • 'postal_code1' => 'required', 'postal_code2' => 'bail|required_with:postal_code1|numeric', Commented Jul 5, 2018 at 6:51
  • @JinalSomaiya: because i want both of input required but error message is display once time only. Commented Jul 5, 2018 at 6:56
  • @SachinAghera not working :(( Commented Jul 5, 2018 at 6:57
  • if you need both field required then why you not use simple required? Commented Jul 5, 2018 at 7:11

1 Answer 1

2

I have found the solution, it's very easy. Change the rule as below:

'postal_code2' => 'nullable|required_with:postal_code1|numeric'
1
  • That's the solution but for example if both 'postal_code2' and 'postal_code1' are nullable validation not passes.
    – JEX
    Commented Oct 29, 2020 at 18:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.