achse/nette-date-time-input

My custom DateTime Input implementation for Nette Forms.

4.0.0 2018-05-15 17:12 UTC

README

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 IDateTimeImmutableConverter|string $dateConverterOrFormat
 * @return DateTimeImmutableInput
 */
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:

  • IDateTimeImmutableConverter - responsible for conversion from string to DateTime object and vice versa.
  • IDateTimeImmutableFixer - 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 DateTimeImmutable::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. DateTimeImmutable::createFromFormat creates DateTimeImmutable 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: SimpleDateTimeImmutableConverter::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) IDateTimeImmutableConverter 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.