Economy, containers and other hot news in Nette Forms
An overview of the most important news in the nette/forms 3.0.x package.
Economic checkbox lists
The CheckboxList sent by the GET method is not transmitted economically, it wastes characters and creates a long line in the URL
?filter[]=val1&filter[]=val2&filter[]=val3&filter[]=val4&filter[]=val5
The new feature is a shorter and more readable format like this:
?filter=val1,val2,val3,val4,val5
The format change is handled by the script netteForms.js
, which
don't forget to update as well. This feature runs in test mode and must be
activated using the data attribute:
$form = new Form;
$form->setMethod('GET');
$form->setHtmlAttribute('data-nette-compact');
Write to us in the comments how you like the simplified format.
Macro {formPrint}
Another novelty is the macro {formPrint [form-name]}
, which will
simplifie your work when you need to play with the output design of the form.
The macro switches the output to a special mode in which the Latte code of form
is generated instead of a page. It can be easily selected, copied to the
clipboard and pasted into a template, where you can edit it as needed.
The output might look like this:
<form n:name="signInForm">
<ul class="error" n:ifcontent>
<li n:foreach="$form->getOwnErrors() as $error">{$error}</li>
</ul>
<table>
<tr class="required">
<th>{label username/}</th>
<td>{input username} <span class="error" n:ifcontent>{inputError username}</span></td>
</tr>
<tr class="required">
<th>{label password/}</th>
<td>{input password} <span class="error" n:ifcontent>{inputError password}</span></td>
</tr>
<tr>
<th></th>
<td>{input send}</td>
</tr>
</table>
</form>
All settings of the renderer via
$form->getRenderer()->wrappers
are respected.
Mapping containers to classes in PHP 7.4
Do you use form containers, class mapping and PHP 7.4? Then you don't have to
call setMappedType()
for containers, just define the
property type:
class FormData
{
public string $title;
public AddressFormData $address;
}
class AddressFormData
{
public string $street;
public string $city;
public string $zip;
}
$form = new Form;
$address = $form->addContainer('address');
$address->addText('street');
$address->addText('city');
$address->addText('zip');
$form->onSuccess[] = function ($form, FormData $data) {
// $data->address is instance of AddressFormData
$street = $data->address->street;
}
If you receive data as an array function ($form, array $data)
,
then the $data['address']
will also be array.
More news
- fields
addHidden()
have setNullable() and addFilter() methods, as well as text inputs. - field
addUpload()
automatically sets the ruleMAX_FILE_SIZE
to the maximum allowed size of the uploaded file according to the PHP configurationupload_max_filesize
, so the attempt to upload a larger file is stopped already on the browser side by the netteForms.js script. - netteForms.js also checks the MIME-TYPE rule on the browser side. This feature was turned off in the past due to a bug in Firefox.
- netteForms.js also accepts image/webp as image
Sign in to submit a comment