Optional Ardent Requirement

36 Views Asked by At

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.

1

There are 1 best solutions below

1
Ben Claar On BEST ANSWER

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.