diff --git a/.travis.yml b/.travis.yml index 925312e61..cfff85217 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,45 +3,62 @@ php: - 5.6 - 7.0 - 7.1 + - 7.2 -env: -matrix: - include: - - php: 5.6 - env: dependencies="--prefer-lowest --prefer-stable" - - php: 7.0 - env: coverage=on +before_install: + # turn off XDebug + - phpenv config-rm xdebug.ini || return 0 - allow_failures: - - php: 7.0 - env: coverage=on +install: + - travis_retry composer install --no-progress --prefer-dist script: - - vendor/bin/tester tests -s -c tests/php-unix.ini $coverageArgs - - php temp/code-checker/src/code-checker.php --short-arrays -i netteForms - - if [ $TRAVIS_PHP_VERSION == "7.0" ]; then grunt --gruntfile=tests/netteForms/Gruntfile.js test; fi + - vendor/bin/tester tests -s after_failure: # Print *.actual content - for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done -before_script: - # Install Nette Tester & Code Checker - - travis_retry composer update --no-interaction --prefer-dist $dependencies - - travis_retry composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction - - if [ $TRAVIS_PHP_VERSION == "7.0" ]; then npm install -g grunt-cli; cd tests/netteForms; npm install; cd ../..; fi - - if [ "$coverage" == "on" ]; then coverageArgs="-p phpdbg --coverage ./coverage.xml --coverage-src ./src"; fi - -after_script: - # Report Code Coverage - - > - if [ "$coverage" == "on" ]; then - wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar - && php coveralls.phar --verbose --config tests/.coveralls.yml - || true; fi +jobs: + include: + - env: title="Lowest Dependencies" + install: + - travis_retry composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable + + + - stage: Code Standard Checker + php: 7.1 + install: + # Install Nette Code Checker + - travis_retry composer create-project nette/code-checker temp/code-checker ~2 --no-progress + # Install Nette Coding Standard + - travis_retry composer create-project nette/coding-standard temp/coding-standard --no-progress + # Install Grunt + - npm install -g grunt-cli; cd tests/netteForms; npm install; cd ../.. + script: + - php temp/code-checker/src/code-checker.php --short-arrays + - php temp/coding-standard/ecs check src tests examples --config temp/coding-standard/coding-standard-php56.neon + - grunt --gruntfile=tests/netteForms/Gruntfile.js test + + + - stage: Code Coverage + php: 7.1 + script: + - vendor/bin/tester -p phpdbg tests -s --coverage ./coverage.xml --coverage-src ./src + after_script: + - wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar + - php coveralls.phar --verbose --config tests/.coveralls.yml + + + allow_failures: + - stage: Code Coverage + sudo: false cache: directories: - $HOME/.composer/cache + +notifications: + email: false diff --git a/composer.json b/composer.json index b19cfbbf3..630ef929d 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "nette/forms", - "description": "Nette Forms: greatly facilitates web forms", + "description": "?? Nette Forms: generating, validating and processing secure forms in PHP. Handy API, fully customizable, server & client side validation and mature design.", + "keywords": ["nette", "forms", "validation", "csrf", "javascript", "bootstrap"], "homepage": "https://nette.org", "license": ["BSD-3-Clause", "GPL-2.0", "GPL-3.0"], "authors": [ diff --git a/src/Bridges/FormsLatte/FormMacros.php b/src/Bridges/FormsLatte/FormMacros.php index 789b7bc61..20186dd59 100644 --- a/src/Bridges/FormsLatte/FormMacros.php +++ b/src/Bridges/FormsLatte/FormMacros.php @@ -163,6 +163,11 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer) $tagName = strtolower($node->htmlNode->name); $node->empty = $tagName === 'input'; + $definedHtmlAttributes = array_keys($node->htmlNode->attrs); + if (isset($node->htmlNode->macroAttrs['class'])) { + $definedHtmlAttributes[] = 'class'; + } + if ($tagName === 'form') { $node->openingCode = $writer->write( 'global->formsStack[] = ' @@ -172,17 +177,17 @@ public function macroNameAttr(MacroNode $node, PhpWriter $writer) ); return $writer->write( 'echo Nette\Bridges\FormsLatte\Runtime::renderFormBegin(end($this->global->formsStack), %0.var, false)', - array_fill_keys(array_keys($node->htmlNode->attrs), null) + array_fill_keys($definedHtmlAttributes, null) ); } else { $method = $tagName === 'label' ? 'getLabel' : 'getControl'; return $writer->write( '$_input = ' . ($name[0] === '$' ? 'is_object(%0.word) ? %0.word : ' : '') . 'end($this->global->formsStack)[%0.word]; echo $_input->%1.raw' - . ($node->htmlNode->attrs ? '->addAttributes(%2.var)' : '') . '->attributes()', + . ($definedHtmlAttributes ? '->addAttributes(%2.var)' : '') . '->attributes()', $name, $method . 'Part(' . implode(', ', array_map([$writer, 'formatWord'], $words)) . ')', - array_fill_keys(array_keys($node->htmlNode->attrs), null) + array_fill_keys($definedHtmlAttributes, null) ); } } diff --git a/src/Forms/Controls/BaseControl.php b/src/Forms/Controls/BaseControl.php index 145db0a46..ace5d7eeb 100644 --- a/src/Forms/Controls/BaseControl.php +++ b/src/Forms/Controls/BaseControl.php @@ -504,9 +504,9 @@ public function validate() * @param string|object * @return void */ - public function addError($message) + public function addError($message, $translate = true) { - $this->errors[] = $message; + $this->errors[] = $translate ? $this->translate($message) : $message; } diff --git a/src/Forms/Controls/HiddenField.php b/src/Forms/Controls/HiddenField.php index fd023026d..f9c75dacf 100644 --- a/src/Forms/Controls/HiddenField.php +++ b/src/Forms/Controls/HiddenField.php @@ -81,8 +81,8 @@ public function getLabel($caption = null) * @param string|object * @return void */ - public function addError($message) + public function addError($message, $translate = true) { - $this->getForm()->addError($message); + $this->getForm()->addError($message, $translate); } } diff --git a/src/Forms/Form.php b/src/Forms/Form.php index c75aee89b..a2044c0c0 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -465,6 +465,18 @@ private function invokeHandlers($handlers, $button = null) } + /** + * Resets form. + * @return static + */ + public function reset() + { + $this->setSubmittedBy(null); + $this->setValues([], true); + return $this; + } + + /** * Internal: returns submitted HTTP data or null when form was not submitted. * @return array|null @@ -534,8 +546,11 @@ public function validateMaxPostSize() * @param string|object * @return void */ - public function addError($message) + public function addError($message, $translate = true) { + if ($translate && $this->translator) { + $message = $this->translator->translate($message); + } $this->errors[] = $message; } diff --git a/src/Forms/Rendering/DefaultFormRenderer.php b/src/Forms/Rendering/DefaultFormRenderer.php index e6f68aa99..3c0f19fca 100644 --- a/src/Forms/Rendering/DefaultFormRenderer.php +++ b/src/Forms/Rendering/DefaultFormRenderer.php @@ -214,10 +214,6 @@ public function renderEnd() */ public function renderErrors(Nette\Forms\IControl $control = null, $own = true) { - $translator = $control - ? ($control instanceof \Nette\Forms\Controls\BaseControl ? $control->getTranslator() : null) - : $this->form->getTranslator(); - $errors = $control ? $control->getErrors() : ($own ? $this->form->getOwnErrors() : $this->form->getErrors()); @@ -232,7 +228,7 @@ public function renderErrors(Nette\Forms\IControl $control = null, $own = true) if ($error instanceof IHtmlString) { $item->addHtml($error); } else { - $item->setText($translator ? $translator->translate($error) : $error); + $item->setText($error); } $container->addHtml($item); } diff --git a/src/Forms/Rules.php b/src/Forms/Rules.php index e8cf2d6af..8a2d99133 100644 --- a/src/Forms/Rules.php +++ b/src/Forms/Rules.php @@ -246,7 +246,7 @@ public function validate($emptyOptional = false) return false; } elseif (!$success && !$rule->branch) { - $rule->control->addError(Validator::formatMessage($rule, true)); + $rule->control->addError(Validator::formatMessage($rule, true), false); return false; } } diff --git a/tests/Forms.Latte/FormMacros.forms.phpt b/tests/Forms.Latte/FormMacros.forms.phpt index a7184d4df..c696ffde2 100644 --- a/tests/Forms.Latte/FormMacros.forms.phpt +++ b/tests/Forms.Latte/FormMacros.forms.phpt @@ -28,8 +28,10 @@ class MyControl extends Nette\Forms\Controls\BaseControl $form = new Form; +$form->getElementPrototype()->addClass('form-class'); $form->addHidden('id'); $form->addText('username', 'Username:'); // must have just one textfield to generate IE fix +$form['username']->getControlPrototype()->addClass('control-class'); $form->addRadioList('sex', 'Sex:', ['m' => 'male', 'f' => 'female']); $form->addSelect('select', null, ['m' => 'male', 'f' => 'female']); $form->addTextArea('area', null)->setValue('one +
@@ -9,13 +9,13 @@ - + error error
- + error @@ -52,7 +52,7 @@ - + @@ -60,11 +60,11 @@ - +
-
+ @@ -91,13 +91,19 @@ - +
-
- + + + +
+ + +
+
diff --git a/tests/Forms.Latte/expected/FormMacros.forms.phtml b/tests/Forms.Latte/expected/FormMacros.forms.phtml index 7f59824b9..46737a98a 100644 --- a/tests/Forms.Latte/expected/FormMacros.forms.phtml +++ b/tests/Forms.Latte/expected/FormMacros.forms.phtml @@ -202,6 +202,24 @@ ?> +global->formsStack[] = $this->global->uiControl["myForm"]; + ?>global->formsStack), array ( + 'class' => NULL, + ), false) ?>> + global->formsStack)["username"]; + echo $_input->getControlPart()->addAttributes(array ( + 'class' => NULL, + ))->attributes(); + if ($_tmp = array_filter(['nclass'])) echo ' class="', LR\Filters::escapeHtmlAttr(implode(" ", array_unique($_tmp))), '"' ?>> +global->formsStack), false); +?> + + global->formsStack[] = is_object($this->global->uiControl['myForm']) ? $this->global->uiControl['myForm'] : $this->global->uiControl[$this->global->uiControl['myForm']]; ?>getControlPart()->attributes() ?>>getControl()->getHtml() ?> -