<?php
/* https://forum.nette.org/cs/35218-muze-checkboxlist-vracet-associativni-pole-misto-indexoveho#p220072 */
0 => 'po'
1 => 'st'
2 => 'ct'
po => true
ut => false
st => true
ct => true
pa => false
array_walk($arr, fn(&$item, $key, $values) => $item = in_array($key, $values), $values);
<?php
$form->addEmail('iEmail')
->addRule(function($control) {
return $this->user->checkEmail($control->value)
});
<?php
// https://git.andweb.cz/brandcloud/app/-/blob/bcnew/app/BrandCloudModule/components/SharingForm.php#L148
$form->addSubmit('share', 'Share')
->onInvalidClick[] = function () use ($form) {
// we have to set recipients as items to revalidate recipients field
// TODO: posibly change multiselectbox to some other implementation which works only with raw value
if ($form['sharing_type']->getValue() === 'email') {
$form['recipients']->setItems((array) $form['recipients']->getRawValue(), false);
$form->validate();
}
};
$form->onValidate[] = [$this, 'validateForm'];
$form->onSubmit[] = [$this, 'submitForm'];
$form->onSuccess[] = [$this, 'successForm'];
$form->setDefaults($this->getFormDefaults());
return $form;
<?php
// https://forum.nette.org/cs/27213-dependent-form-select-with-ajax-chyba-please-select-a-valid-option
// https://forum.nette.org/cs/32545-jak-na-vicekrokovy-formular#p207046
protected function createComponentSelectForm($name)
{
$firstItems = [
1 => 'First option 1',
2 => 'First option 2'
];
// Je důležité předávat formuláři $this a $name jinak by nefungovalo níže použité $firstSelect->getValue()
$form = new \Nette\Application\UI\Form($this, $name);
$firstSelect = $form->addSelect('first', 'First select:', $firstItems)
->setPrompt('Select');
$secondItems = [0 => 'Select from first'];
if ($firstSelect->getValue()) {
$secondItems = [
0 => 'Select',
1 => 'First option ' . $value . ' - second option 1',
2 => 'First option ' . $value . ' - second option 2'
];
}
$form->addSelect('second', 'Second select:')
->setItems($secondItems);
$form->addSubmit('send', 'Submit');
$form->onSuccess[] = [$this, 'processSelectForm'];
return $form;
}
<?php
return new Multiplier(function ($id) {
return new Multiplier(function ($id2) use ($id) {
});
});
<?php
if ($form['submit']->isSubmittedBy()) {
// ...
}
or
if ($form->isSubmitted() === $form['submit']) {
// ...
}
or
public function validate($form)
{
if ($form->isSubmitted()->getName() == 'reloadSelect')
return;
}
{define form $formName}
<form n:name=$formName>
<ul class=error n:ifcontent>
<li n:foreach="$form->ownErrors as $error">{$error}</li>
</ul>
<table>
<tr n:foreach="$form->controls as $input"
n:if="!$input->getOption(rendered) && $input->getOption(type) !== hidden"
n:class="$input->required ? required">
<th>{label $input}</th>
<td>{input $input} <span class=error n:ifcontent>{$input->error}</span></td>
</tr>
</table>
</form>
{/define}
{* for Bootstrap v3 *}
{define bootstrap-form $formName}
<form n:name=$formName class=form-horizontal>
<ul class=error n:ifcontent>
<li n:foreach="$form->ownErrors as $error">{$error}</li>
</ul>
<div n:foreach="$form->controls as $name => $input"
n:if="!$input->getOption(rendered) && $input->getOption(type) !== hidden"
n:class="form-group, $input->required ? required, $input->error ? has-error">
<div class="col-sm-2 control-label">{label $input}</div>
<div class="col-sm-10">
{if $input->getOption(type) in [text, select, textarea]}
{input $input class => form-control}
{elseif $input->getOption(type) === button}
{input $input class => "btn btn-default"}
{elseif $input->getOption(type) === checkbox}
<div class="checkbox">{input $input}</div>
{elseif $input->getOption(type) === radio}
<div class="radio">{input $input}</div>
{else}
{input $input}
{/if}
<span class=help-block n:ifcontent>{$input->error ?: $input->getOption(description)}</span>
</div>
</div>
</form>
{/define}
<?php
declare(strict_types=1);
namespace App\Forms;
use Nette;
use Nette\Application\UI\Form;
use Nette\Security\User;
final class SignInFormFactory
{
use Nette\SmartObject;
/** @var FormFactory */
private $factory;
/** @var User */
private $user;
public function __construct(FormFactory $factory, User $user)
{
$this->factory = $factory;
$this->user = $user;
}
public function create(callable $onSuccess): Form
{
$form = $this->factory->create();
$form->addText('username', 'Username:')
->setRequired('Please enter your username.');
$form->addPassword('password', 'Password:')
->setRequired('Please enter your password.');
$form->addCheckbox('remember', 'Keep me signed in');
$form->addSubmit('send', 'Sign in');
$form->onSuccess[] = function (Form $form, \stdClass $values) use ($onSuccess): void {
try {
$this->user->setExpiration($values->remember ? '14 days' : '20 minutes');
$this->user->login($values->username, $values->password);
} catch (Nette\Security\AuthenticationException $e) {
$form->addError('The username or password you entered is incorrect.');
return;
}
$onSuccess();
};
return $form;
}