Upgrading

Upgrading to Version 4.0

Nette Forms 4.0 requires PHP 8.4 and nette/utils 4.1. If you render forms with Latte, you need Latte 3.1.5 or newer.

New Features

  • Meet the new Repeater, a container with a dynamic number of items that the user can add and remove directly in the browser, without anything being sent to the server.
  • Validation rules are objects, such as new Validation\LengthValidator(min: 3), and you can write your own just as easily.
  • Every error message has a stable identifier, such as forms.length.min, so translations no longer have to depend on the English text.
  • New controls addUrl() and addPhone(), and suggestions in a text field via setSuggestions().
  • Mapping to classes supports readonly classes and reports a value that has nowhere to go.

Changes in Behavior

Your forms keep working, but some of them may behave differently:

  • The maximum length set by setMaxLength() is now checked on the server too, so a longer text makes the form invalid.
  • Adding a rule that the control already has, with the same argument, replaces it instead of adding a second one. That's how you change the message of a rule added by addEmail() and similar methods.
  • The MinLength, MaxLength and Length rules on CheckboxList, MultiSelectBox and multiple upload count the selected items, and their messages speak of items, not characters.
  • addMultiUpload() always returns an array, empty if nothing was uploaded, and setNullable() can no longer be used with it.
  • addFloat() renders a text field with inputmode="decimal" instead of <input type="number">, so that a decimal comma works in every browser. If you want the number type, set it with setHtmlType('number') together with setHtmlAttribute('step', 'any').
  • The arguments of the built-in rules are checked as soon as the rule is added, so for example Form::Range without two bounds throws an exception right away, not only when the form is submitted.
  • When mapping to a class, a value with no property or constructor parameter to go to throws an exception, as does a required parameter without a value.
  • The Form::initialize() method was removed. The form creates the HTTP request by itself; if you need to pass your own, use setSubmissionSource().

Error Messages and Translations

  • The translator receives the default messages the same way as before, that is the English text of version 3 and the number, so existing translations work unchanged. You can now translate them by stable identifiers, see Translating. Without a translator, the English text of a few messages has changed (images, the type of an uploaded file, a length with equal bounds, the number of selected items).
  • The Nette\Forms\Validator::$messages array is empty by default and contains only the texts you override. Your overrides work as before.
  • The SelectBox::Valid and UploadControl::Valid constants now contain message identifiers (forms.selectBox, forms.upload) instead of the former strings; this matters only if you used their values literally.

JavaScript

  • The script is now distributed only as the npm package nette-forms; the Composer package no longer contains the src/assets/netteForms.js file. Load it from the CDN as nette-forms@4, or install it via npm, see JavaScript Validation.
  • The package defines its exports, so it can only be imported as nette-forms, not by a path to a file inside it.
  • The Nette.webalize() function was removed.

Latte

  • The {formContainer} and {formContext} tags are deprecated, use {form scope name} instead.
  • The {formPrint} and {formClassPrint} tags are deprecated, call Nette\Forms\Blueprint::latte($form) and Nette\Forms\Blueprint::dataClass($form) instead.
  • Arguments of the {input} and {label} tags are separated from the name by a comma: {input name, class: foo}. The form without a comma is deprecated.

Removed and Deprecated

  • The CsrfProtection class was removed together with the constants related to it. Protection against CSRF is built into the form, see Vulnerability Protection.
  • The IControl, IFormRenderer and ISubmitterControl aliases were removed, use Control, FormRenderer and SubmitterControl.
  • The uppercase constants such as Form::FILLED now report a deprecation, use Form::Filled etc. The same applies to the methods deprecated in 3.x versions: addImage(), setAttribute(), setType(), getSeparatorPrototype(), addOptionAttributes(), getUnsafeValues() and addProtection().
  • The Validator::validate*() methods are deprecated, use the validator objects from the Nette\Forms\Validation namespace.

For Authors of Custom Controls and Renderers

Submitted data now travels to the controls layer by layer through the containers, which is exactly the mechanism that made Repeater possible. Nothing changes for ordinary form usage; the change concerns exclusively authors of custom form controls and low-level data reading:

  • The Form::getHttpData() method is deprecated. All submitted data is returned by getSubmittedData() (beware, raw and unsanitized) and reading individual values is shown in the low-level fields chapter.
  • The protected BaseControl::getHttpData() method for custom control authors is deprecated, the replacement is getSubmittedValue(). The optional second parameter is no longer a part of the HTML name, but a key:
// 3.x
$value = $this->getHttpData(Form::DataLine);
$day = $this->getHttpData(Form::DataLine, '[day]');

// 4.0
$value = $this->getSubmittedValue(Form::DataLine);
$day = $this->getSubmittedValue(Form::DataLine, 'day');
  • The Form::DataKeys constant was renamed to Form::DataArray (the old name remains as a deprecated alias) and a new Form::DataList constant takes over the former meaning of an HTML name ending with []: it returns an array of values renumbered from zero.
  • The Helpers::extractHttpData() method is deprecated, sanitization of raw data is now provided by Helpers::sanitizeData().
  • The value of a control is read using the component name. An overridden HTML name attribute works as in 3.x, i.e. as an absolute pointer into the submitted data. Custom controls that overrode the getHtmlName() method itself instead of the attribute must newly adjust the data reading as well.

The deprecated methods still work and report a deprecation notice when called. The getSubmittedValue() method as well as its deprecated predecessor getHttpData() on a control work correctly even inside a Repeater.

Other changes for those who extend the library:

  • The Control interface declares everything the framework calls on a control, such as loadHttpData(), isFilled(), getRules(), getControl() or getLabel(). A class that implements the interface directly must add the missing methods; descendants of BaseControl have them.
  • Native types were added throughout the library, so methods you override need matching signatures.
  • Helpers::exportRules() is deprecated, use ClientRuleExporter::export().
  • When a rule is added, TextInput, TextArea and UploadControl no longer write the type, maxlength, min, max, pattern or accept attributes into getControlPrototype(); they are derived from the rules when the control is rendered.
  • Rule is a readonly object whose properties are set in the constructor, and a filter added by addFilter() is represented by a Validation\CallbackFilter object instead of a closure.
  • Container::addComponent() accepts only form controls and containers.
  • The BaseControl::$disabled property is a bool; choice controls keep their disabled items separately.

The complete list of changes, including those affecting only rare cases, can be found in the upgrading guide in the repository.

Upgrading to Version 3.3

  • the automatic CSRF protection switched from isSameSite() to isFrom(FetchSite::SameOrigin) and became stricter: requests coming from subdomains no longer pass
  • thanks to that, addProtection() is no longer needed, because the automatic protection covers the same cases; omit it in new forms and feel free to remove it from the existing ones

Why tokens in the session are no longer necessary is explained in the article Quarter Century of CSRF.

Upgrading to Version 3.1

  • getValues() returns only the validated controls; if you need the values of all controls regardless of validation, use the new method getUntrustedValues()
  • the $values passed to the onSuccess and onClick handlers likewise contain only the validated controls
  • standalone forms are automatically protected against CSRF by a cookie with the SameSite flag; you can allow submission from another origin using allowCrossOrigin()
  • the Form::URL rule now completes a missing protocol with https instead of http
  • Form::addImage() was renamed to addImageButton()
  • Checkbox::getSeparatorPrototype() was renamed to getContainerPrototype()
  • forms no longer create the $_form variable in templates

More about these changes in the article News in Nette Forms 3.1.

Upgrading to Version 3.0

  • all form controls are optional by default now (this change was introduced in Nette 2.4), so you can remove setRequired(false)
  • be sure to update netteForms.js to version 3 (npm install nette-forms)
  • ChoiceControl::$checkAllowedValues and MultiChoiceControl::$checkAllowedValues have been replaced by the method checkDefaultValue()

Upgrading to Version 2.4

  • if a control has a rule via addRule() (i.e. it is effectively mandatory), you must also mark it as mandatory via setRequired(); also, setRequired(false) now makes the control optional, which replaces addCondition($form::FILLED) branches
  • the validators Form::EMAIL, URL and INTEGER automatically change the HTML type attribute to email, url, and number respectively
  • negative validation rules are deprecated; the alternative for ~Form::FILLED is Form::BLANK, and ~Form::EQUAL can be replaced with Form::NOT_EQUAL
  • the internal parameter do is now sent via POST as _do to avoid a collision
  • the internal underscored variables such as $_form are deprecated
  • remember to update netteForms.js

Upgrading to Version 2.3

  • internal filtering methods such as Nette\Forms\Controls\TextBase::filterFloat were removed
  • internal validation methods such as TextBase::validateFloat were moved to Nette\Forms\Validator, as was Rules::$defaultMessages
  • Buttons and Hidden fields are generated without an HTML ID; if you want an ID, set it via setHtmlId()
  • RadioList items are generated without an ID too; you can enable it via $radioList->generateId = true
  • filters added via TextBase::addFilter() are processed during validation, and you can now add filters to conditions: $input->addCondition(...)->addFilter(...)
version: 4.x 3.x