Number class return error " is not a valid number."

Created on 12 February 2025, 2 months ago

Problem/Motivation

We create a content type with the field "field.field.node.training_instance.field_trn_inst__course_fee".
There is an error when to add/change the value when the number is great than 3596000.00.

Steps to reproduce

There is an error when to add/change the value when the number is great than 3596000.00.

The config of the field:

langcode: en
status: true
dependencies:
  module:
    - node
id: node.field_trn_inst__course_fee
field_name: field_trn_inst__course_fee
entity_type: node
type: decimal
settings:
  precision: 10
  scale: 2
module: core
locked: false
cardinality: 1
translatable: true
indexes: {  }
persist_with_no_fields: false
custom_storage: false
Drupal core 10.3.10 => Number.php
public static function validateNumber(&$element, FormStateInterface $form_state, &$complete_form) {
	// other code.
if (!NumberUtility::validStep($value, $element['#step'], $offset)) {
   $form_state->setError($element, t('%name is not a valid number.', ['%name' => $name]));
}
// other code.
}

Proposed resolution

Implement code in a custom module:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * Override the Node edit form to fix a Drupal core bug: ticket AB#529796.
 */
function sis_training_form_node_training_instance_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Add custom validation for the course fee field.
  if (isset($form['field_trn_inst__course_fee'])) {
    $form['field_trn_inst__course_fee']['widget'][0]['value']['#element_validate'][] = 'sis_training_course_fee_validation';
  }
}

/**
 * Custom validation for the Course Fee field.
 */
function sis_training_course_fee_validation(&$element, FormStateInterface $form_state, &$complete_form) {
  $value = $element['#value'];

  // Custom validation logic: ensure it's numeric and within range.
  if (!is_numeric($value) || $value < 0 || $value > 100000000) {
    $form_state->setError($element, t('%name must be a valid number between 0 and 100,000,000.', ['%name' => $element['#title']]));
  }
}

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

10.3 ✨

Component

field system

Created by

πŸ‡ΊπŸ‡ΈUnited States gung wang

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024