valitron icon indicating copy to clipboard operation
valitron copied to clipboard

Add alphaSpace and alphaNumSpace rules

Open infostreams opened this issue 2 years ago • 3 comments

infostreams avatar Aug 10 '21 08:08 infostreams

I have a form where the user can enter his name (e.g. "John Doe"). None of the rules in this otherwise excellent library allow me to validate his input! I have another field where they can put in their address (e.g. "Example Street 1"). There is no rule to validate that either! I think that's an oversight.

So, I've added two new rules to address that: alphaSpace and alphaNumSpace. The first allows alphabetic characters AND whitespace. That allows people to validate input such as "John Doe" without writing their own rules. The second allows alpha-numeric characters and whitespace, so you can validate "Example Street 1" without any additional work.

I've added tests, translations, and updated the README. Tests were succeeding on my machine, but seem to be failing for the older PHP versions here. Will have a look and update, it probably has to do with me updating a call to assertRegExp call that PHPUnit was complaining about.

infostreams avatar Aug 10 '21 08:08 infostreams

Can someone please give permissions to re-run the workflow?

infostreams avatar Sep 03 '21 09:09 infostreams

I think there's another missing default validation: one that checks if the string only contains letters.

There's a standard check that checks for a-z, but if you want to validate (for example) a "name" field, that's insufficient. There are many names outside of the Anglosphere that have accented characters, such as "René" (French), "Müller" (German), or "Goçak" (Turkish). I cannot predict the names of my customers, but I do need to deal with them - and I expect many other people do as well. The only options they have now, if they want to use this library, is to use the "alpha" rule, or to the "alphaSpace" rule, or to write their own validator function. I don't think it should be necessary for people to write a validation function if they want to use this library to do something as common as validating a name.

Luckily, there's a feature in PHP that allows us to very easily solve the above problem, and that's to use the pL regexp (documentation). It matches all letters, including all weird and accented ones. The matching functions would look something like this:

function letters($field, $value, $params) {
	return preg_match('/^([\pL])+$/ui', $value);
}

function lettersSpace($field, $value, $params) {
	return preg_match('/^([\pL\s])+$/ui', $value);
}

function lettersNumSpace($field, $value, $params) {
	return preg_match('/^([\pL\s0-9])+$/ui', $value);
}

If I had time and energy I would have written a pull request, but it doesn't seem the best use of my time. Thanks for the library!

infostreams avatar Sep 07 '21 12:09 infostreams