Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 form number input over 2 billion

Tags:

php

symfony

I'm seeking some help on my Symfony number input issue as described below. I'd like to be able to enter extremely large numbers into my form, but it is currently limited to about a 2 billion maximum value.

I have an entity called Transaction with a field called "quantity" that has been defined as a BIGINT, as shown here:

/**
 * @ORM\Column(type="bigint", nullable=true)
 */
protected $quantity;

A Symfony form that allows me to populate Transactions uses the "number" type:

    $builder->add('quantity', 'number', array(
        'label'             => 'Quantity',
        'grouping'          => 1,
        'required'          => true,
    ));

This all works like a charm, except if the quantity value goes above approx. 2 billion (I haven't confirmed the number exactly). To me it looks suspiciously like a 32-bit boundary issue.

When that happens, it complains about "Number parsing failed". In the following screen shot I have entered 3 billion, and it come up with this error:

Example when I enter 3 billion

I don't believe the problem is the bigint in the Transaction entity, as this is an 8-byte value that should be able to hold 64-bits of data.

I'm using Symfony 2.7.16 (this issue was the same under earlier versions). Any help or recommendations most welcome.

Thanks!

like image 569
ritter Avatar asked Aug 02 '16 04:08

ritter


2 Answers

Yes it's 32bit issue. you can confirm it by typing

php -r 'echo PHP_INT_MAX;'
2147483647

If you can install 64 bit php build do it, I recommend using http://dotdeb.org as they provide latest stable releases.

To make sure your application is not run on 32 bit add to composer.json

"require": {
    "php-64bit": ">=5.3.9"
}

Composer will fail if you try installation on not 64bit system

If 64bit is not possible

You will need to work with strings I'm afraid.

Create custom form type that extend TextType and keep value as string but render as number input by overriding

/**
 * {@inheritdoc}
 */
public function getBlockPrefix()
{
    return 'integer';
}
like image 107
Konrad Podgórski Avatar answered Nov 15 '22 03:11

Konrad Podgórski


The maximal number of an interger in 32-bits and default value is 2,147,483,647.

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined using the constant PHP_INT_SIZE, and maximum value using the constant PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

You will have to change the PHP_INT_SIZE in the php.ini if you are actually using a 64-bits OS.

You could also upgrade to PHP 7 and use "long" instead of "int"

like image 44
prgrm Avatar answered Nov 15 '22 03:11

prgrm