Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:stack_overflow:29779716

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

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.1pele_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->add('business_number', 'integer', array(
 +            'required' => false,
 +        ));
 +    }
 +}
 +</code>
 +
 +Here is my validation rule
 +
 +<code yaml>
 +My\Bundle\Entity\Business:
 +    properties:
 +        business_number:
 +            - Type:
 +                type: integer
 +</code>
 +
 +So nothing extravagant!
 +
 +But I get the following error
 +
 +> Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Warning: NumberFormatter::parse(): Number parsing failed"
 +
 +I already found a work around [[http://forum.symfony-project.org/viewtopic.php?t=40191&p=130870|here]], but it doesn't feel right to do that. I will if there is no other solution but I prefer to avoid it.
 +
 +I know it was a known bug in earlier version of Symfony but it is supposed to be fix. See [[https://github.com/symfony/symfony/pull/5943|here]].
 +
 +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://github.com/symfony/symfony/issues/14430|bug report]]
 +
 +<WRAP help>
 +The problem is that the ''integer'' and/or ''number'' Symfony Form Type utilizes the [[https://github.com/symfony/symfony/blob/v2.6.6/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php#L533|Symfony\Component\Intl\NumberFormatter\NumberFormatter::parse method]] before storing the value to the Form. The contents of the method are as such (as of Symfony 2.6.6):
 +
 +<code php>
 +public function parse($value, $type = self::TYPE_DOUBLE, &$position = 0)
 +{
 +    if ($type == self::TYPE_DEFAULT || $type == self::TYPE_CURRENCY) {
 +        trigger_error(__METHOD__.'(): Unsupported format type '.$type, \E_USER_WARNING);
 +        return false;
 +    }
 +    preg_match('/^([^0-9\-\.]{0,})(.*)/', $value, $matches);
 +    // Any string before the numeric value causes error in the parsing
 +    if (isset($matches[1]) && !empty($matches[1])) {
 +        IntlGlobals::setError(IntlGlobals::U_PARSE_ERROR, 'Number parsing failed');
 +        $this->errorCode = IntlGlobals::getErrorCode();
 +        $this->errorMessage = IntlGlobals::getErrorMessage();
 +        $position = 0;
 +        return false;
 +    }
 +    preg_match('/^[0-9\-\.\,]*/', $value, $matches);
 +    $value = preg_replace('/[^0-9\.\-]/', '', $matches[0]);
 +    $value = $this->convertValueDataType($value, $type);
 +    $position = strlen($matches[0]);
 +    // behave like the intl extension
 +    $this->resetError();
 +    return $value;
 +}
 +</code>
 +
 +Notably this part:
 +
 +<code php>
 +preg_match('/^([^0-9\-\.]{0,})(.*)/', $value, $matches);
 +// Any string before the numeric value causes error in the parsing
 +if (isset($matches[1]) && !empty($matches[1])) {
 +    IntlGlobals::setError(IntlGlobals::U_PARSE_ERROR, 'Number parsing failed');
 +
 +// ...
 +</code>
 +
 +will cause any malformed entry with a string at the start to throw an Exception.
 +
 +Unfortunately, changing the validation rules will do nothing as this parsing is run //before// validation occurs.
 +
 +Your only workaround will be the one you've linked, and to submit a bug report until the problem is fixed. The [[https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php#L533|current master branch doesn't have an update to this file]] and it's unclear if the problem was solved elsewhere (further research would be required).
 +
 +Front-end validation could also help (for example, the built-in validation for HTML5 ''number'' and ''integer'' types will cause most browsers to stop you before submitting to Symfony).
 +</WRAP>
 +<WRAP info>
 +[[https://stackoverflow.com/questions/29779716/symfony-form-validation-of-integer-field|Symfony form validation of integer field - Stack Overflow]]
 +</WRAP>