1

Situation where I came across: Consider the following cases :

  1. Route::pattern() or we can use Route::({method}/{keyword})->where('keyword', {regex});
  2. Validation Rule array( 'keyword' => "/^{regex}$/");

Both accepts different format of regex in case 1: Route::pattern() or in Route::()->where() it does not accept regex in /^{regex}$/ format. It only accepts Route::pattern('keword', {regex}).

in case 2: its accepts regex in /^{regex}$/ pattern.

So in short, I can't apply the same in both place. That is why I have to write two different regex, however they are same.

Am I missing something? Is that possible that model regex and route pattern regex can be defined at once? So changing one regex, we dont have to change another one. Just because of /^$/ ?

Rajan Rawal
  • 6,171
  • 6
  • 40
  • 62

1 Answers1

1

Actually you can't do that because in route declaration the regex is simplified, it just accepts a pattern string and it should not contain ^$ because when the route is being compiled by Symfony\Component\Routing\Route class; it just removes the ^ and $ from that string and if you provide slashes /.../ then the route won't work. So, you can't use a regular expression like /^[...]$/ in route declaration. In the Symfony\Component\Routing\Route class you may find a method like this (which prepares the route's regex):

private function sanitizeRequirement($key, $regex)
{
    if (!is_string($regex)) {
        throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
    }

    if ('' !== $regex && '^' === $regex[0]) {
        $regex = (string) substr($regex, 1); // returns false for a single character
    }

    if ('$' === substr($regex, -1)) {
        $regex = substr($regex, 0, -1);
    }

    if ('' === $regex) {
        throw new \InvalidArgumentException(sprintf('Routing requirement for "%s" cannot be empty.', $key));
    }

   // ...

    return $regex;
}

On the other hand, the Validator expects a valid regular expression and you need to specify one like /^[...]$/. So, that's not possible to use same regex for Route and Validator because they are different things and works differently.

Even if both were same then there is no built-in way to get the route regex pattern in the Validator to apply as a rule. Keep it simple, use those the way they work and don't make it too complicated.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • ok. agreed. But let me tell one thing. Suppose I am using username filed. For which I have validation regex in model class and then say in route I want to use `/users/{username}` and applying regex. Now say if some new characters are allowed in username then I'll have to change it at two places. Which turn un organized. SO... – Rajan Rawal Jun 09 '14 at 09:35
  • Definitely but you may keep a common pattern in a file and can use that in both places but it would be so much a hacky way. For example, create a file `regexes.php` and declare and return an array (associative) and add `'username' => '[a-z_]'`, now you may save this file in `config` folder and then from both places you can access that pattern using `Config->get('regexes.username')` but you need to use `'/^'. Config.get('regexes.username') .'$/'`in `validator and without `/^$/` in `route`. – The Alpha Jun 09 '14 at 09:44
  • Idea is really good but again we are the situation which we wanted to fix without any custom methods. Anyway for now, what I understand is that using the same one at two places. and with changes, keep in mind to reflect is at two places. I was creating one API so that this would be great but anyway... Thank you Seikh – Rajan Rawal Jun 09 '14 at 09:47
  • 1
    I liked your idea about keeping that in one common file! Say in config folder and we can access using `Config` object. Right? Simple but it didn't light me! – Rajan Rawal Jul 23 '14 at 16:50