Skip to content

Instantly share code, notes, and snippets.

@richthegeek
Created March 5, 2012 04:20
Show Gist options
  • Save richthegeek/1976604 to your computer and use it in GitHub Desktop.
Save richthegeek/1976604 to your computer and use it in GitHub Desktop.
Laravel: patch for adding generic validtor.
diff --git a/laravel/validator.php b/laravel/validator.php
index 0c84e8e..1a09bff 100644
--- a/laravel/validator.php
+++ b/laravel/validator.php
@@ -584,7 +584,7 @@ class Validator {
*/
protected function validate_alpha($attribute, $value)
{
- return preg_match('/^([a-z])+$/i', $value);
+ return $this->validate_match($attribute, $value, 'a-z');
}
/**
@@ -596,7 +596,7 @@ class Validator {
*/
protected function validate_alpha_num($attribute, $value)
{
- return preg_match('/^([a-z0-9])+$/i', $value);
+ return $this->validate_match($attribute, $value, 'a-z0-9');
}
/**
@@ -608,7 +608,28 @@ class Validator {
*/
protected function validate_alpha_dash($attribute, $value)
{
- return preg_match('/^([-a-z0-9_-])+$/i', $value);
+ return $this->validate_match($attribute, $value, '-a-z0-9_-');
+ }
+
+ /**
+ * Validate that an attribute contains only the characters specified in $match
+ *
+ * @param string $attribute
+ * @param mixed $value
+ * @param string $match
+ * @param string $flags
+ * @return bool
+ */
+ protected function validate_match($attribute, $value, $pattern) {
+ // this allows for escaped commas in the pattern array
+ while (substr($pattern[0], -1, 1) == '\\') {
+ $pattern[0] = $pattern[0] . $pattern[1];
+ $pattern = array_merge(array($pattern[0]), array_slice($pattern, 2));
+ }
+
+ $match = $pattern[0];
+ $flags = isset($pattern[1]) ? $pattern[1] : 'i';
+ return preg_match('/^([' . $match .'])+$/' . $flags, $value);
}
/**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment