Hello,

this is my first question in this forums. I already read many documentation about drupal 7, however I'm still getting many troubles developing my module. I hope you can help me.

My question is. I have a content type with a field that is a date, and many other fields of course. I want to introduce the date in the validation or submit function because I don't want the user to edit this value. The problem is so simple, I know how to set values like integers or text using "form_set_value" in the validation function, but I don't know how to set a date!!

My code:

function fablab_order_validation(&$form, &$form_state) {

  // I set the state of this order to 0
  form_set_value($form['fablab_field_order_state'], 0, $form_state); # 0 = placed

  // I want to set the "limit date" to today + 5 days. But I'm trying hardcoding a date
  form_set_value($form['fablab_field_limit_date'], '03/26/2016 - 15:00', $form_state);
}

First "form_set_value" works. But I get an error with the second: "the date is not valid format". Could somebody tell me the way to do it?

Thanks!

Comments

Jaypan’s picture

If I recall correctly, you need to set an array with the keys year, month and day:

  form_set_value($form['fablab_field_limit_date'], array('year' => 2016, 'month' => 3, 'day' => 26), $form_state);
djemili’s picture

Thanks for your anwer, but it doesn't work...

the error I'm getting is:

There are errors in Limit date value #1:

The dates are invalid.

I paste here the definition of the field. Maybe somebody knows what I'm doing wrong

function _fablab_installed_fields() {

  $field_bases = array();

//....

$field_bases['fablab_field_limit_date'] = array(
    'active' => 1,
    'cardinality' => 1,
    'deleted' => 0,
    'entity_types' => array(),
    'field_name' => 'fablab_field_limit_date',
    'indexes' => array(),
    'locked' => 0,
    'module' => 'date',
    'settings' => array(
      'cache_count' => 4,
      'cache_enabled' => 0,
      'entity_translation_sync' => FALSE,
      'granularity' => array(
        'day' => 'day',
        'hour' => 'hour',
        'minute' => 'minute',
        'month' => 'month',
        'second' => 0,
        'year' => 'year',
      ),
      'timezone_db' => 'UTC',
      'todate' => '',
      'tz_handling' => 'site',
    ),
    'translatable' => 0,
    'type' => 'datetime',
  );

and here the instance

function _fablab_installed_instances() {
  $t = get_t();
  $field_instances = array();

//...
$field_instances['node-fablab_order-fablab_field_limit_date'] = array(
    'bundle' => 'fablab_order',
    'deleted' => 0,
    'description' => t('The last date that this order can be in queue'),
    'display' => array(
      'checkout_pane' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
      'default' => array(
        'label' => 'above',
        'module' => 'date',
        'settings' => array(
          'format_type' => 'long',
          'fromto' => 'both',
          'multiple_from' => '',
          'multiple_number' => '',
          'multiple_to' => '',
          'show_remaining_days' => FALSE,
        ),
        'type' => 'date_default',
        'weight' => 5,
      ),
      'teaser' => array(
        'label' => 'above',
        'settings' => array(),
        'type' => 'hidden',
        'weight' => 0,
      ),
    ),
    'entity_type' => 'node',
    'field_name' => 'fablab_field_limit_date',
    'label' => 'Limit date',
    'required' => 0,
    'settings' => array(
      'default_value' => 'now',
      'default_value2' => 'same',
      'default_value_code' => '',
      'default_value_code2' => '',
      'entity_translation_sync' => FALSE,
      'user_register_form' => FALSE,
    ),
    'widget' => array(
      'active' => 1,
      'module' => 'date',
      'settings' => array(
        'increment' => 15,
        'input_format' => 'm/d/Y - H:i:s',
        'input_format_custom' => '',
        'label_position' => 'above',
        'no_fieldset' => 0,
        'text_parts' => array(),
        'year_range' => '-3:+3',
      ),
      'type' => 'date_text',
      'weight' => 36,
    ),
  );
}

I repeat my question again. How can I set a date value using form_set_value in the validation function?

Thanks!

djemili’s picture

I finally changed the field to be an integer and I update it with UNIX time value... So, my problem is "solved".

Anyway, if somebody knows the format of dates to update them via form_set_values an answer will be welcome.

Regards