propelorm/Propel — Issue #181: Match validator fails with escaped single quote
Author: jakerella
Created: 2011-11-07 21:03:58+00:00
State: open

Using a single quote in a match validator (or indeed, I think, in any validator "value" attribute) will cause the resulting ObjectTableMap to fail since the generated PHP uses single quotes to wrap the value.

Example Schema:
```

```

Resulting code in MyObjectTableMap:
```public function initialize()
{
...
$this->addValidator('FIELD', 'match', 'propel.validator.MatchValidator', '/[\\']/', 'This should generate an error in MyObejctTableMap');
...
} // initialize()
```

This code is generated on LINE 251 (and 248) of /generator/lib/builder/om/PHP5TableMapBuilder.php:
```protected function addInitialize(&$script)
{
...
\$this->addValidator('$cup', '".$rule->getName()."', '".$rule->getClass()."', '".str_replace("'", "\'", $rule->getValue())."', ".$val->getTranslate()."('".str_replace("'", "\'", $rule->getMessage())."'));";
...
}
```

It looks like the single quote is replaced with "\'", but unfortunately if it is in a regex and escaped already this results in a double slashed single quote, which is actually an escaped slash an a plain single quote, and this breaks the generated code. Perhaps the fix is to not use str_replace, but preg_replace instead and only replace the string if there is no "\" in front of the singe quote?

So lines 248 and 251 would then read:
```\$this->addValidator('$cup', '".$rule->getName()."', '".$rule->getClass()."', '".preg_replace("/[^\\\]\'/", "\'", $rule->getValue())."', ".$val->getTranslate()."('".preg_replace("/[^\\\]\'/", "\'", $rule->getMessage())."'));";
...
\$this->addValidator('$cup', '".$rule->getName()."', '".$rule->getClass()."', '".preg_replace("/[^\\\]\'/", "\'", $rule->getValue())."', '".preg_replace("/[^\\\]\'/", "\'", $rule->getMessage())."');";
```

Thoughts?