2

Field : field_date_1
Type : Date
Widget : Pop-up calendar
The settings for date field is as shown in 2 images

enter image description here
enter image description here

I get the values of a form_state into the $source_node and move that to $fs variable.The $source_node has date field stored as follows :

 [field_pt_date_1] => Array
        (
            [und] => Array
        (
                    [0] => Array
                        (
                            [value] => 2010-08-27 00:00:00
                            [timezone] => America/Phoenix
                            [timezone_db] => America/Phoenix
                [date_type] => datetime
                        )
             )

)

 $fs['values']['field_date_1']['und'][0]['value'] = date('m/d/Y', strtotime($source_node->field_date_1['und'][0]['value']));
  $fs['values']['field_date_1']['und'][0]['timezone'] = $source_node->field_date_1['und'][0]['timezone'];
  $fs['values']['field_date_1']['und'][0]['timezone_db'] = $source_node->field_date_1['und'][0]['timezone_db'];
  $fs['values']['field_date_1']['und'][0]['date_type'] = $source_node->field_date_1['und'][0]['date_type'];

......... The errors I am getting after executing the above code is as follows :

A valid date is required for date_1.

 drupal_form_submit('form_id', $fs, $source_node);
  $errors = form_get_errors();

And when checked the $errors variable I get

$errors = Array
(
 [field_date_1][und][0][value] => A valid date is required for <em class="placeholder">date_1</em>.
)
  • date('m/d/Y', strtotime($source_node->field_date_1['und'][0]['value'])); why this conversion instead of the actual value ? – jonhattan Oct 4 '13 at 7:38
  • I had same code in D6 where it used the above date conversion and a comment was left that error will be produced while executing the drupal_execute() in D6 .So I made the changes.. Also I have tried by directly placing the value but it still gives me error. Pls help . I want that this issue be solved by the end of day. – harshal Oct 4 '13 at 7:43
  • When I use node_save() I dont get the same error. – harshal Oct 4 '13 at 10:09
0

I was facing similar kind of problem with drupal_form_submit.

After spending too much time following solution is working for me

    $datetime = $source_node->field_date_1['und'][0]['value'] ;
    $form_state['values']['field_date_1']['und'][0]['value'] = array(
      'date' => date('d/m/Y', strtotime($datetime)),
      'time' => date('h:ia', strtotime($datetime)),
    );

Hope it will help someone

  • 1
    Weird that it is not mentioned anywhere in document. Ideally I would expect a function by the module that takes care of this. – Gokul N K Jan 18 '16 at 9:31

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.