Required field_date with default value 'Now'

Hide field_date from user input:

hook_form_alter(&$form, &$form_state, $form_id) {
    $form['field_date']['#access'] = FALSE;

After saving node the required field_date with default value is empty.

Notice: Undefined index: field_date in date_combo_validate() (line 383 of /modules/date/date_elements.inc).

Members fund testing for the Drupal project. Drupal Association Learn more

Comments

KarenS’s picture

Status: Active » Fixed

This should be fixed in latest dev code.

dusov’s picture

Title: Hidden field and date_combo_validate() » Hidden field and required default value
Version: 7.x-2.0-alpha4 » 7.x-2.x-dev
Status: Fixed » Active

After saving node the required field_date with default $value is empty.

No errorrs or warnings as date_hidden_element($element) always returns TRUE.

So if i want to grant access for one role and hide from others with default value, i cant use Form API

KarenS’s picture

Status: Active » Fixed

I reworked the default value handling so it happens earlier, before the field is processed. This should make sure default values get set even on hidden fields.

KarenS’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

kopeboy’s picture

Title: Hidden field and required default value » Hidden field and required default value: The dates are invalid.
Priority: Normal » Major
Issue summary: View changes
Status: Closed (fixed) » Active

I set default value to "now" and hide the date field from the node form (at manage fields I use Display Suite and disable it, cause I only want default value).

Node can't be saved:

There are errors in Top Date value #1:
The dates are invalid.

I tried with different formats..

If I enable the field in the form, I can see the default value and on save it works without notices..

kopeboy’s picture

Version: 7.x-2.x-dev » 7.x-2.8

I'm actually using 2.8

Alauddin’s picture

Seeing same issue on both dev version as well as 7.x-2.8

There are errors in Updated Date value #1:
The dates are invalid.

Also using DS to hide date field from user and using 'now' for default value.

nithinkolekar’s picture

Title: Hidden field and required default value: The dates are invalid. » date field with default value set to now and made hidden, ends with validation error "The dates are invalid" on form submmision

Whether it is required or not when field is in the hidden region of display suite forms this error is still present. This is happening only when default value is set to now.
Additional info
- Widget : select list
- Date format is set to accept only Year,Month, Day
- Date entry options : j M Y - H:i:s , -1 years from now , +0 years from now
- Tested with both required and non-required settings.

NWOM’s picture

This error occurs also on fields set to "none" rather than just "now". I have a panel set up with both fields not exposed and are both optional. I have rules set up to auto-add a value to each field after saving the node.

It works fine when both fields are visible in the panel, but not when I remove them.

monaw’s picture

I'm having the same error as kopeboy (#6). I'm also using Date 2.8. I have a date, not required, set to default to now but during form processing, I get the error messages:

There are errors in Timestamp value #1:
The dates are invalid.

Does someone have a patch? I'm desperate…

milos.kroulik’s picture

Title: date field with default value set to now and made hidden, ends with validation error "The dates are invalid" on form submmision » Hidden date field leads to validation error "The dates are invalid" on form submmision

I've made the same observation as #10 regarding default value - it doesn't matter. Changed the title to reflect this. I understand, that it might mean, that there are 2 separate issues.

monaw’s picture

Ok, I've fixed my runtime error by setting my field_ticker_timestamp field as follows:

$form['field_ticker_timestamp']['#access'] = FALSE;

Hope this helps someone else.

By the way, I see a new release 2.9 but from the release note, it does not appear this issue is fixed.

PapaGrande’s picture

I ran into this issue and discovered that the cause was that a multi-value field with over 600 entries in the form above my date field was causing $form_state['input'] to have null values and $form_state['values'] to not have any updates. That caused $posted = drupal_array_get_nested_value($form_state['input'], $element['#parents']); (line 456 of date_elements.inc) to have a null value, which eventually triggers the error in if (!$instance['required'] && (!date_is_date($from_date) || !date_is_date($to_date))) { (line 543).

I didn't have time to debug the Drupal Form API and how it populates $form_state, but my workaround was to disable my multi-value field if it has over 100 entries. That allowed date_combo_validate() to validate my field correctly.

joseonate’s picture

Same problem
I'm removing the field programmatically using ['#access'] = FALSE;
The form renders without the date field, but upon submission the error occurs: The dates are invalid.
The field is not required. It doesn't matter if default value is set to now or none.
Tried Date 7 - 2.9, as well as the dev version dated 2015-Oct-02

Workaround
I don't have a solution, but in order to be able to proceed with my current setup, I documented-out the code that is creating this false error. Line 572 of date_elements.inc now reads:
//$errors[] = t('The dates are invalid.');

The error that prevented the form submission is now gone, and field validation still takes place for other more normal scenarios.

peterlolty’s picture

Version: 7.x-2.8 » 7.x-2.x-dev
peterlolty’s picture

re #14, @PapaGrande may you explain more about the case?

maximpodorov’s picture

vasike’s picture

I can confirm this issue for the Hidden Date fields with Display Suite.
I'm not sure if it's a Date issue, but a Display Suite issue.

maximpodorov’s picture

It's definitely a Date module issue.

Yuri’s picture

It's not a Display Suite issue. I don't use DS and have the same problem using a custom entity add form that doesnt use the date field.

Yuri’s picture

The patch #18 does save the form data, but gives a white ERROR page with:
"EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of /home/domains/public_html/sites/all/modules/entity/includes/entity.wrapper.inc)."

halmsx’s picture

hi

having same problem in panels.

i have a node with lotsa fields. but in panel i only display a few of fields. for now i just add the date fields and use css to hide them.

There are errors in Commencement date value #1:
The dates are invalid.
shi99’s picture

Patch #18 worked for me.

I was disabling date fields via code form api states (When a checkbox is clicked then disable some form fields).
I could get the "The dates are invalid" error.

Patch #18 solved that problem for me.

Thanks

PapaGrande’s picture

Status: Needs review » Needs work

I don't think this patch is the right solution.

+++ b/date_elements.inc
@@ -482,6 +482,11 @@ function date_combo_validate($element, &$form_state) {
+  // Do not validate values which do not exist in form input.
+  if (!isset($posted['value'])) {
+    return;
+  }
+

While this avoids the error, it doesn't address the underlying issue of why $form_state['input'] has null values.

I did some more investigation into my situation (#14), and found that the reason why $form_state['input'] (and $form_state in general) was messed up was that the $_POST variable from the form submit was getting overloaded. When I changed the max_input_vars setting in php.ini from the default of 1000 to 2000, the date validation error went away. In my situation, I had another field on the form that had over 600 values in an array and that was overloading $_POST. Try changing max_input_vars on your server and see if that solves your issue.

maximpodorov’s picture

No, max_input_vars doesn't fix the problem. I have this value 100000, and the problem remains. The real source of the problem (but not the bug!) is inside Form API.

thirdender’s picture

I think the situation described in #25 is more of an edge case, although a quick google search for "drupal max_input_vars" does bring up a few issues that mention this problem. Unfortunately, it's really a PHP configuration issue. I think the best discussion about managing that in core is at #1565704: Core interfaces can go over max_input_vars.

I ran into this issue after hiding a Date field using Display Suite. I think a better solution than checking $form_state['input'] is to rely on the $element['#needs_validation'] value. It works for fields hidden by Display Suite. Can anyone check to see if this also works with Panels?

Shane Birley’s picture

I ran into this today and applied the patch #27 and it does fix the issue with validation on hidden fields.

Is this the best way?

maximpodorov’s picture

Could you try #18 ?

dagomar’s picture

@thirdender

I sadly cannot confirm this works for panels. Perhaps my setup differs a bit:

I have a date field which is mandatory. I am pulling apart the form in different pages with panels, but the mandatory date field gives the error, even when it is not displayed. I tried adjusting the form when rendering the panels page, but to no avail. Any input greatly appreciated.

thirdender’s picture

Neither patch #18 or patch #27 seems to work with Panels. Panels acts as though the field is rendered and submitted with empty fields.

#18 doesn't work because the rough values of $form_state['input'] and $form_state['values'] look like this when a Date field is hidden by Panels:

$form_state['input']['field_FIELD_NAME'][LANGUAGE_NONE][0]['value'] =>
  array(
    'year' => NULL,
    'month' => NULL,
    'day' => NULL,
  );

$form_state['values']['field_FIELD_NAME'][LANGUAGE_NONE][0]['value'] =>
  array(
    'value' => '2016-10-20',
    'timezone' => 'America/New_York',
    'timezone_db' => 'UTC',
    'date_type' => 'datetime',
    'value2' => '2016-10-20 23:06:40',
    'show_todate' => TRUE,
  );

#27 doesn't work because ['#needs_validation'] is only ever set to TRUE by Drupal core.

Not really sure how to handle fields when they're hidden, might need some input from someone better versed in the Form API. $form['field_FIELD_NAME']['#access'] is set to FALSE by both Panels and Display Suite, but I don't know if that's available to the ['#element_validate'] function (date_combo_validate). In fact, I'm not sure why the ['#element_validate'] function is called at all if the field ['#access'] is set to FALSE.

lazzyvn’s picture

#27 doesn't work i tested with form_alter access =false and hidden field collection (collection field have one date field) i got
There are errors in Date value #1:
- The dates are invalid.

hmdnawaz’s picture

I can confirm this error occurs on hidden date with display suit.

tokyonohana’s picture

i am using hidden type instead of access false but this works for me:

// hide the wrapper
$form[$field_name]['#type'] = 'hidden';
// hide the date combo as well
// see date_combo_validate()
$form[$field_name][LANGUAGE_NONE][0]['#type'] = 'hidden';
swetavishal’s picture

I have provided field_permission to date field . It is working for me.

DrupalHack’s picture

I am also encountering this issue using Panels on 7.x-2.x-dev.
Patch #27 and #18 did not work for me.