diff --git a/examples/assets/style.css b/examples/assets/style.css index 6778af646..e31f88bc6 100644 --- a/examples/assets/style.css +++ b/examples/assets/style.css @@ -27,13 +27,21 @@ fieldset { border: 1px solid #B2D1EB; } -textarea, select, input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) { +textarea, +select, +input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) { padding: .3em .5em; color: black; background: white; border: 1px solid silver; } +.has-error textarea, +.has-error select, +.has-error input:not([type="checkbox"]):not([type="radio"]):not([type="submit"]):not([type="image"]):not([type="range"]) { + border-color: #E22; +} + select { padding-right: .3em; } @@ -55,6 +63,7 @@ th { .error { color: #E22; font-weight: bold; + margin-left: 1em; } footer a { diff --git a/examples/basic-example.php b/examples/basic-example.php index 08c6b0949..88b5f970e 100644 --- a/examples/basic-example.php +++ b/examples/basic-example.php @@ -42,10 +42,8 @@ 'b' => 'blue', ]); -$form->addText('email', 'Email:') - ->setEmptyValue('@') - ->addCondition($form::FILLED) // conditional rule: if is email filled, ... - ->addRule($form::EMAIL, 'Incorrect email address'); // ... then check email +$form->addEmail('email', 'Email:') + ->setEmptyValue('@'); // group Shipping address @@ -93,8 +91,8 @@ ->addRule($form::EQUAL, 'Passwords do not match', $form['password']); $form->addUpload('avatar', 'Picture:') - ->addCondition($form::FILLED) - ->addRule($form::IMAGE, 'Uploaded file is not image'); + ->setRequired(FALSE) + ->addRule($form::IMAGE, 'Uploaded file is not image'); $form->addHidden('userid'); @@ -114,7 +112,7 @@ if ($form->isSuccess()) { echo '

Form was submitted and successfully validated

'; - Dumper::dump($form->getValues()); + Dumper::dump($form->getValues(), [Dumper::COLLAPSE => FALSE]); exit; } diff --git a/examples/bootstrap2-rendering.php b/examples/bootstrap2-rendering.php index 260580bd8..0e400a963 100644 --- a/examples/bootstrap2-rendering.php +++ b/examples/bootstrap2-rendering.php @@ -74,7 +74,7 @@ $usedPrimary = TRUE; } elseif (in_array($type, ['checkbox', 'radio'], TRUE)) { - $control->getLabelPrototype()->addClass($control->getControlPrototype()->type); + $control->getLabelPrototype()->addClass($type); $control->getSeparatorPrototype()->setName(NULL); } } diff --git a/examples/bootstrap3-rendering.php b/examples/bootstrap3-rendering.php index 1ef3de903..039b74db0 100644 --- a/examples/bootstrap3-rendering.php +++ b/examples/bootstrap3-rendering.php @@ -77,7 +77,7 @@ $control->getControlPrototype()->addClass('form-control'); } elseif (in_array($type, ['checkbox', 'radio'], TRUE)) { - $control->getSeparatorPrototype()->setName('div')->addClass($control->getControlPrototype()->type); + $control->getSeparatorPrototype()->setName('div')->addClass($type); } } diff --git a/examples/custom-control.php b/examples/custom-control.php index 946b84d53..9c80d555b 100644 --- a/examples/custom-control.php +++ b/examples/custom-control.php @@ -46,12 +46,12 @@ public function setValue($value) /** - * @return DateTime|NULL + * @return DateTimeImmutable|NULL */ public function getValue() { return self::validateDate($this) - ? (new DateTime)->setDate($this->year, $this->month, $this->day)->setTime(0, 0) + ? (new DateTimeImmutable)->setDate($this->year, $this->month, $this->day)->setTime(0, 0) : NULL; } diff --git a/examples/custom-rendering.php b/examples/custom-rendering.php index 225af2b8c..6b9d7bef6 100644 --- a/examples/custom-rendering.php +++ b/examples/custom-rendering.php @@ -37,8 +37,8 @@ ->setRequired('Enter your name'); $form->addRadioList('gender', 'Your gender', [ - 'm' => Html::el('option', 'male')->style('color: #248bd3'), - 'f' => Html::el('option', 'female')->style('color: #e948d4'), + 'm' => Html::el('span', 'male')->style('color: #248bd3'), + 'f' => Html::el('span', 'female')->style('color: #e948d4'), ]); $form->addSelect('country', 'Country', [ diff --git a/examples/html5.php b/examples/html5.php index f22b6eb80..828bb8c5f 100644 --- a/examples/html5.php +++ b/examples/html5.php @@ -36,12 +36,9 @@ ->addRule($form::INTEGER, 'Precision must be numeric value') ->addRule($form::RANGE, 'Precision must be in range from %d to %d', [0, 100]); -$form->addText('email', 'Send to email:') - ->setType('email') +$form->addEmail('email', 'Send to email:') ->setAttribute('autocomplete', 'off') - ->setAttribute('placeholder', 'Optional, but Recommended') - ->addCondition($form::FILLED) // conditional rule: if is email filled, ... - ->addRule($form::EMAIL, 'Incorrect email address'); // ... then check email + ->setAttribute('placeholder', 'Optional, but Recommended'); $form->addSubmit('submit', 'Send'); diff --git a/examples/live-validation.php b/examples/live-validation.php new file mode 100644 index 000000000..d66437359 --- /dev/null +++ b/examples/live-validation.php @@ -0,0 +1,104 @@ +addText('name', 'Your name:') + ->setRequired('Enter your name'); + +$form->addText('age', 'Your age:') + ->setRequired('Enter your age') + ->addRule($form::INTEGER, 'Age must be numeric value') + ->addRule($form::RANGE, 'Age must be in range from %d to %d', [10, 100]); + +$form->addPassword('password', 'Choose password:') + ->setRequired('Choose your password') + ->addRule($form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3); + +$form->addPassword('password2', 'Reenter password:') + ->setRequired('Reenter your password') + ->addRule($form::EQUAL, 'Passwords do not match', $form['password']); + +$form->addSubmit('submit', 'Send'); + + +if ($form->isSuccess()) { + echo '

Form was submitted and successfully validated

'; + Dumper::dump($form->getValues()); + exit; +} + +$renderer = $form->getRenderer(); +$renderer->wrappers['pair']['.error'] = 'has-error'; + +?> + + +Nette Forms live validation example + + + + + + +

Nette Forms live validation example

+ + + + diff --git a/examples/manual-rendering.php b/examples/manual-rendering.php index 4eac95f75..bb0596aaa 100644 --- a/examples/manual-rendering.php +++ b/examples/manual-rendering.php @@ -29,8 +29,8 @@ ]); $form->addText('email') - ->addCondition($form::FILLED) - ->addRule($form::EMAIL, 'Incorrect email address'); + ->setRequired(FALSE) + ->addRule($form::EMAIL, 'Incorrect email address'); $form->addSubmit('submit');