0

I have an inherited project to which I need to add small form api changes. The changes are to two form fields shown below:

$form['start'] = [
    '#type' => 'number',
    '#title' => t('Between:'),
    '#required' => TRUE,
    '#default_value' => 1988,
    '#min' => 1900,
    '#max' => date('Y'),
    '#value' => 1988,
];
$form['test'] = [
    '#type' => 'number',
    '#title' => t('TEST:'),
    '#required' => TRUE,
    '#default_value' => 1988,
    '#max' => date('Y'),
    '#min' => 1900,
    '#value' => 1995,
];
$form['end'] = [
    '#type' => 'number',
    '#title' => t('and'),
    '#required' => TRUE,
    '#min' => 1900,
    '#max' => date('Y'),
    '#default_value' => 1999,
    '#value' => 1999,
];

This is a bizarre problem as my attempts to set the #value and #default_value for both the start and end fields are not working.

However, I created the test field and it successfully adds to the form and sets my configured value and default_value.

So, for the start field, I expect to see 1988 as the displayed value but instead that form field displays 1900. The test field I create meanwhile displays and behaves properly ... go figure.

As it is an inherited project, I wonder if there are other means of configuring a form which I am unaware of ?

UPDATE: Thanks to Leymannx for contributing and attempting to help. As per your suggestions, I tried the following but without success:

$form['start'] = [
    '#type' => 'number',
    '#title' => t('Between:'),
    '#required' => TRUE,
    '#min' => 1900,
    '#max' => idate('Y'),
    '#default_value' => 1988,
];

Note: The problem is that both the start and end fields refuse all my efforts to configure the value and default_value. So, for the start field, I expect to see 1988 but instead that form field displays 1900.

8
  • I'd say date('Y') is the problem. Try idate('Y') to format a local date/time as integer. – leymannx Apr 20 '20 at 11:35
  • Thanks @leymannx but I think you misunderstand. The problem is that both the start and end fields refuse all my efforts to configure the value and default_value. So, for the start field, I expect to see 1988 but instead that form field displays 1900 – sisko Apr 20 '20 at 11:40
  • Ah now I see. Remove '#value'. – leymannx Apr 20 '20 at 11:41
  • @leymannx: Just tried commenting out '#value' but it didn't fix the issue – sisko Apr 20 '20 at 11:45
  • You should also try idate(). And remove #value. Then update your question if that's still not working – leymannx Apr 20 '20 at 11:45
-1

I removed all '#value' keys and replaced date('Y') with idate('Y') and for me it's working without any problem. Flush cache!

$form['start'] = [
  '#type' => 'number',
  '#title' => t('Between') . ':',
  '#required' => TRUE,
  '#default_value' => 1988,
  '#min' => 1900,
  '#max' => idate('Y'),
];
$form['test'] = [
  '#type' => 'number',
  '#title' => t('TEST') . ':',
  '#required' => TRUE,
  '#default_value' => 1988,
  '#max' => idate('Y'),
  '#min' => 1900,
];
$form['end'] = [
  '#type' => 'number',
  '#title' => t('and'),
  '#required' => TRUE,
  '#min' => 1900,
  '#max' => idate('Y'),
  '#default_value' => 1999,
];

enter image description here

9
  • 1
    Or there could be a form alter, a theme function/preprocessing for these fields that's messing with the default value – Leigh Apr 20 '20 at 12:11
  • 1
    Or JavaScript... – leymannx Apr 20 '20 at 12:27
  • 1
    The form element is showing the #min value if an initial value is not set. That is the OP's issue, which we cannot reproduce. – apaderno Apr 20 '20 at 14:58
  • 1
    @kiamlaluno – I tried it out. It just shows nothing, when no #default_value is set. No matter what #min and #max where set. – leymannx Apr 20 '20 at 15:03
  • 1
    There must be code that is interfering in some way, from the same module, or a different one. If I recall correctly, Drupal also uses validation on client side, but I don't know in which way a browser or JavaScript code could interfere with that. – apaderno Apr 20 '20 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.