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.

Stay up to date with all things Laravel, PHP, and JavaScript.

Follow me on Twitter. I regularly tweet out programming tips, and what I myself have learned in ongoing projects.

Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

Webmentions

Runar Jørgensen liked on 5th December 2020
Catalin Banu liked on 5th December 2020
Jeremy Granadillo liked on 5th December 2020
Paulund liked on 5th December 2020
Justin Dantzer liked on 5th December 2020
Gautam Kumar liked on 5th December 2020
PippinSmith replied on 4th December 2020
I literally installed it hours before this tweet!
Vishnu R Nair liked on 4th December 2020
Thomas Sander retweeted on 4th December 2020
Ray Bonander liked on 4th December 2020
Manojkiran liked on 4th December 2020
Jack Watling liked on 4th December 2020
Zekaryas liked on 4th December 2020
Victor liked on 4th December 2020
Kalle Palokankare liked on 4th December 2020
Niels liked on 4th December 2020
Romain Léger liked on 4th December 2020
Leonel Elimpe liked on 4th December 2020
Jonathan Williamson liked on 4th December 2020
AlaaMU liked on 4th December 2020
Julien Bourdeau liked on 4th December 2020
Songhua Hu liked on 4th December 2020
Navi liked on 4th December 2020
Thelemaque Dukens liked on 4th December 2020
Thelemaque Dukens retweeted on 4th December 2020
Samuel Breuillet retweeted on 3rd December 2020
José Cage liked on 3rd December 2020
Michael Aguiar liked on 3rd December 2020
Samuel Breuillet liked on 3rd December 2020
Salman Zafar liked on 3rd December 2020
Daniel Gehn liked on 3rd December 2020
Mithicher Baro retweeted on 3rd December 2020
Hassan Haseed liked on 3rd December 2020
Vaggelis Yfantis liked on 3rd December 2020
Алексаднр liked on 3rd December 2020
Saumya Sthapit liked on 3rd December 2020
Laravel Idea liked on 3rd December 2020
Tauseef shah liked on 3rd December 2020
Patrick Silburn liked on 3rd December 2020
Daniel Lucas liked on 3rd December 2020
Asif Mulla liked on 3rd December 2020
Wyatt liked on 3rd December 2020