/** * {@inheritDoc} * * - extra: Holds string date incoming from POST * - value: Holds datetime information */ public function beforeSave(Field $field, $post) { if (!empty($post['date']) && !empty($post['format'])) { $date = $post['date']; $format = $post['format']; if ($date = DateToolbox::createFromFormat($format, $date)) { $field->set('extra', $post['date']); } else { $field->metadata->entity->errors($field->name, __d('field', 'Invalid date/time, it must match the the pattern: {0}', $format)); return false; } $field->set('value', date_timestamp_get($date)); } else { $field->set('value', null); } return true; }
/** * {@inheritDoc} */ public function beforeSave(Field $field, $post) { $values = []; $extra = ['from' => ['string' => null, 'timestamp' => null], 'to' => ['string' => null, 'timestamp' => null]]; foreach (['from', 'to'] as $type) { if (!empty($post[$type]['string']) && !empty($post[$type]['format'])) { $date = $post[$type]['string']; $format = $post[$type]['format']; if ($date = DateToolbox::createFromFormat($format, $date)) { $extra[$type]['string'] = $post[$type]['string']; $extra[$type]['timestamp'] = date_timestamp_get($date); $values[] = $extra[$type]['timestamp'] . ' ' . $post[$type]['string']; } else { $typeLabel = $type == 'from' ? __d('field', 'Start') : __d('field', 'Finish'); $field->metadata->entity->errors($field->name, __d('field', 'Invalid date/time range, "{0}" date must match the the pattern: {1}', $typeLabel, $format)); return false; } } } $field->set('value', implode(' ', $values)); $field->set('extra', $extra); return true; }