createComponent nastavení selectboxu pomocí setteru
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- Fyasko
- Člen | 106
Ahoj všem. Vím že na toto téma je tu dost vláken, ale žádné mi neporadilo. Předávám komponentě přes setter proměnnou abych mohl vybrat správné pole v selectboxu. Proměnná se do komponenty dostane, ale nejde jí vypsat v createComponent. Nějaká rada? :/
Presenter // Level je int
public function createComponentUserRightsForm()
{
$usr = $this->users->find($this->user->identity->id);
$level = $usr->section->priority;
$form = $this->user_rights_form_factory->create();
if($id = $this->getParameter('id')){
$form->setId($id);
$form->setMaxLevel($level);
}
$form->onSave[] = function(UserRightsForm $form){
$this->flashMessage($this->flash->ok);
$this->redirect('this');
};
return $form;
}
Komponenta
public $maxLevel;
public function setMaxLevel($maxLevel)
{
$this->maxLevel = $maxLevel;
}
public function createComponentUserForm()
{
$form = new EntityForm;
$form->setEntityService($this->users);
$form->bindEntity(new User);
$form->getElementPrototype()
->class('form-horizontal')
->role('form');
if($this->maxLevel == 1)
{
$form->addSelect('section','Oprávnění')
->setItems($this->groups->findPairs('name'))
->setAttribute('class=form-control');
}
else
{
$form->addSelect('section','Oprávnění')
->setItems($this->groups->findPairs(['priority >' => 2], 'name'))
->setAttribute('class=form-control');
}
$form->addSubmit('save', 'uložit')
->getControlPrototype()->class[] = 'btn btn-sm btn-primary';
$form->onSuccess[] = $this->processUserForm;
return $form;
}
proměnná $this->maxLevel vrací NULL pokud jí dumpnu v createComponentUserForm. Když jí dumpnu v setMaxLevel nebo v processuserForm tak vrátí správnou hodnotu. Nevíte kde by mohla být chyba?
- David Matějka
- Moderator | 6445
@PavelKravčík to by spise byla videt v createComponent* a nebyla videt v processUserForm.
- Fyasko
- Člen | 106
<?php
namespace AdminModule\Components;
use Components\EntityForm;
use Entity\User;
use Model\Groups;
use Model\Users;
use Nette;
use Nette\Application\UI;
use Nette\Application\UI\Form;
use Libs\ImageStorage;
class UserRightsForm extends UI\Control
{
/** @var Users */
private $users;
/** @var Groups */
private $groups;
/** @var ImageStorage */
private $image_storage;
public $onSave = [];
public $id;
public $maxLevel;
public function __construct(Users $users,Groups $groups, ImageStorage $image_storage)
{
parent::__construct();
$this->users = $users;
$this->groups = $groups;
$this->image_storage = $image_storage;
}
public function render()
{
$this->template->render(__DIR__ . '/userRightsForm.latte');
}
public function setId($id)
{
$form = $this['userForm'];
$this->id = $id;
$form->bindEntity($this->users->find($id));
}
public function setMaxLevel($maxLevel)
{
$this->maxLevel = $maxLevel;
}
public function createComponentUserForm()
{
$form = new EntityForm;
$form->setEntityService($this->users);
$form->bindEntity(new User);
$form->getElementPrototype()
->class('form-horizontal')
->role('form');
if($this->maxLevel == 1)
{
$form->addSelect('section','Oprávnění')
->setItems($this->groups->findPairs('name'))
->setAttribute('class=form-control');
}
else
{
$form->addSelect('section','Oprávnění')
->setItems($this->groups->findPairs(['priority >' => 1], 'name'))
->setAttribute('class=form-control');
}
$form->addSubmit('save', 'uložit')
->getControlPrototype()->class[] = 'btn btn-sm btn-primary';
$form->onSuccess[] = $this->processUserForm;
return $form;
}
public function processUserForm(EntityForm $form, $values)
{
$group = $this->groups->find($values->section);
//dump($group);exit;
//dump($form);exit;
//dump($values);exit;
$user = $this->users->find($this->id);
$user->section = $group;
//dump($user);exit;
$this->users->save($user);
//$form->handler($group);
$this->onSave($this);
}
}
interface IUserRightsFormFactory
{
/** @return UserRightsForm */
function create();
}
- David Matějka
- Moderator | 6445
v setId inicializujes ten form pres
$form = $this['userForm'];
takze bude stacit prohodit
if($id = $this->getParameter('id')){
$form->setId($id);
$form->setMaxLevel($level);
}
na
if($id = $this->getParameter('id')){
$form->setMaxLevel($level);
$form->setId($id);
}