Skip to content

Commit 5a6ec49

Browse files
committed
form controls are auto-optional
- setRequired(false) is unnecessary - BaseControl::enableAutoOptionalMode() deprecated
1 parent 2210656 commit 5a6ec49

File tree

13 files changed

+16
-72
lines changed

13 files changed

+16
-72
lines changed

examples/basic-example.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
->addRule($form::EQUAL, 'Passwords do not match', $form['password']);
9494

9595
$form->addUpload('avatar', 'Picture:')
96-
->setRequired(false)
9796
->addRule($form::IMAGE, 'Uploaded file is not image');
9897

9998
$form->addHidden('userid');

examples/custom-control.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class DateInput extends Nette\Forms\Controls\BaseControl
2929
public function __construct($label = null)
3030
{
3131
parent::__construct($label);
32-
$this->setRequired(false)
33-
->addRule([__CLASS__, 'validateDate'], 'Date is invalid.');
32+
$this->addRule([__CLASS__, 'validateDate'], 'Date is invalid.');
3433
}
3534

3635

src/Forms/Container.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ public function addTextArea(string $name, $label = null, int $cols = null, int $
263263
public function addEmail(string $name, $label = null): Controls\TextInput
264264
{
265265
return $this[$name] = (new Controls\TextInput($label))
266-
->setRequired(false)
267266
->addRule(Form::EMAIL);
268267
}
269268

@@ -276,7 +275,6 @@ public function addInteger(string $name, $label = null): Controls\TextInput
276275
{
277276
return $this[$name] = (new Controls\TextInput($label))
278277
->setNullable()
279-
->setRequired(false)
280278
->addRule(Form::INTEGER);
281279
}
282280

src/Forms/Controls/BaseControl.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements ICo
7474
/** @var array user options */
7575
private $options = [];
7676

77-
/** @var bool */
78-
private static $autoOptional = false;
79-
8077

8178
/**
8279
* @param string|object $caption
@@ -87,9 +84,6 @@ public function __construct($caption = null)
8784
$this->label = Html::el('label');
8885
$this->caption = $caption;
8986
$this->rules = new Rules($this);
90-
if (self::$autoOptional) {
91-
$this->setRequired(false);
92-
}
9387
$this->setValue(null);
9488
$this->monitor(Form::class, function (Form $form): void {
9589
if (!$this->isDisabled() && $form->isAnchored() && $form->isSubmitted()) {
@@ -528,12 +522,11 @@ public function cleanErrors(): void
528522

529523

530524
/**
531-
* Globally enables new required/optional behavior.
532-
* This method will be deprecated in next version.
525+
* @deprecated
533526
*/
534527
public static function enableAutoOptionalMode(): void
535528
{
536-
self::$autoOptional = true;
529+
trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
537530
}
538531

539532

src/Forms/Controls/UploadControl.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ public function __construct($label = null, bool $multiple = false)
3232
$this->control->type = 'file';
3333
$this->control->multiple = $multiple;
3434
$this->setOption('type', 'file');
35-
$this->addCondition(Forms\Form::FILLED)
36-
->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
35+
$this->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
3736

3837
$this->monitor(Forms\Form::class, function (Forms\Form $form): void {
3938
if (!$form->isMethod('post')) {

src/Forms/Form.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,6 @@ protected function beforeRender()
591591
public function fireRenderEvents(): void
592592
{
593593
if (!$this->beforeRenderCalled) {
594-
foreach ($this->getComponents(true, Controls\BaseControl::class) as $control) {
595-
$control->getRules()->check();
596-
}
597594
$this->beforeRenderCalled = true;
598595
$this->beforeRender();
599596
$this->onRender($this);

src/Forms/Helpers.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,6 @@ public static function exportRules(Rules $rules): array
128128

129129
$payload[] = $item;
130130
}
131-
if ($payload && $rules->isOptional()) {
132-
array_unshift($payload, ['op' => 'optional']);
133-
}
134131
return $payload;
135132
}
136133

src/Forms/Rules.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Rules implements \IteratorAggregate
1919
{
2020
use Nette\SmartObject;
2121

22-
/** @var Rule|false|null */
22+
/** @var Rule|null */
2323
private $required;
2424

2525
/** @var Rule[] */
@@ -51,7 +51,7 @@ public function setRequired($value = true)
5151
if ($value) {
5252
$this->addRule(Form::REQUIRED, $value === true ? null : $value);
5353
} else {
54-
$this->required = false;
54+
$this->required = null;
5555
}
5656
return $this;
5757
}
@@ -66,15 +66,6 @@ public function isRequired(): bool
6666
}
6767

6868

69-
/**
70-
* @internal
71-
*/
72-
public function isOptional(): bool
73-
{
74-
return $this->required === false;
75-
}
76-
77-
7869
/**
7970
* Adds a validation rule for the current control.
8071
* @param callable|string $validator
@@ -217,7 +208,7 @@ public function getToggleStates(array $toggles = [], bool $success = true): arra
217208
*/
218209
public function validate(bool $emptyOptional = false): bool
219210
{
220-
$emptyOptional = $emptyOptional || $this->isOptional() && !$this->control->isFilled();
211+
$emptyOptional = $emptyOptional || !$this->isRequired() && !$this->control->isFilled();
221212
foreach ($this as $rule) {
222213
if (!$rule->branch && $emptyOptional && $rule->validator !== Form::FILLED) {
223214
continue;
@@ -236,30 +227,6 @@ public function validate(bool $emptyOptional = false): bool
236227
}
237228

238229

239-
/**
240-
* @internal
241-
*/
242-
public function check(): bool
243-
{
244-
if ($this->required !== null) {
245-
return false;
246-
}
247-
foreach ($this->rules as $rule) {
248-
if ($rule->control === $this->control && ($rule->validator === Form::FILLED || $rule->validator === Form::BLANK)) {
249-
// ignore
250-
} elseif ($rule->branch) {
251-
if ($rule->branch->check() === true) {
252-
return true;
253-
}
254-
} else {
255-
trigger_error("Missing setRequired(true | false) on field '{$rule->control->getName()}' in form '{$rule->control->getForm()->getName()}'.", E_USER_WARNING);
256-
return true;
257-
}
258-
}
259-
return false;
260-
}
261-
262-
263230
/**
264231
* Clear all validation rules.
265232
*/

src/assets/netteForms.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
elem = elem.tagName ? elem : elem[0]; // RadioNodeList
150150
rules = rules || JSON.parse(elem.getAttribute('data-nette-rules') || '[]');
151151
value = value === undefined ? {value: Nette.getEffectiveValue(elem)} : value;
152+
emptyOptional = emptyOptional || !Nette.validateRule(elem, ':filled', null, value);
152153

153154
for (var id = 0, len = rules.length; id < len; id++) {
154155
var rule = rules[id],
@@ -161,9 +162,6 @@
161162

162163
if (!curElem) {
163164
continue;
164-
} else if (rule.op === 'optional') {
165-
emptyOptional = !Nette.validateRule(elem, ':filled', null, value);
166-
continue;
167165
} else if (emptyOptional && !rule.condition && rule.op !== ':filled') {
168166
continue;
169167
}

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,5 @@ test(function () { // addInteger
221221

222222
Assert::null($input->getValue());
223223

224-
Assert::same('<input type="number" name="text" id="frm-text" data-nette-rules=\'[{"op":"optional"},{"op":":integer","msg":"Please enter a valid integer."}]\'>', (string) $input->getControl());
224+
Assert::same('<input type="number" name="text" id="frm-text" data-nette-rules=\'[{"op":":integer","msg":"Please enter a valid integer."}]\'>', (string) $input->getControl());
225225
});

0 commit comments

Comments
 (0)