Match validator fails with escaped single quote #181

jakerella opened this Issue Nov 7, 2011 · 0 comments


None yet

1 participant


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())."');";


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment