Skip to content

Commit ac902b1

Browse files
committed
examples: improved custom-control.php
1 parent ffcbb56 commit ac902b1

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

examples/custom-control.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Nette\Forms\Form;
1313
use Nette\Utils\Html;
14+
use Nette\Forms\Helpers;
1415

1516

1617
class DateInput extends Nette\Forms\Controls\BaseControl
@@ -25,7 +26,8 @@ class DateInput extends Nette\Forms\Controls\BaseControl
2526
public function __construct($label = NULL)
2627
{
2728
parent::__construct($label);
28-
$this->addRule(__CLASS__ . '::validateDate', 'Date is invalid.');
29+
$this->addCondition(Form::FILLED)
30+
->addRule(__CLASS__ . '::validateDate', 'Date is invalid.');
2931
}
3032

3133

@@ -77,13 +79,26 @@ public function loadHttpData()
7779
public function getControl()
7880
{
7981
$name = $this->getHtmlName();
80-
return Html::el()
81-
->add(Html::el('input')->name($name . '[day]')->id($this->getHtmlId())->value($this->day))
82-
->add(Nette\Forms\Helpers::createSelectBox(
83-
[1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
84-
['selected?' => $this->month]
85-
)->name($name . '[month]'))
86-
->add(Html::el('input')->name($name . '[year]')->value($this->year));
82+
return Html::el('input', [
83+
'name' => $name . '[day]',
84+
'id' => $this->getHtmlId(),
85+
'value' => $this->day,
86+
'type' => 'number',
87+
'min' => 1,
88+
'max' => 31,
89+
'data-nette-rules' => Helpers::exportRules($this->getRules()) ?: NULL,
90+
])
91+
92+
. Helpers::createSelectBox(
93+
[1 => 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
94+
['selected?' => $this->month]
95+
)->name($name . '[month]')
96+
97+
. Html::el('input', [
98+
'name' => $name . '[year]',
99+
'value' => $this->year,
100+
'type' => 'number',
101+
]);
87102
}
88103

89104

@@ -123,6 +138,7 @@ public static function validateDate(Nette\Forms\IControl $control)
123138
<meta charset="utf-8">
124139
<title>Nette Forms custom control example</title>
125140
<link rel="stylesheet" media="screen" href="assets/style.css" />
141+
<script src="https://nette.github.io/resources/js/netteForms.js"></script>
126142

127143
<h1>Nette Forms custom control example</h1>
128144

0 commit comments

Comments
 (0)