Dynamický selectbox ve formuláři coby komponentě
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- Kakaku
- Člen | 27
Zdravím,
včera jsem se naučil jak dělat dynamický selectbox skrz nette, avšak
u formuláře který je deklarován v preenteru. Teď jsem si udělal
továrničku a hlásí mi to tu chybu, jako včera (Please select a valid
option).
Komponenta formuláře:
class CreatingCRMForm extends Control
{
const MSG_REQ = "Toto pole je povinné";
protected $customersManager;
protected $listManager;
protected $contactPersonsManager;
protected $customers;
protected $crm_types;
protected $contactPersons_positions;
protected $product_list;
public $onFormSuccess;
/**
* CreatingCRMForm constructor.
* @param CustomersManager $customersManager
* @param ListManager $listManager
*/
public function __construct(CustomersManager $customersManager, ListManager $listManager, ContactPersonsManager $contactPersonsManager)
{
parent::__construct();
$this->customersManager = $customersManager;
$this->listManager = $listManager;
$this->contactPersonsManager = $contactPersonsManager;
$this->fill();
}
public function render()
{
$this->template->getLatte()->addProvider('formsStack', [$this["creatingCRMForm"]]);
$this->template->setFile(__DIR__ . '/creatingCRMForm.latte');
$this->template->product_list = $this->product_list;
$this->template->render();
}
/**
* @param $name
* @return Form
*/
public function createComponentCreatingCRMForm()
{
$form = new Form();
$form->getElementPrototype()->addAttributes(array('class' => 'form-horizontal'));
$form->getElementPrototype()->onsubmit('tinyMCE.triggerSave()');
$form->addSelect('company', "Zákazník ", $this->customers)
->setRequired(TRUE)
->addRule(Form::NOT_EQUAL, "Vyberte zákazníka!", -1);
$form['company']->getControlPrototype()->addAttributes(array('class' => 'form-control ajax'));
$form['company']->getLabelPrototype()->addAttributes(array('class' => 'control-label col-sm-offset-1 col-sm-2 col-xs-12'));
$form->addText('company_street', "Ulice ")->setDisabled();
$form['company_street']->getControlPrototype()->addAttributes(array('class' => 'form-control'));
$form['company_street']->getLabelPrototype()->addAttributes(array('class' => 'control-label col-sm-offset-1 col-sm-2 col-xs-12'));
$form->addText('company_city', "Město ")->setDisabled();
$form['company_city']->getControlPrototype()->addAttributes(array('class' => 'form-control'));
$form['company_city']->getLabelPrototype()->addAttributes(array('class' => 'control-label col-sm-offset-1 col-sm-2 col-xs-12'));
$form->addText('company_zip', "PSČ ")->setDisabled();
$form['company_zip']->getControlPrototype()->addAttributes(array('class' => 'form-control'));
$form['company_zip']->getLabelPrototype()->addAttributes(array('class' => 'control-label col-sm-offset-1 col-sm-2 col-xs-12'));
$form->addRadioList('contact_person_type', "Vybrání kontaktní osoby: ", array("old" => "Vybrat ze zadaných", "new" => "Vytvořit novou"))
->setDefaultValue('old')
->getSeparatorPrototype()->setName(NULL);
$form['contact_person_type']->addCondition(Form::EQUAL, "old")
->toggle('contact_persons_list');
$form['contact_person_type']->addCondition(Form::EQUAL, "new")
->toggle('contact_person_firstname')
->toggle('contact_person_lastname')
->toggle('contact_person_email')
->toggle('contact_person_phone')
->toggle('contact_person_position')
->toggle('contact_person_main');
$form['contact_person_type']->getControlPrototype()->addAttributes(array('class' => 'admin-radiobox'));
$form['contact_person_type']->getLabelPrototype()->addAttributes(array('class' => 'control-label col-sm-offset-1 col-sm-2 col-xs-12'));
$form->addSelect('contact_person', "", $this->getPersons($form['company']->value))
->setRequired(FALSE)
->addConditionOn($form['contact_person_type'], Form::EQUAL, "old")
->addRule(Form::NOT_EQUAL, "Musí být vybrána kontaktní osoba!", -1);
$form['contact_person']->getControlPrototype()->addAttributes(array('class' => 'form-control ajax', 'id' => 'contact_persons_list'));
$form['contact_person']->getLabelPrototype()->addAttributes(array('class' => 'control-label col-sm-offset-1 col-sm-2 col-xs-12'));
...
$form->addSubmit('submit', 'Vytvořit CRM záznam');
return $form;
}
public function processForm($form, $values)
{
$this->onFormSuccess();
}
public function getPersons($customer_id)
{
$contactPersons = $this->contactPersonsManager->getPersonsByCustomer($customer_id);
$contactPersonsForArray[-1] = "Nevybráno";
foreach($contactPersons as $cp)
{
$contactPersonsForArray[$cp->id] = $cp->firstname . " " . $cp->lastname;
}
return $contactPersonsForArray;
}
public function fill()
{
$customers = $this->customersManager->getAllCustomers()->order("company_name ASC");
$this->customers[-1] = "Nevybráno";
foreach ($customers as $c) {
$this->customers[$c->id] = $c->company_name . " - " . $c->ref('OZ_id')->firstname . " " . $c->ref('OZ_id')->lastname;
}
$this->crm_types = $this->listManager->getListOfSomething("crm_types", false);
$this->contactPersons_positions[-1] = "Nevybráno";
$this->contactPersons_positions += $this->listManager->getListOfPersons("positions", true);
$this->product_list = $this->listManager->getListOfSomething("product_type", true);
}
}
interface ICreatingCRMFormFactory
{
/**
* @return CreatingCRMForm
*/
function create();
}
Presenter:
<?php
class CRMPresenter extends BasePresenter
{
protected $managerList = array("customers", "list", "contactPersons");
/** @var CustomersManager */
protected $customersManager;
/** @var ContactPersonsManager */
protected $contactPersonsManager;
/** @var ListManager */
protected $listManager;
/** Seznam zákazníků */
protected $customers;
/** Seznam kontaktních osob */
protected $contactPersons;
/** Typ CRM záznamů*/
protected $crm_types;
/** Seznam produktů */
protected $product_list;
/** Seznam pozic kontaktních osob */
protected $contactPersons_positions;
/** @var \App\CoreModule\Controls\ICreatingCRMFormFactory @inject */
public $ICreatingCRMFormFactory;
public function beforeRender()
{
parent::beforeRender();
}
public function actionCreateCRM()
{
$customers = $this->customersManager->getAllCustomers()->order("company_name ASC");
$this->customers[-1] = "Nevybráno";
foreach ($customers as $c) {
$this->customers[$c->id] = $c->company_name . " - " . $c->ref('OZ_id')->firstname . " " . $c->ref('OZ_id')->lastname;
}
$this->crm_types = $this->listManager->getListOfSomething("crm_types", false);
$this->contactPersons_positions[-1] = "Nevybráno";
$this->contactPersons_positions += $this->listManager->getListOfPersons("positions", true);
$this->product_list = $this->listManager->getListOfSomething("product_type", true);
}
protected function createComponentCreatingCRMForm($name)
{
$form = $this->ICreatingCRMFormFactory->create();
$form->onFormSuccess[] = function () {
$this->flashMessage('Form has been submitted successfuly', 'success');
};
return $form;
}
public function handleLoadCustomer($id)
{
if($id != -1)
{
$customer = $this->customersManager->getCustomer($id);
$this['creatingCRMForm']['creatingCRMForm']['contact_person']->setItems($this['creatingCRMForm']->getPersons($id));
$this['creatingCRMForm']['creatingCRMForm']['company_street']->setValue($customer->company_street);
$this['creatingCRMForm']['creatingCRMForm']['company_city']->setValue($customer->company_city);
$this['creatingCRMForm']['creatingCRMForm']['company_zip']->setValue($customer->company_zip);
}
else
{
$this['creatingCRMForm']['creatingCRMForm']['contact_person']->setItems(array(-1 => "Nevybráno"));
$this['creatingCRMForm']['creatingCRMForm']['company_street']->setValue("");
$this['creatingCRMForm']['creatingCRMForm']['company_city']->setValue("");
$this['creatingCRMForm']['creatingCRMForm']['company_zip']->setValue("");
}
$this['creatingCRMForm']->redrawControl('contactPersonSnippet');
$this['creatingCRMForm']->redrawControl('companyAddressSnippet');
}
}
{form creatingCRMForm}
<ul class="errors" n:if="$form->hasErrors()">
<li n:foreach="$form->errors as $error">{$error}</li>
</ul>
<div class="form-group">
{label company /}
<div class="col-sm-4">
{input company}
</div>
</div>
{snippet companyAddressSnippet}
<div class="form-group">
{label company_street /}
<div class="col-sm-4">
{input company_street}
</div>
</div>
<div class="form-group">
{label company_city /}
<div class="col-sm-4">
{input company_city}
</div>
</div>
<div class="form-group">
{label company_zip /}
<div class="col-sm-4">
{input company_zip}
</div>
</div>
{/snippet}
<div class="form-group">
{label type /}
<div class="col-sm-4">
{input type}
</div>
</div>
Jde o to, že v selectu vyberu firmu a pro tu se mi načtou kontaktní osoby do dalšího selectboxu. Dělám se s tím půl dne a pořád na to nemohu přijít :/
Editoval Kakaku (18. 2. 2017 18:25)
- David Matějka
- Moderator | 6445
jeden z moznych workaroundu pro dynamicke select boxy je https://zlml.cz/…t-select-box