2

I'm using Ardent with Laravel. We are working on a site that only allows US customers but we're extending it to Canadian customers as well. One of our requirements is that the zip code be 5-9 characters long, all numbers (we strip out the dash and other punctuation to validate).

We want to have validation for postal codes as well, but only for postal_codes to be required if zip_code is not offered (and vice versa). Is this possible? Theoretically we could use just one field but we'd have to have a more complex regex.

StackOverflowed
  • 5,854
  • 9
  • 55
  • 119

1 Answers1

2

In my site, I handle this with a country field:

public static $rules = array(
    'postal_code' => 'required_if:country,CAN',
    'zip_code' => 'required_if:country,USA',
)

You could also use required_without.

Ben Claar
  • 3,285
  • 18
  • 33