Skip to content
Snippets Groups Projects
Commit 2218f528 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Patch #631048 by yched: number rounding should not be done by widgets.

parent c62f56c8
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -397,6 +397,15 @@ function hook_field_validate($obj_type, $object, $field, $instance, $langcode, &
* $object->{$field['field_name']}[$langcode], or an empty array if unset.
*/
function hook_field_presave($obj_type, $object, $field, $instance, $langcode, &$items) {
if ($field['type'] == 'number_decimal') {
// Let PHP round the value to ensure consistent behavior across storage
// backends.
foreach ($items as $delta => $item) {
if (isset($item['value'])) {
$items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
}
}
}
}
/**
......
......@@ -191,6 +191,21 @@ function number_field_validate($obj_type, $object, $field, $instance, $langcode,
}
}
/**
* Implements hook_field_presave().
*/
function number_field_presave($obj_type, $object, $field, $instance, $langcode, &$items) {
if ($field['type'] == 'number_decimal') {
// Let PHP round the value to ensure consistent behavior across storage
// backends.
foreach ($items as $delta => $item) {
if (isset($item['value'])) {
$items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
}
}
}
}
/**
* Implement hook_content_is_empty().
*/
......@@ -349,12 +364,6 @@ function number_field_widget_validate($element, &$form_state) {
if ($type == 'decimal' || $type == 'float') {
$value = strtr($value, $field['settings']['decimal_separator'], '.');
}
// Let PHP round the value to ensure consistent behavior across storage
// backends.
// @todo This should be done at field level.
if ($type == 'decimal') {
$value = round($value, $field['settings']['scale']);
}
form_set_value($element, $value, $form_state);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment