| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Nette Forms custom control example. |
| 5 |
*/ |
| 6 |
declare (strict_types=1); |
| 7 |
namespace Packetery; |
| 8 |
|
| 9 |
if (@(!(include __DIR__ . '/../vendor/autoload.php'))) { |
| 10 |
die('Install packages using `composer install`'); |
| 11 |
} |
| 12 |
use Packetery\Nette\Forms\Form; |
| 13 |
use Packetery\Nette\Forms\Helpers; |
| 14 |
use Packetery\Nette\Utils\Html; |
| 15 |
class DateInput extends \Packetery\Nette\Forms\Controls\BaseControl |
| 16 |
{ |
| 17 |
/** @var string */ |
| 18 |
private $day = ''; |
| 19 |
private $month = ''; |
| 20 |
private $year = ''; |
| 21 |
public function __construct($label = null) |
| 22 |
{ |
| 23 |
parent::__construct($label); |
| 24 |
$this->addRule([self::class, 'validateDate'], 'Date is invalid.'); |
| 25 |
} |
| 26 |
public function setValue($value) |
| 27 |
{ |
| 28 |
if ($value === null) { |
| 29 |
$this->day = $this->month = $this->year = ''; |
| 30 |
} else { |
| 31 |
$date = \Packetery\Nette\Utils\DateTime::from($value); |
| 32 |
$this->day = $date->format('j'); |
| 33 |
$this->month = $date->format('n'); |
| 34 |
$this->year = $date->format('Y'); |
| 35 |
} |
| 36 |
return $this; |
| 37 |
} |
| 38 |
public function getValue() : ?\DateTimeImmutable |
| 39 |
{ |
| 40 |
return self::validateDate($this) ? (new \DateTimeImmutable())->setDate((int) $this->year, (int) $this->month, (int) $this->day)->setTime(0, 0) : null; |
| 41 |
} |
| 42 |
public function isFilled() : bool |
| 43 |
{ |
| 44 |
return $this->day !== '' || $this->year !== ''; |
| 45 |
} |
| 46 |
public function loadHttpData() : void |
| 47 |
{ |
| 48 |
$this->day = $this->getHttpData(Form::DATA_LINE, '[day]'); |
| 49 |
$this->month = $this->getHttpData(Form::DATA_LINE, '[month]'); |
| 50 |
$this->year = $this->getHttpData(Form::DATA_LINE, '[year]'); |
| 51 |
} |
| 52 |
/** |
| 53 |
* Generates control's HTML element. |
| 54 |
*/ |
| 55 |
public function getControl() |
| 56 |
{ |
| 57 |
$name = $this->getHtmlName(); |
| 58 |
return Html::el('input', ['name' => $name . '[day]', 'id' => $this->getHtmlId(), 'value' => $this->day, 'type' => 'number', 'min' => 1, 'max' => 31, 'data-nette-rules' => Helpers::exportRules($this->getRules()) ?: null]) . Helpers::createSelectBox([1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [], $this->month)->name($name . '[month]') . Html::el('input', ['name' => $name . '[year]', 'value' => $this->year, 'type' => 'number']); |
| 59 |
} |
| 60 |
public static function validateDate(\Packetery\Nette\Forms\Control $control) : bool |
| 61 |
{ |
| 62 |
return \ctype_digit($control->day) && \ctype_digit($control->month) && \ctype_digit($control->year) && \checkdate((int) $control->month, (int) $control->day, (int) $control->year); |
| 63 |
} |
| 64 |
} |
| 65 |
Tracy\Debugger::enable(); |
| 66 |
$form = new Form(); |
| 67 |
$form['date'] = new DateInput('Date:'); |
| 68 |
$form['date']->setDefaultValue(new \DateTime()); |
| 69 |
$form->addSubmit('submit', 'Send'); |
| 70 |
if ($form->isSuccess()) { |
| 71 |
echo '<h2>Form was submitted and successfully validated</h2>'; |
| 72 |
Tracy\Dumper::dump($form->getValues()); |
| 73 |
exit; |
| 74 |
} |
| 75 |
?> |
| 76 |
<!DOCTYPE html> |
| 77 |
<meta charset="utf-8"> |
| 78 |
<title>Nette Forms custom control example</title> |
| 79 |
<link rel="stylesheet" media="screen" href="assets/style.css" /> |
| 80 |
<script src="https://nette.github.io/resources/js/3/netteForms.js"></script> |
| 81 |
|
| 82 |
<h1>Nette Forms custom control example</h1> |
| 83 |
|
| 84 |
<?php |
| 85 |
$form->render(); |
| 86 |
?> |
| 87 |
|
| 88 |
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer> |
| 89 |
<?php |
| 90 |
|