pele_mele:stack_exchange:stack_overflow:29779716
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| pele_mele:stack_exchange:stack_overflow:29779716 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1 | pele_mele:stack_exchange:stack_overflow:29779716 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:stack_overflow:stackoverflow-29779716 à pele_mele:stack_exchange:stack_overflow:29779716 alexis | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Symfony form validation of integer field ====== | ||
| + | I am trying to use the type validation rule with integer and it fails with some warning. | ||
| + | |||
| + | Here is my form | ||
| + | |||
| + | <code php> | ||
| + | class BusinessType extends AbstractType { | ||
| + | public function buildForm(FormBuilderInterface $builder, array $options) { | ||
| + | $builder-> | ||
| + | ' | ||
| + | )); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Here is my validation rule | ||
| + | |||
| + | <code yaml> | ||
| + | My\Bundle\Entity\Business: | ||
| + | properties: | ||
| + | business_number: | ||
| + | - Type: | ||
| + | type: integer | ||
| + | </ | ||
| + | |||
| + | So nothing extravagant! | ||
| + | |||
| + | But I get the following error | ||
| + | |||
| + | > Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: | ||
| + | |||
| + | I already found a work around [[http:// | ||
| + | |||
| + | I know it was a known bug in earlier version of Symfony but it is supposed to be fix. See [[https:// | ||
| + | |||
| + | So is there a way I can use the type validation? And if so, what am I missing? | ||
| + | |||
| + | **Edit 1** | ||
| + | |||
| + | I am using Symfony 2.6.6 | ||
| + | |||
| + | **Edit 2** | ||
| + | |||
| + | If my value starts with numbers (like //123dd//), I have the following error message, even if I customized my error message | ||
| + | |||
| + | > This value is not valid. | ||
| + | |||
| + | But if my value starts with something else, I have the error fore-mentioned. | ||
| + | |||
| + | **Edit 3** | ||
| + | |||
| + | The longest value I need to store is 9 digits long. So integer should work properly. | ||
| + | |||
| + | **Edit 4** | ||
| + | |||
| + | Here is the [[https:// | ||
| + | |||
| + | <WRAP help> | ||
| + | The problem is that the '' | ||
| + | |||
| + | <code php> | ||
| + | public function parse($value, | ||
| + | { | ||
| + | if ($type == self:: | ||
| + | trigger_error(__METHOD__.' | ||
| + | return false; | ||
| + | } | ||
| + | preg_match('/ | ||
| + | // Any string before the numeric value causes error in the parsing | ||
| + | if (isset($matches[1]) && !empty($matches[1])) { | ||
| + | IntlGlobals:: | ||
| + | $this-> | ||
| + | $this-> | ||
| + | $position = 0; | ||
| + | return false; | ||
| + | } | ||
| + | preg_match('/ | ||
| + | $value = preg_replace('/ | ||
| + | $value = $this-> | ||
| + | $position = strlen($matches[0]); | ||
| + | // behave like the intl extension | ||
| + | $this-> | ||
| + | return $value; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Notably this part: | ||
| + | |||
| + | <code php> | ||
| + | preg_match('/ | ||
| + | // Any string before the numeric value causes error in the parsing | ||
| + | if (isset($matches[1]) && !empty($matches[1])) { | ||
| + | IntlGlobals:: | ||
| + | |||
| + | // ... | ||
| + | </ | ||
| + | |||
| + | will cause any malformed entry with a string at the start to throw an Exception. | ||
| + | |||
| + | Unfortunately, | ||
| + | |||
| + | Your only workaround will be the one you've linked, and to submit a bug report until the problem is fixed. The [[https:// | ||
| + | |||
| + | Front-end validation could also help (for example, the built-in validation for HTML5 '' | ||
| + | </ | ||
| + | <WRAP info> | ||
| + | [[https:// | ||
| + | </ | ||
