Volanie componenty v componente `UI\Control` a `Form`
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- Chipso
- Člen | 28
Zdravim,
Mam jednu otazku, ktoru by som chcel vyriesit nejako solidnejsie. Momentalne riesenie je:
- Umiestnena komponenta pre validaciu passwordu
components/passwordIndicator/PasswordIndicatorComponent.php
- Componenta sa nasledne vola vo
RegistrationFormControl
<?php
namespace App\AuthModule;
use App;
use Nette\Application\UI;
class RegistrationFormControl extends UI\Control
{
/** @var callable[] */
public $onSuccess = [];
/** @var RegistrationFormFactory */
private $registrationFormFactory;
/**
* @param RegistrationFormFactory $registrationFormFactory
*/
public function __construct(RegistrationFormFactory $registrationFormFactory)
{
$this->registrationFormFactory = $registrationFormFactory;
parent::__construct();
}
/**
* @return App\BaseForm
*/
protected function createComponentForm()
{
$form = $this->registrationFormFactory->create();
$form->onSuccess = $this->onSuccess;
return $form;
}
/**
* @return App\PasswordStrengthIndicatorComponent
*/
protected function createComponentPasswordStrengthIndicator()
{
return new App\PasswordStrengthIndicatorComponent();
}
public function render()
{
$this->template->setFile(__DIR__ . '/default.latte');
$this->template->render();
}
}
?>
No tu je potrebne vytvorit explicitne sablonu default.latte
pre
form ktora vyzera nejako takto:
<form n:name="form" class="registration-form">
<fieldset n:class="$form['email']->required ? 'required', form-group">
<label n:name="email" n:class="$form['email']->required ? 'required'">{$form['email']->caption}</label>
<input n:name="email" n:class="$form['email']->error ? 'error', form-control">
<small n:ifcontent class="error-message text-muted">{$form['email']->error}</small>
</fieldset>
<fieldset n:class="$form['company']->required ? 'required', form-group">
<label n:name="company" n:class="$form['company']->required ? 'required'">{$form['company']->caption}</label>
<input n:name="company" n:class="$form['company']->error ? 'error', form-control">
<small n:ifcontent class="error-message text-muted">{$form['company']->error}</small>
</fieldset>
<fieldset n:class="$form['password']->required ? 'required', form-group">
<label n:name="password" n:class="$form['password']->required ? 'required'">{$form['password']->caption}</label>
<div class="input-group">
<input n:name="password" n:class="$form['password']->error ? 'error', form-control">
<div class="registration-form-reveal input-group-addon"><i class="fa fa-eye-slash"></i></div>
</div>
<small n:ifcontent class="error-message text-muted">{$form['password']->error}</small>
</fieldset>
{control passwordStrengthIndicator}
<fieldset class="registration-form-submit form-group">
<input n:name="submit" class="btn btn-primary button">
</fieldset>
</form>
Potreboval by som urobit nieco taketo, (napisem v pseudo kode)
<?php
namespace App\AuthModule;
use App;
use Nette;
use Nette\Forms\Form;
class RegistrationFormFactory extends Nette\Object
{
/**
* @return App\BaseForm
*/
public function create()
{
$form = new App\BaseForm();
$form->addText('email', 'Email')
->setRequired('Please enter your Email.')
->addRule(Form::EMAIL)
->getControlPrototype()->autofocus = TRUE;
$form->addText('company', 'Company name')
->setRequired('Please enter your company name.');
$form->addPassword('password', 'Password')
->setRequired('Please enter your password')
->setComponent(new PasswordStrengthIndicatorComponent()); // NIECO NA TENTO SPOSOB.
$form->addSubmit('submit', 'Register');
return $form;
}
}
?>
Ide oto, aby som nemusel vytvarat explicitnu sablonu pre formu, ako ju mam v
default.latte
form.
Editoval Chipso (17. 10. 2015 17:02)
- David Matějka
- Moderator | 6445
Udelej si to jako vlastni formularovy control, viz priklad. Pokud bys potreboval nejake pokrocilejsi veci z komponentoveho modelu (jako napriklad signaly), muzes vyuzit nextras/forms, konkretne tyhle traity