Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alphaSpace and alphaNumSpace rules #347

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

infostreams
Copy link

No description provided.

@infostreams
Copy link
Author

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
Copy link
Author

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

@infostreams
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant