Uniform - more extensive validation questions

Hello (@mzur) and any others who may be able to help,

I’m looking to create a more thorough user registration using the uniform plugin that will ideally have some more advanced validation on fields, such as phone numbers, zipcodes, states, etc.

The uniform documentation doesn’t seem to have an extensive list of validation methods. The few I was able to extract while perusing it for answers were “required”, “email” and “num” and “in”.

Are these validation rules based entirely on he Kirby form field validation rules, meaning others like “match” would be available (i presume this would be my best bet for validating a telephone number)?

And if the Num works like this in Kirby:

if(v::num(1.0)) {
  echo 'Yay, valid!';
}

and like this in Uniform:

'attendees' => [
    'rules' => ['required', 'num'],
    'message' => 'Please enter a number of attendees',
],
'booth' => [
    'rules' => ['required', 'in' => [['6 sqm', '12 sqm', 'special']]],
    'message' => 'Please choose a booth size',
],

I presume a match would follow a similar conversion, sort of a blend of the two above:

if(v::match($value, '/[a-z0-9]+/')) {
  echo 'Yay, valid!';
}

to:

'phone' => [
    'rules' => ['required', 'match' => '/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$/'],
    'message' => 'Please enter a valid phone number',
],

regex example sourced from here because I’m not fluent in regex… just added some leading and trailing \ to the answer provided there, because otherwise I was getting an error otherwise (and that’s what kirby’s match had).

As I was typing this up, I ended up just testing it myself. And it does look like it work so far, testing various formatting of phone numbers I would encounter in a US only registration page. But I’m going to post this anyway in case it helps anyone else, and also because I’d like to encourage @mzur to update the documentation with a more extensive list/conversion of validation methods when he has time.

The Uniform validation rules use Kirby’s invalid helper internally. The invalid helper in turn uses the validators of the Kirby Toolkit. You can use any of these to validate form fields. I’ll update the Uniform documentation to make that more clear.

You can also implement custom validators to do things like these:

'phone' => [
    'rules' => ['required', 'phone'],
    'message' => 'Please enter a valid phone number',
],

Side note: Show your support for my PR that implements improved error messages for the invalid helper :wink:

Edit: The docs now include links to the available validators and to the guide on how to implement custom validators. I did mention the invalid helper there before, though :stuck_out_tongue_winking_eye:

1 Like

Thank you for the incredibly prompt, and very helpful, response. I’ve given your PR a thumbs up emoji. I like the second item you listed with multiple error messages for each validation check. It’s not necessarily a requirement in my case, but if it was available I would certainly be using it opposed to my single “Please enter a valid phone number” error.