Skip to content

📆 My custom DateTime Input implemeentation for Nette Forms.

License

Notifications You must be signed in to change notification settings

vvoody-/nette-date-time-input

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

📆 My custom DateTime Input implemeentation for Nette Forms.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%