0

I need to add required rule if one field is available. Also need to check if it is an integer and 10 digit. So I added the rule like below.

'id_number' => 'sometimes|required|digits:10|integer'

Validations works only when the field is available. But here required rule is not working. It directly shows integer error even if the field is empty.

I use Laravel 5.1

Joel James
  • 1,315
  • 1
  • 20
  • 37

1 Answers1

2

Finally I figured it!

You need to change the order of required rule to last. It works when I add rule like this,

'id_number' => 'sometimes|digits:10|integer|required'
Joel James
  • 1,315
  • 1
  • 20
  • 37
  • Did you figure out why this is happening? I have the same problem, but the official documentation says that it should be `sometimes|required`? – Dmitry Torba Aug 24 '16 at 20:25
  • @DmitryTorba In Laravel validation, normally all rules will be validated. My problem was, I was trying to add these error messages to a string instead of array. So all my error messages were being overwritten. – Joel James Aug 25 '16 at 05:44