/** * @param Form $form */ public function errorCallback(Form $form) { if (!$form->isValid() && $this->uploadedImages) { foreach ($this->uploadedImages as $image) { $this->storage->delete($image); } } }
$form->addText('city', 'City')->addConditionOn($form['send'], Form::EQUAL, TRUE)->addRule(Form::FILLED, 'Enter your shipping address'); $form->addSelect('country', 'Country', $countries)->skipFirst()->addConditionOn($form['send'], Form::EQUAL, TRUE)->addRule(Form::FILLED, 'Select your country'); // group Your account $form->addGroup('Your account'); $form->addPassword('password', 'Choose password')->addRule(Form::FILLED, 'Choose your password')->addRule(Form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3)->setOption('description', '(at least 3 characters)'); $form->addPassword('password2', 'Reenter password')->addConditionOn($form['password'], Form::VALID)->addRule(Form::FILLED, 'Reenter your password')->addRule(Form::EQUAL, 'Passwords do not match', $form['password']); $form->addFile('avatar', 'Picture'); $form->addHidden('userid'); $form->addTextArea('note', 'Comment'); // group for buttons $form->addGroup(); $form->addSubmit('submit', 'Send'); // Step 2: Check if form was submitted? if ($form->isSubmitted()) { // Step 2c: Check if form is valid if ($form->isValid()) { echo '<h2>Form was submitted and successfully validated</h2>'; $values = $form->getValues(); Debug::dump($values); // this is the end, my friend :-) if (empty($disableExit)) { exit; } } } else { // not submitted, define default values $defaults = array('name' => 'John Doe', 'userid' => 231, 'country' => 'CZ'); $form->setDefaults($defaults); } // Step 3: Render form ?>
/** * @param Form */ public function save(Form $form) { $this->form = $form; $this->entities = []; $this->execute(function () { $this->entityManager->persist($this->entity); }); $this->saveValues($this->applyOffset($form->getComponents(), $this->offset), $this->entity); if (!$form->isValid()) { return; } $this->getExecutionStrategy()->confirm(); $valid = TRUE; foreach ($this->entities as $entity) { try { $this->runValidation(function (ValidatorInterface $validator) use($entity) { return $validator->validate($entity); }, $form); } catch (ValidationException $e) { $valid = FALSE; } } if ($valid && $this->getAutoFlush()) { $this->flush(); } }
/** * Do the magic */ public function processActions() { // WP_List_Table export \SimpleSubscribe\TableSubscribes::process(); // settings form if ($this->formSettings->isSubmitted() && $this->formSettings->isValid()) { $values = $this->formSettings->getValues(TRUE); // if there are cateogires selected, and ALL as well, uncheck remaining if (count(array_filter($values['cat'])) > 0 && $values['cat']['0'] == TRUE) { foreach ($values['cat'] as $key => $value) { $values['cat'][$key] = FALSE; $this->formSettings['cat'][$key]->value = FALSE; } $values['cat']['0'] = TRUE; $this->formSettings['cat']['0']->value = TRUE; // if there is other category selected, unselect ALL } elseif (count(array_filter($values['cat'])) > 1) { $values['cat']['0'] = FALSE; $this->formSettings['cat']['0']->value = FALSE; // if there's no category selected, select ALL } elseif (!in_array(TRUE, $values['cat'])) { $values['cat']['0'] = TRUE; $this->formSettings['cat']['0']->value = TRUE; } $this->settings->saveSettings($values); $this->addNotice('updated', 'Settings successfully saved.'); } elseif ($this->formSettings->hasErrors()) { foreach ($this->formSettings->getErrors() as $error) { $this->addNotice('error', $error); } } // email template (saved in settings table tho) if ($this->formEmailTemplate->isSubmitted() && $this->formEmailTemplate->isValid()) { $this->settings->saveSettings($this->formEmailTemplate->getValues(TRUE)); $this->addNotice('updated', 'Settings successfully saved.'); } elseif ($this->formEmailTemplate->hasErrors()) { foreach ($this->formEmailTemplate->getErrors() as $error) { $this->addNotice('error', $error); } } // mass email if ($this->formEmail->isSubmitted() && $this->formEmail->isValid()) { try { $this->email->sendMassEmail($this->formEmail->getValues(TRUE)); $this->addNotice('updated', 'Email successfully sent.'); } catch (EmailException $e) { $this->addNotice('error', $e->getMessage()); } } elseif ($this->formEmail->hasErrors()) { foreach ($this->formEmail->getErrors() as $error) { $this->addNotice('error', $error); } } // subscriber form if ($this->formSubscriber->isSubmitted() && $this->formSubscriber->isValid()) { try { $this->subscribers->addThruAdmin($this->formSubscriber->getValues()); $this->addNotice('updated', 'Subscriber successfully added.'); } catch (RepositarySubscribersException $e) { $this->addNotice('error', $e->getMessage()); } } elseif ($this->formSubscriber->hasErrors()) { foreach ($this->formSubscriber->getErrors() as $error) { $this->addNotice('error', $error); } } // wp subscriber form if ($this->formSubscriberWp->isSubmitted() && $this->formSubscriberWp->isValid()) { try { $users = $this->formSubscriberWp->getValues(TRUE); $this->subscribers->addWpRegistered($users['users']); $this->addNotice('updated', 'Subscriber(s) successfully added.'); } catch (RepositarySubscribersException $e) { $this->addNotice('error', $e->getMessage()); } } elseif ($this->formSubscriberWp->hasErrors()) { foreach ($this->formSubscriberWp->getErrors() as $error) { $this->addNotice('error', $error); } } // email preview form if ($this->formEmailPreview->isSubmitted() && $this->formEmailPreview->isValid()) { try { $this->email->sendEmailPreview($this->formEmailPreview->getValues(TRUE)); $this->addNotice('updated', 'Email Preview successfully sent.'); } catch (EmailException $e) { $this->addNotice('error', $e->getMessage()); } } elseif ($this->formEmailPreview->hasErrors()) { foreach ($this->formEmailPreview->getErrors() as $error) { $this->addNotice('error', $error); } } }
use FileDownloader\Downloader\AdvancedDownloader; use FileDownloader\FDTools; use FileDownloader\FileDownload; use Nette\Diagnostics\Debugger; use Nette\Forms\Form; // Generate form $f = new Form("upload-form"); $f->getElementPrototype()->id = "frm"; $f->setMethod("GET"); $f->addSelect("speed", "Speed", array(1 => "1byte/s", 50 => "50bytes/s", 512 => "512bytes/s", 1 * FDTools::KILOBYTE => "1kb/s", 5 * FDTools::KILOBYTE => "5kb/s", 20 * FDTools::KILOBYTE => "20kb/s", 32 * FDTools::KILOBYTE => "32kb/s", 50 * FDTools::KILOBYTE => "50kb/s", 64 * FDTools::KILOBYTE => "64kb/s", 100 * FDTools::KILOBYTE => "100kb/s", 128 * FDTools::KILOBYTE => "128kb/s", 200 * FDTools::KILOBYTE => "200kb/s", 256 * FDTools::KILOBYTE => "256kb/s", 300 * FDTools::KILOBYTE => "300kb/s", 512 * FDTools::KILOBYTE => "512kb/s", 1 * FDTools::MEGABYTE => "1mb/s", 2 * FDTools::MEGABYTE => "2mb/s", 5 * FDTools::MEGABYTE => "5mb/s", 10 * FDTools::MEGABYTE => "10mb/s", 0 => "Unlimited")); $f->addText("filename", "Filename")->addRule(Form::FILLED, "You must fill name!"); $f->addSelect("size", "Size", array(1 => "1MB", 4 => "4MB", 8 => "8MB", 16 => "16MB", 32 => "32MB", 64 => "64MB", 128 => "128MB", 256 => "256MB", 512 => "512MB")); $f->addSelect("log", "Log called events?", array(0 => "No", 1 => "Yes (may cause CPU load)")); $f->addSubmit("download", "Download!"); $f->setDefaults(array("speed" => 50, "filename" => "Some horrible file name - ěščřžýáíé.bin", "size" => 8, "log" => 1)); if ($f->isSubmitted() and $f->isValid()) { Debugger::enable(Debugger::PRODUCTION); // Log errors to file! $val = $f->getValues(); $location = dirname(__FILE__) . "/cache/test-" . $val["size"] . "MB.tmp"; if (!file_exists($location)) { generateFile($location, $val["size"] * 1024); } /* Interface with getters and setters */ $file = new FileDownload(); $file->sourceFile = $location; $file->transferFileName = $val["filename"]; $file->speedLimit = (int) $val["speed"]; //$file->mimeType = $val["mimeType"]; /* Functions defined in example_library.php */ if ($val["log"] == 1) {
* - for the best experience, use the latest version of browser (Internet Explorer 9, Firefox 4, Chrome 5, Safari 5, Opera 9) */ require_once __DIR__ . '/../../Nette/loader.php'; use Nette\Forms\Form, Nette\Debug; Debug::enable(); // Step 1: Define form with validation rules $form = new Form(); $form->addGroup(); $form->addText('query', 'Search:')->setType('search')->setAttribute('autofocus'); $form->addText('count', 'Number of results:')->setType('number')->setDefaultValue(10)->addRule(Form::INTEGER, 'Must be numeric value')->addRule(Form::RANGE, 'Must be in range from %d to %d', array(1, 100)); $form->addText('precision', 'Precision:')->setType('range')->setDefaultValue(50)->addRule(Form::INTEGER, 'Precision must be numeric value')->addRule(Form::RANGE, 'Precision must be in range from %d to %d', array(0, 100)); $form->addText('email', 'Send to e-mail:')->setType('email')->setAttribute('autocomplete', 'off')->setAttribute('placeholder', 'Optional, but Recommended')->addCondition(Form::FILLED)->addRule(Form::EMAIL, 'Incorrect E-mail Address'); // ... then check email $form->addSubmit('submit', 'Send'); // Step 2: Check if form was submitted? if ($form->isSubmitted() && $form->isValid()) { echo '<h2>Form was submitted and successfully validated</h2>'; $values = $form->getValues(); Debug::dump($values); // this is the end, my friend :-) if (empty($disableExit)) { exit; } } // Step 3: Render form ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8">
<div class="page-header"> <h1> <i class="fa fa-plus"></i> Add a new station log </h1> </div> <?php use Nette\Forms\Form; use Kdyby\BootstrapFormRenderer\BootstrapRenderer; $form = new Form(); $form->setRenderer(new BootstrapRenderer()); $form->addProtection(); $form->addText('reporter', 'Nickname')->setAttribute('placeholder', 'anonymous')->setRequired(); date_default_timezone_set("UTC"); $form->addText('datetime', 'When')->setAttribute('placeholder', '2014-01-01 14:00')->setDefaultValue(date('Y-m-d H:i:s'))->setRequired(); $form->addText('station', 'Station designator')->setRequired()->setAttribute('placeholder', 'E11'); $form->addText('qrh', 'Frequency')->setRequired()->setAttribute('placeholder', '4625')->addRule(Form::FLOAT); $form->addText('callnumber', 'Call # (leave empty if not captured)')->setAttribute('placeholder', '472 639 5 or 441/30'); $form->addText('callid', 'Call ID (leave empty if not captured)')->setAttribute('placeholder', '472 639 5 or 441/30'); $form->addText('gc', 'Group Count')->setAttribute('placeholder', '10'); $form->addTextArea('body', 'Message (leave empty if not captured)')->setAttribute('placeholder', '39715 12345'); $form->addSubmit('send', 'Add to our mighty database'); if ($form->isSuccess() && $form->isValid()) { //die(); $f = $form->getValues(); //dump($f); $arr = array('time' => $f['datetime'], 'station' => $f['station'], 'qrh' => $f['qrh'], 'call_number' => $f['callnumber'], 'call_id' => $f['callid'], 'gc' => $f['gc'], 'body' => $f['body'], 'reporter' => $f['reporter']); dibi::query('insert into logs_new', $arr); echo "Log has been added. Thank you."; } $form->render();