2

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

10
  • 2
    Are you trying to input a phone number or an integer? Phone numbers are a terrible thing to store as integers. You have to strip out things like dashes and potentially useful information (what about extensions? How do I express that as an integer?) You should use a text Form Type with regex validation instead, or come up with a custom form type so you can utilize the HTML5 tel input type. Commented Apr 21, 2015 at 17:56
  • It's an integer I need to store. It's not a phone number. Commented Apr 21, 2015 at 18:01
  • Do you get this error when you submit the form and leave the business_number blank? Commented Apr 21, 2015 at 18:05
  • Nope, it is working properly when blank or null. Commented Apr 21, 2015 at 18:07
  • It should be noted that the PR you linked to the Symfony2 repo is for the number Form Type, not the integer one, although the IntegerToLocalizedStringTransformer extends the NumberToLocalizedStringTransformer. Could you try using a number Form Type instead? Commented Apr 21, 2015 at 18:11

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I will do a bug report.
@A.D. Please do link it here. I may look into it more in my free time.
I added the link to the bug report in the original message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.