Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

A rule to validate delimited data

Original – by Freek Van der Herten – 3 minute read

laravel-validation-rules is a package that contains handy custom validation rules that are being used in various client projects we made at Spatie. Last week I added a new validate rule called Delimited to the package.

In this blog post, I'd like to explain why we added it and how it can be used.

Do you really need multi-value inputs?

Last week Caleb "String King" Porzio tweeted this:

I think he's right. Why bother to go through all the hassle creating a multi-input thing, when accepting a delimited string is good enough.

In a project I'm working on, there are some fields that accept multiple email-addresses and other fields that accept multiple phone numbers. I decided to let them be input as comma separated strings. The first thing that was needed is validation.

Introducing the Delimited rule

To validate both comma separated emails and comma separated phone number, I created a new validation rule named Delimited that can validate all kinds of validated data. It's meant to be used in a form request. Its constructor accepts a validation rule that will be used to validate each item in the delimited string.

Here's an example of its usage:

// in a `FormRequest`

public function rules()
{
    return [
        'emails' => [new Delimited('email')],
    ];
}

Here's some example input that passes this rule:

  • 'sebastian@example.com, alex@example.com'
  • ''
  • 'sebastian@example.com'
  • 'sebastian@example.com, alex@example.com, brent@example.com'
  • ' sebastian@example.com , alex@example.com , brent@example.com '

This input will not pass:

  • '@example.com'
  • 'nocomma@example.com nocommatoo@example.com'
  • 'valid@example.com, invalid@'

If you need to validate a comma-separated string with phone numbers, you can simple pass the phone rule to Delimited.

// in a `FormRequest`

public function rules()
{
    return [
        'phone_numbers' => [new Delimited('phone')],
    ];
}

How to process the validated delimited data is up to you. You could save the validated delimited string to the database. Or you could explode the value and save it as an array in the db. The choice is up to you.

In closing

The Delimited Rule has many other options. You can set a minimum and maximum of items, use another delimiter, accept duplications, disable trimming, ... Take a look at the readme of the laravel-validation package to learn more about these options.

Share Post LinkedIn

I write about Laravel, PHP, AI and building better software.

Every two weeks, I share practical tips, tutorials, and behind-the-scenes insights from maintaining 300+ open source packages. Join thousands of developers who read along.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share?

The community section is a place where developers share links to articles, tutorials and videos. Submit a link and help fellow developers discover great content. As a thank you, you'll receive a coupon for a discount on Spatie products.