Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:stackoverflow-29779716

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

class BusinessType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('business_number', 'integer', array(
            'required' => false,
        ));
    }
}

Here is my validation rule

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: “Warning: NumberFormatter::parse(): Number parsing failed”

I already found a work around 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 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 bug report

The problem is that the integer and/or number Symfony Form Type utilizes the 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):

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;
}

Notably this part:

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

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 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).

pele_mele/stack_exchange/stackoverflow-29779716.txt · Dernière modification : 2020/11/18 06:37 de alexis