-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathcustom-control.php
More file actions
150 lines (113 loc) · 3.03 KB
/
custom-control.php
File metadata and controls
150 lines (113 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/**
* Nette Forms custom control example.
*/
declare(strict_types=1);
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
use Nette\Forms\Form;
use Nette\Forms\Helpers;
use Nette\Utils\Html;
class DateInput extends Nette\Forms\Controls\BaseControl
{
/** @var string */
private $day = '';
private $month = '';
private $year = '';
public function __construct($label = null)
{
parent::__construct($label);
$this->setRequired(false)
->addRule([__CLASS__, 'validateDate'], 'Date is invalid.');
}
public function setCurrentValue($value)
{
if ($value === null) {
$this->day = $this->month = $this->year = '';
} else {
$date = Nette\Utils\DateTime::from($value);
$this->day = $date->format('j');
$this->month = $date->format('n');
$this->year = $date->format('Y');
}
return $this;
}
/**
* @return DateTimeImmutable|null
*/
public function getValue()
{
return self::validateDate($this)
? (new DateTimeImmutable)->setDate((int) $this->year, (int) $this->month, (int) $this->day)->setTime(0, 0)
: null;
}
/**
* @return bool
*/
public function isFilled(): bool
{
return $this->day !== '' || $this->year !== '';
}
public function loadHttpData(): void
{
$this->day = $this->getHttpData(Form::DATA_LINE, '[day]');
$this->month = $this->getHttpData(Form::DATA_LINE, '[month]');
$this->year = $this->getHttpData(Form::DATA_LINE, '[year]');
}
/**
* Generates control's HTML element.
*/
public function getControl()
{
$name = $this->getHtmlName();
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',
]);
}
/**
* @return bool
*/
public static function validateDate(Nette\Forms\IControl $control)
{
return ctype_digit($control->day)
&& ctype_digit($control->month)
&& ctype_digit($control->year)
&& checkdate((int) $control->month, (int) $control->day, (int) $control->year);
}
}
Tracy\Debugger::enable();
$form = new Form;
$form['date'] = new DateInput('Date:');
$form['date']->setDefaultValue(new DateTime);
$form->addSubmit('submit', 'Send');
if ($form->isSuccess()) {
echo '<h2>Form was submitted and successfully validated</h2>';
Tracy\Dumper::dump($form->getValues());
exit;
}
?>
<!DOCTYPE html>
<meta charset="utf-8">
<title>Nette Forms custom control example</title>
<link rel="stylesheet" media="screen" href="assets/style.css" />
<script src="https://nette.github.io/resources/js/netteForms.js"></script>
<h1>Nette Forms custom control example</h1>
<?php echo $form ?>
<footer><a href="https://doc.nette.org/en/forms">see documentation</a></footer>