Commit e8716ebf authored by webchick's avatar webchick
Browse files

#950134 by mfb: Fixed Number module maxlength prevents saving negative decimal.

parent c10220f9
...@@ -6,4 +6,5 @@ version = VERSION ...@@ -6,4 +6,5 @@ version = VERSION
core = 7.x core = 7.x
dependencies[] = field dependencies[] = field
files[] = number.module files[] = number.module
files[] = number.test
required = TRUE required = TRUE
...@@ -330,8 +330,9 @@ function number_field_widget_form(&$form, &$form_state, $field, $instance, $lang ...@@ -330,8 +330,9 @@ function number_field_widget_form(&$form, &$form_state, $field, $instance, $lang
'#default_value' => $value, '#default_value' => $value,
// Allow a slightly larger size that the field length to allow for some // Allow a slightly larger size that the field length to allow for some
// configurations where all characters won't fit in input field. // configurations where all characters won't fit in input field.
'#size' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 2 : 12, '#size' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 4 : 12,
'#maxlength' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] : 10, // Allow two extra characters for signed values and decimal separator.
'#maxlength' => $field['type'] == 'number_decimal' ? $field['settings']['precision'] + 2 : 10,
// Extract the number type from the field type name for easier validation. // Extract the number type from the field type name for easier validation.
'#number_type' => str_replace('number_', '', $field['type']), '#number_type' => str_replace('number_', '', $field['type']),
); );
......
<?php
// $Id$
/**
* @file
* Tests for number field types.
*/
/**
* Tests for number field types.
*/
class NumberFieldTestCase extends DrupalWebTestCase {
protected $field;
protected $instance;
protected $web_user;
public static function getInfo() {
return array(
'name' => 'Number field',
'description' => 'Test the creation of number fields.',
'group' => 'Field types'
);
}
function setUp() {
parent::setUp('field_test');
$this->web_user = $this->drupalCreateUser(array('access field_test content', 'administer field_test content'));
$this->drupalLogin($this->web_user);
}
/**
* Test number_decimal field.
*/
function testNumberDecimalField() {
// Create a field with settings to validate.
$this->field = array(
'field_name' => drupal_strtolower($this->randomName()),
'type' => 'number_decimal',
'settings' => array(
'precision' => 8, 'scale' => 4, 'decimal_separator' => '.',
)
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'widget' => array(
'type' => 'number',
),
'display' => array(
'default' => array(
'type' => 'number_decimal',
),
),
);
field_create_instance($this->instance);
// Display creation form.
$this->drupalGet('test-entity/add/test-bundle');
$langcode = LANGUAGE_NONE;
$this->assertFieldByName("{$this->field['field_name']}[$langcode][0][value]", '', t('Widget is displayed'));
// Submit a signed decimal value within the allowed precision and scale.
$value = '-1234.5678';
$edit = array(
"{$this->field['field_name']}[$langcode][0][value]" => $value,
);
$this->drupalPost(NULL, $edit, t('Save'));
preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match);
$id = $match[1];
$this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), t('Entity was created'));
$this->assertRaw(round($value, 2), t('Value is displayed.'));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment