I have a webform that has a Date Obtained field.

Label Form Key Type Value
Date Obtained date_obtained1 Date -

It is not required.

I have 2 other labels that are the same thing except Date Obtained 2/3 and date_obtained2/3, again not required.

Under the edit, I have the Day hidden, start date -30 years, and end date +2 years and a popup calendar enabled.

The issue I have is when this form is submitted, it gives me the following if the user doesn't select a date for the fields:
Month in Date Obtained is missing.
Year in Date Obtained is missing.
Month in Date Obtained 2 is missing.
Year in Date Obtained 2 is missing.
Month in Date Obtained 3 is missing.
Year in Date Obtained 3 is missing.

And will not submit the form.

Anyone have this before or know how to fix it? Thanks.

Dan

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dcamburn created an issue. See original summary.

dcamburn’s picture

Issue summary: View changes
DanChadwick’s picture

Category: Bug report » Support request
Status: Active » Fixed

Webform does not support hiding the day. If you want to hide the day, you're going to have to write some more complicated javascript to include a day (e.g. 1) when month/year are present and not when it isn't.

dcamburn’s picture

Hide
Day
Month
Year
A hidden day or month will be set to 1. A hidden year will be set to the year of the default value.

And the Day is checked here. It doesn't show up on the webform as selectable, which is fine. The issue is that the user shouldn't have to select a month or year here since it is not required. However, it is forcing them to select such.

I'll assume that possibly someone already wrote some of the javascript that you are talking about on my site. I wasn't part of the original development.

dcamburn’s picture

Status: Fixed » Active
DanChadwick’s picture

Status: Active » Fixed

Yes, you have some custom code. You'll need to find support for that outside the webform issue queue.

dcamburn’s picture

Dan, I don't mean to beat a dead horse: but maybe you can help out. I just downloaded the webform module to check all the files vs the original files. This is under webform\components\date.inc

$form['extra']['exclude'] = array(
'#type' => 'checkboxes',
'#title' => t('Hide'),
'#default_value' => $component['extra']['exclude'],
'#options' => array(
'day' => t('Day'),
'month' => t('Month'),
'year' => t('Year'),
),
'#description' => t('A hidden day or month will be set to 1. A hidden year will be set to the year of the default value.'),
'#weight' => 3,
);

That's exactly what the webform says and it looks like the ability to exclude the day, month, and year has been added. Any ideas?

DanChadwick’s picture

Category: Support request » Bug report
Status: Fixed » Active

Wow. My error. I had forgotten that this feature went in.

I suggest you don't use hiding the day until this bug is addressed.

AlinCut’s picture

Issue tags: +Cannot display partial date fields
trzcinski.t@gmail.com’s picture

I have came across this issue as well.

It turns out that those are the lines that are responsible for the issue:

function webform_validate_date($element, $form_state) {
  if ($element['month']['#value'] !== '' || $element['day']['#value'] !== '' || $element['year']['#value'] !== '') {
    // Check that each part of the date has been filled in.
    foreach (array('day', 'month', 'year') as $field_type) {
      if (empty($element[$field_type]['#value'])) {
        form_error($element[$field_type], t('!part in !name is missing.', array('!name' => $element['#title'], '!part' => $element[$field_type]['#title'])));

Basically when the day / month field is hidden - it gets automatically set to 1. Therefore if we leave other fields empty (even for non-required field) the condition is met, inner block is executed and error set.

To prevent that I have added a check - the value for the date part is only checked if this part is not hidden / excluded.

function webform_validate_date($element, $form_state) {
  // An array of excluded elements.
  $excludes = isset($element['#exclude']) ? $element['#exclude'] : array();
  // Create a few variables as a shorthand for next checks.
  $day_exclude = in_array('day', $excludes);
  $month_exclude = in_array('month', $excludes);
  $year_exclude = in_array('year', $excludes);

  // We need to check the value for a given parte of the date only if the part
  // has not been excluded. If it has been excluded - the value is set to 1 and
  // will cause error of missing fields.
  if ((!$month_exclude && $element['month']['#value'] !== '') || (!$day_exclude && $element['day']['#value'] !== '') || (!$year_exclude && $element['year']['#value'] !== '')) {

that helped but caused php notices in submit function - so I have added a few issets to submit function as well:

function _webform_submit_date($component, $value) {
  // Convert the date to an ISO 8601 format.
  // Additional checks to prevent php notices.
  return (isset($value['year']) && $value['year'] && isset($value['month']) && $value['month'] && isset($value['day']) && $value['day']) ? webform_date_string($value, 'date') : '';
}

This worked for me.

trzcinski.t@gmail.com’s picture

Status: Active » Needs review
humansky’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #10 works for me

CatherineOmega’s picture

The patch in #10 works for me as well.

arunkumark’s picture

The patch #10 working fine for Webform 7.x-4.12.

Ramya Bala’s picture

The patch #10 works for me

Ramya Balasubramanian’s picture

I have applied this patch #10 and its working fine.

  • DanChadwick committed ec32e33 on 7.x-4.x
    Issue #2612844 by tom_ek, DanChadwick: Fixed non-required partial dates...
DanChadwick’s picture

Version: 7.x-4.12 » 7.x-4.13
Status: Reviewed & tested by the community » Fixed
Issue tags: -Cannot display partial date fields
FileSize
1.57 KB

Thanks for the patch. Great work.

I simplified the test a bit and removed the safety checks for #required. Also, I don't get the PHP notices you mention. Please re-open if you do with an explanation. I don't see why/how the day/month/year should be missing from the value array. They should be present but empty.

Committed to 7.x-4.x.

DanChadwick’s picture

Version: 7.x-4.13 » 8.x-4.x-dev
Category: Bug report » Task
Status: Fixed » Patch (to be ported)
dcamburn’s picture

Thanks for the hard work on this guys!

fenstrat’s picture

Version: 8.x-4.x-dev » 7.x-4.x-dev
Category: Task » Bug report
Status: Patch (to be ported) » Fixed

Closing to clear out the old Webform 8.x-4.x branch. See #2827845: [roadmap] YAML Form 8.x-1.x to Webform 8.x-5.x.

  • DanChadwick committed ec32e33 on 8.x-5.x
    Issue #2612844 by tom_ek, DanChadwick: Fixed non-required partial dates...

Status: Fixed » Closed (fixed)

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