Statistics
25539
2
0
1.2y
Badges
Tags
forms
Dependencies
Keywords
datetime

Downloads this Month Latest Stable Version Build Status Scrutinizer Code Quality Coverage Status

Installation

composer require achse/nette-date-time-input

Usage

Basic usage is to add into your BaseForm or FormElements trait code like this:

/**
 * @param string $name
 * @param string|NULL $label
 * @param IDateTimeConverter|string $dateConverterOrFormat
 * @return DateTimeInput
 */
public function addDate($name, $label = NULL, $dateConverterOrFormat = 'j. n. Y')
{
	return $this[$name] = DateTimeInputFactory::create($label, $dateConverterOrFormat);
}

Validation

  • You can use any addRule(Form::XXX like you are used to do for any other input.
  • Rules Form::VALID and Form::FILLED are reimplemented to behave according to given DateTime format.

How does it work?

It creates simple text input. Handling client side is fully up to you. (For example: https://eonasdan.github.io/bootstrap-datetimepicker/)

As result it returns DateTime object. Internally it use:

  • IDateTimeConverter - responsible for conversion from string to DateTime object and vice versa.
  • IDateTimeFixer - responsible for removing PHP specific behavior like this:

You can provide both of them as service. If not specified, single new object is created for each input.

Default converter: SimpleDateTimeConverter and what "safe symbols" means?

In PHP, method DateTime::createFromFormat has this really unexpected behavior:

Therefore SimpleDateTimeConverter prevents you from being affected by this "language feature".

It works line this:

  1. It strips all duplicate whitespaces and trims the string,
  2. DateTime::createFromFormat creates DateTime object,
  3. object is formatted by original patter back to string,
  4. string is compared, if is same as input string.

(For more you can see: SimpleDateTimeConverter::parseValue method.)

But, there is a leading zero problem.

  • You insert: 1. 1. 2015 with pattern d. m. Y,
  • algorithm creates object and ties to compare it with original,
  • but by pattern created: 01. 01. 2015 !== (original) 1. 1. 2015.

Because of this, it's strongly recommended to use only no-leading-zero formats in your datepicker.

Contribution

  • If you wrote better (alternative) IDateTimeConverter send me pull request or just send me email. I'll be really happy to integrate it into package.
  • If you have ANY suggestion or idea how to make it better I'll be happy for every opened issue.
  • 3.2.0 3.2.0

    • DateTimeInputFactory added.
    • Fixes in naming (no BC breaks).
  • 3.1.0 3.1.0

    • Tools extracted to separate package.
  • 3.0.0 3.0.0

    • BC Break due renaming
  • 2.0.0 2.0.0

    • When new datetime object is created from string by pattern, default values are used instead of "now" (php default behavior).
    • Improved tests.
    • Documentation.

Is this addon outdated? Did you find an issue? Please report it.

Componette Componette admin@componette.com