Predani promennych do sablony formulare
- mo
- Člen | 5
Zdravim, mam nasledujici formular
class UploadForm extends BootstrapForm
{
…
public function __construct($parent, $name, $fileName, $filePath, $fileType = TYPE_ALL, $uploadParams = array(), $uploadId = null)
{
$this->fileName = $fileName;
$this->filePath = $filePath;
$this->fileType = $fileType;
$this->uploadId = $uploadId;
if (!isset($uploadParams[self::MAX_WIDTH]))
$uploadParams[self::MAX_WIDTH] = self::DEFAULT_MAX_IMG_SIZE;
if (!isset($uploadParams[self::MAX_HEIGHT]))
$uploadParams[self::MAX_HEIGHT] = self::DEFAULT_MAX_IMG_SIZE;
$this->uploadParams = $uploadParams;
parent::__construct($parent, $name);
$this->addUpload('img')->getControlPrototype()->class('btn-submit');
$this->addSubmit("save", "Uložit")
->getControlPrototype()->class('btn-submit');
$this->onSuccess[] = $this->uploadFormSucceeded;
}
a k nemu odpovidajici misto v sablone (v samosatatne sablone uploadForm.latte includuje se do sablony cele stranky)
{form uploadForm}
<div class="modal-header">
<a n:href="cancelEdit!" type="button" class="close ajax" data-dismiss="modal" aria-hidden="true"></a>
<h1 class="modal-title">{_'Nahrajte logo firmy'}</h1>
</div>
<div class="modal-body">
{control $form errors}
{input img class => 'btn btn-upload btn-auto'}
</div>
<div class="modal-footer">
{input save class => 'btn btn-submit button'}
<a n:href="cancelEdit!" type="button" class="btn btn-cancel ajax" data-dismiss="modal">{_'Zrušit změny'}</a>
</div>
{/form}
potreboval bych v sablone formulare vypsat promenne ktere mam v dispozici v konstruktoru formulare, ale v konstruktoru nemuzu pristoupit rovnou k casti sablony kde se vykresluje formular, pokud je predam zpusobem
$this->presenter->template->maxWidth = 50;
tak dostanu error unknown variable, a primo k cast sablony kde se vykresluje formular se nedostanu jak bych cekal pres
$this->template->maxWidth = 50;
nebo podobne
tusi nekdo jak to vyresit? diky.
- mo
- Člen | 5
diky za odpoved, nemam primo sablonu pro formular(jak sam pises tak to ani nejde), je to proste jenom misto v hlavni sablone(sablona presenteru ktery formular v tovarne vytvari) kde je include sablony uploadForm.latte (viz druhy kod v predchozim prispevku – to je cely kod sablony uploadForm.latte) – proste mam jenom kod ktery se tyka formulare vyhozeny do samostatneho souboru a nasledne includnuty.
zkusil jsem tedy predat sablone promenne az v metode render tzn do kodu formu jsem pridal toto:
public function render()
{
$this->presenter->template->maxWidth = $this->uploadParams[self::MAX_WIDTH];
$this->presenter->template->maxHeight = $this->uploadParams[self::MAX_HEIGHT];
}
ale porad dostavam error „Undefined variable: maxWidth“.
zkousel jsem si promenou vypsat i mimo includovany kod formu rovnou do hlavni
sablony a porad stejny problem.
je mozny ze cely pristup k problemu je z hlediska nette spatne, a mel bych to resit uplne jinak, jde mi jenom o to ze upload formular pouzivam na mnoha mistech s jinymy parametry, a parametry maxWidth a maxHeight ktere znam v konstruktoru formulare potrebuju vypsat do formulare pro uzivatele, pokud ma nekdo napad jak to udelat tak ho uvitam… pokud je potreba vice informaci jak mam co udelane tak se ptejte, diky moc
Editoval mo (10. 8. 2014 12:47)
- David Matějka
- Moderator | 6445
Pouzij formulare jako komponenty – jen pridas render metodu a budes s tim zachazet jako s klasickou komponentou
- mo
- Člen | 5
tak jeste jeden problem, hodil jsem form do komponenty coz mi umoznuje pouzit sablonu presne jak bych si predstavoval, ale znefunkcni mi to cely formular… proste se vubec nezavola metoda process form ktera predtim fungovala bez problemu, zadna chyba ani kdyz koukam do requestu, nikde zadny problem, kdyz si to loguju, proste se ta metoda vubec nezavola.
zde je moje nova trida pro formular
<?php
namespace Forms;
use MessageType;
use Models\CompanyRepository;
use Models\StudentRepository;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form as AppForm;
use Nette\ComponentModel\IContainer;
use Nette\Diagnostics\Debugger;
use Nette\Forms\Form;
use Nette\Http\FileUpload;
use Nette\Image;
use Nette\Templating\Template;
class UploadControlForm extends Control
{
const TYPE_ALL = 0;
const TYPE_IMAGE = 1;
const DEFAULT_MAX_IMG_SIZE = 1500;
const MAX_WIDTH = 'maxWidth';
const MAX_HEIGHT = 'maxHeight';
const WIDTH = 'width';
const HEIGHT = 'height';
const FLAGS = 'flags';
private $fileName;
private $filePath;
private $fileType;
private $uploadId;
private $uploadParams;
/**
* @param IContainer $parent
* @param null $name
* @param $fileName
* @param $filePath
* @param $fileType
* @param array $uploadParams
* @param null $uploadId
*/
public function __construct($parent, $name, $fileName, $filePath, $fileType = TYPE_ALL, $uploadParams = array(), $uploadId = null)
{
$this->fileName = $fileName;
$this->filePath = $filePath;
$this->fileType = $fileType;
$this->uploadId = $uploadId;
if (!isset($uploadParams[self::MAX_WIDTH]))
$uploadParams[self::MAX_WIDTH] = self::DEFAULT_MAX_IMG_SIZE;
if (!isset($uploadParams[self::MAX_HEIGHT]))
$uploadParams[self::MAX_HEIGHT] = self::DEFAULT_MAX_IMG_SIZE;
$this->uploadParams = $uploadParams;
parent::__construct($parent, $name);
}
public function render()
{
$template = $this->createTemplate();
$template->render();
}
public function createTemplate($class = NULL)
{
$template = parent::createTemplate()->setFile(__DIR__ . '/UploadForm.latte');
$template->setTranslator($this->presenter->translator);
return $template;
}
public function createComponentForm()
{
$form = new Form;
$form->addUpload('img')->getControlPrototype()->class('btn-submit');
$form->addSubmit("save", "Uložit")
->getControlPrototype()->class('btn-submit');
$form->onSuccess[] = $this->processForm;
Debugger::barDump("createComponentForm");
return $form;
}
public function processForm($form)
{
$formValues = $form->getValues();
/* @var $fileUpload FileUpload */
$fileUpload = $formValues['img'];
if (!is_uploaded_file($fileUpload->getTemporaryFile()) || !$fileUpload->isOk()) {
$this->presenter->flashMessage("Vyskytla se chyba při nahrávání obrázku.", MessageType::ERROR);
} else if (!$fileUpload->isImage()) {
$this->presenter->flashMessage("Vložený soubor není obrázek", MessageType::ERROR);
} else if ($fileUpload->imageSize[0] > $this->uploadParams[self::MAX_WIDTH] || $fileUpload->imageSize[1] > $this->uploadParams[self::MAX_HEIGHT]) {
$this->presenter->flashMessage("Vložený obrázek je příliš velký, maximální rozměry jsou " . $this->uploadParams[self::MAX_WIDTH] . " x " . $this->uploadParams[self::MAX_HEIGHT] . " px.", MessageType::ERROR);
} else {
$ext = explode('.', $fileUpload->name);
$ext = $ext[count($ext) - 1];
$filePath = WWW_DIR . $this->filePath;
$fileName = $this->fileName . '.' . $ext;
//copy file
move_uploaded_file($fileUpload->getTemporaryFile(), $filePath . $fileName);
if ($this->uploadParams[self::FLAGS]) {
$image = Image::fromFile($filePath . $fileName);
$image->resize($this->uploadParams[self::WIDTH], $this->uploadParams[self::HEIGHT], $this->uploadParams[self::FLAGS]); //Image::EXACT
$image->save($filePath . $fileName);
}
$this->presenter->uploadComplete($fileName, $filePath, $this->uploadId);
}
}
/**
*
*/
public function handleCancelEdit()
{
$this->presenter->handleCancelEdit();
}
}
v sablone stranky je potom jen
{control uploadLogoForm}
na miste kde se ma obevit formular coz funguje bez problemu.
a na konec pro jistotu prikladam jeste sablonu formu.
{form form}
<div class="modal-header">
<a n:href="cancelEdit!" type="button" class="close ajax" data-dismiss="modal" aria-hidden="true"></a>
<h1 class="modal-title">{_'Nahrajte logo firmy'}</h1>
</div>
<div class="modal-body">
{control $form errors}
{input img class => 'btn btn-upload btn-auto'}
</div>
<div class="modal-footer">
{input save class => 'btn btn-submit button'}
<a n:href="cancelEdit!" type="button" class="btn btn-cancel ajax" data-dismiss="modal">{_'Zrušit změny'}</a>
</div>
{/form}
predem diky za pomoc
Editoval mo (14. 8. 2014 0:59)
- David Matějka
- Moderator | 6445
musis pouzit UI\Form, ne Forms\Form. viz importy, ktery mas spatne:
use Nette\Application\UI\Form as AppForm;
use Nette\Forms\Form;