private function exampleForm() { $countries = array('Select your country', 'Europe' => array('CZ' => 'Czech Republic', 'FR' => 'France', 'DE' => 'Germany', 'GR' => 'Greece', 'HU' => 'Hungary', 'IE' => 'Ireland', 'IT' => 'Italy', 'NL' => 'Netherlands', 'PL' => 'Poland', 'SK' => 'Slovakia', 'ES' => 'Spain', 'CH' => 'Switzerland', 'UA' => 'Ukraine', 'GB' => 'United Kingdom'), 'AU' => 'Australia', 'CA' => 'Canada', 'EG' => 'Egypt', 'JP' => 'Japan', 'US' => 'United States', '?' => 'other'); $sex = array('m' => 'male', 'f' => 'female'); // Step 1: Define form with validation rules $form = new Form(); // group Personal data $form->addGroup('Personal data')->setOption('description', 'We value your privacy and we ensure that the information you give to us will not be shared to other entities.'); $form->addText('name', 'Your name:', 35)->addRule(Form::FILLED, 'Enter your name'); $form->addText('age', 'Your age:', 5)->addRule(Form::FILLED, 'Enter your age')->addRule(Form::INTEGER, 'Age must be numeric value')->addRule(Form::RANGE, 'Age must be in range from %.2f to %.2f', array(9.9, 100)); $form->addRadioList('gender', 'Your gender:', $sex); $form->addText('email', 'E-mail:', 35)->setEmptyValue('@')->addCondition(Form::FILLED)->addRule(Form::EMAIL, 'Incorrect E-mail Address'); // ... then check email // group Shipping address $form->addGroup('Shipping address')->setOption('embedNext', TRUE); $form->addCheckbox('send', 'Ship to address')->addCondition(Form::EQUAL, TRUE)->toggle('sendBox'); // toggle div #sendBox // subgroup $form->addGroup()->setOption('container', Html::el('div')->id('sendBox')); $form->addText('street', 'Street:', 35); $form->addText('city', 'City:', 35)->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:'******'Choose your password')->addRule(Form::MIN_LENGTH, 'The password is too short: it must be at least %d characters', 3); $form->addPassword('password2', 'Reenter password:'******'password'], Form::VALID)->addRule(Form::FILLED, 'Reenter your password')->addRule(Form::EQUAL, 'Passwords do not match', $form['password']); $form->addFile('avatar', 'Picture:')->addCondition(Form::FILLED)->addRule(Form::MIME_TYPE, 'Uploaded file is not image', 'image/*'); $form->addHidden('userid'); $form->addTextArea('note', 'Comment:', 30, 5); // group for buttons $form->addGroup(); $form->addSubmit('submit1', '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); } return $form; }
public function actionUserChange($akce = 0, $id = -1) { $this->access($id); //kvůli Nette - action je vyhrazeno $action = $akce; $this->verifyUser(); $this->form = new AppForm($this, 'userChange'); //TODO: Definovat zprávu //$this->form->addProtection($this->formProtectedMessage); $msg = 'Heslo musí obsahovat VELKÁ a malá písmena a číslice!'; $this->form->addGroup('Přihlašovací informace'); $this->form->addText('userName', 'Uživatelské jméno:')->addRule(Form::FILLED, 'Uživatelské jméno musí být vyplněno.')->addRule(Form::MIN_LENGTH, 'Uživatelské jméno musí mít alespoň %d znaky.', 4)->addRule(Form::MAX_LENGTH, 'Uživatelské jméno může mít nejvýše %d znaků.', 20); if ($action > 0) { $this->form['userName']->setDisabled(true); } if ($action != 1) { $this->form->addPassword('password1', 'Heslo:')->addRule(Form::FILLED, 'Heslo musí být vyplněno.')->addRule(Form::MIN_LENGTH, 'Heslo musí mít alespoň %d znaků.', 8)->addRule(Form::MAX_LENGTH, 'Heslo může mít nejvýše %d znaků', 50)->addRule(Form::REGEXP, $msg, '/[A-Z]+/')->addRule(Form::REGEXP, $msg, '/[a-z]+/')->addRule(Form::REGEXP, $msg, '/[0-9]+/'); $this->form->addPassword('password2', 'Přístupové heslo (pro kontrolu):')->addRule(Form::EQUAL, 'Zadaná hesla se neshodují.', $this->form['password1']); $this->form->addText('navrhHesla', 'Navrhované heslo:'); } if ($action < 2) { $this->form->addGroup('Osobní informace'); $this->form->addText('firstName', 'Jméno:')->addRule(Form::FILLED, 'Jméno musí být vyplněno.'); $this->form->addText('surname', 'Přijmení:')->addRule(Form::FILLED, 'Přijmení musí být vyplněno.'); $this->form->addText('title', 'Titul:')->addRule(Form::MAX_LENGTH, 'Titul může mít nejvýše %d znaků.', 10); $this->form->addGroup('Kontaktní informace'); $this->form->addText('email', 'E-mail:')->addRule(Form::FILLED, 'E-mail musí být vyplněn.')->addRule(Form::EMAIL, 'Zadaný e-mail není platný.'); $this->form->addText('icq', 'ICQ:')->addCondition(Form::FILLED)->addRule(Form::NUMERIC, 'ICQ musí být číslo.')->addRule(Form::MAX_LENGTH, 'ICQ může mít nejvýše %d znaků.', 10); $this->form->addText('skype', 'Skype:')->addRule(Form::MAX_LENGTH, 'Skype může mát nejvýše %d znaků.', 50); $this->form->addText('mobile', 'Mobilní telefon:')->addCondition(Form::FILLED)->addRule(Form::NUMERIC, 'Mobilní telefon musí být číslo.')->addRule(Form::MAX_LENGTH, 'Mobilní telefon může mít nejvýše %d znaků.', 20); $roles = UsersModel::getRoles(); // Zobrazujeme pouze tehdy je-li uživatel administrátor - aby si uživatel sám nemohl měnit skupinu if ($this->user->isInRole('Administrator')) { $this->form->addGroup('Přístupová práva (může být vybráno více skupin)'); $this->form->addMultiSelect('prava', 'Přístupová práva:', $roles, 2); } } $this->form->onSubmit[] = array($this, 'UserFormSubmitted'); $this->form->addHidden('action')->setValue($action); if ($action > 0) { $data = UsersModel::getUser($id); $data['navrhHesla'] = UsersModel::genPass(); // Nastavíme výchozí hodnoty pro formulář $this->form->setDefaults($data); $this->form->addHidden('id')->setValue($id); $this->form->addSubmit('ok', 'Aktualizovat'); // ->onClick[] = array($this, 'OkClicked'); // nebo 'OkClickHandler' } else { $data['navrhHesla'] = UsersModel::genPass(); // Nastavíme výchozí hodnoty pro formulář $this->form->setDefaults($data); $this->form->addSubmit('ok', 'Vytvořit'); // ->onClick[] = array($this, 'OkClicked'); // nebo 'OkClickHandler' } $this->template->form = $this->form; }
/** * Nette\Forms example 7 * * - custom charset encoding (Forms internally works in UTF-8 encoding!) */ require '../../Nette/loader.php'; /*use Nette\Forms\Form;*/ /*use Nette\Debug;*/ /*use Nette\Web\Html;*/ Debug::enable(); $countries = array('Select your country', 'Europe' => array('CZ' => 'Česká republika', 'SK' => 'Slovakia'), 'AU' => 'Australia', 'CA' => 'Canada', 'EG' => 'Egypt', 'JP' => 'Japan', 'US' => 'United States', '?' => 'other'); // Step 1: Define form with validation rules $form = new Form(); $form->encoding = 'ISO-8859-1'; // group Personal data $form->addGroup('Personal data'); $form->addText('name', 'Your name:', 35); $form->addMultiSelect('country', 'Country:')->skipFirst()->setItems($countries, FALSE); $form->addHidden('userid'); $form->addTextArea('note', 'Comment:', 30, 5); // group for buttons $form->addGroup(); $form->addSubmit('submit1', 'Send'); // Step 2: Check if form was submitted? if ($form->isSubmitted()) { // Step 2c: Check if form is valid if ($form->isValid()) { header('Content-type: text/html; charset=utf-8'); echo '<h2>Form was submitted and successfully validated</h2>'; $values = $form->getValues(); Debug::dump($values);
public function getVoteForm($hash) { if ($this->checkHash($hash) == $this->id) { $form = new Form('vote', 'POST', '/Vote/' . $hash, ''); $choices = array(); foreach ($this->getChoices() as $choice) { if (count($this->getChoices($choice->getId())) > 0) { $options = array(); foreach ($this->getChoices($choice->getId()) as $option) { $options[] =& $form->createElement('radio', null, null, $option->getChoice(), $option->getId()); } $form->addGroup($options, 'vote_choice[' . $choice->getId() . ']', $choice->getChoice(), '<br/>'); } } $form->addElement('hidden', 'vote_submit', null, true); $form->addElement('submit', 'submit', 'Vote'); return $form; } return false; }
*/ public function translate($message, $count = NULL) { return parent::translate($message); } } $countries = array('Select your country', 'Europe' => array('CZ' => 'Czech Republic', 'SK' => 'Slovakia'), 'US' => 'USA', '?' => 'other'); $sex = array('m' => 'male', 'f' => 'female'); // Step 1: Define form with validation rules $form = new Form(); // enable translator $translator = new MyTranslator('gettext', dirname(__FILE__) . '/messages.mo', 'cs'); $translator->setLocale('cs'); $form->setTranslator($translator); // group Personal data $form->addGroup('Personal data'); $form->addText('name', 'Your name:', 35)->addRule(Form::FILLED, 'Enter your name'); $form->addText('age', 'Your age:', 5)->addRule(Form::FILLED, 'Enter your age')->addRule(Form::INTEGER, 'Age must be numeric value')->addRule(Form::RANGE, 'Age must be in range from %d to %d', array(10, 100)); $form->addRadioList('gender', 'Your gender:', $sex); $form->addText('email', 'E-mail:', 35)->setEmptyValue('@')->addCondition(Form::FILLED)->addRule(Form::EMAIL, 'Incorrect E-mail Address'); // ... then check email // group Shipping address $form->addGroup('Shipping address')->setOption('embedNext', TRUE); $form->addCheckbox('send', 'Ship to address')->addCondition(Form::EQUAL, TRUE)->toggle('sendBox'); // toggle div #sendBox // subgroup $form->addGroup()->setOption('container', Html::el('div')->id('sendBox')); $form->addText('street', 'Street:', 35); $form->addText('city', 'City:', 35)->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
/** * Nette\Forms example 6 * * - using naming containers */ require '../../Nette/loader.php'; /*use Nette\Forms\Form;*/ /*use Nette\Debug;*/ Debug::enable(); $countries = array('Select your country', 'Europe' => array('CZ' => 'Czech Republic', 'FR' => 'France', 'DE' => 'Germany', 'GR' => 'Greece', 'HU' => 'Hungary', 'IE' => 'Ireland', 'IT' => 'Italy', 'NL' => 'Netherlands', 'PL' => 'Poland', 'SK' => 'Slovakia', 'ES' => 'Spain', 'CH' => 'Switzerland', 'UA' => 'Ukraine', 'GB' => 'United Kingdom'), 'AU' => 'Australia', 'CA' => 'Canada', 'EG' => 'Egypt', 'JP' => 'Japan', 'US' => 'United States', '?' => 'other'); $sex = array('m' => 'male', 'f' => 'female'); // Step 1: Define form with validation rules $form = new Form(); // group First person $form->addGroup('First person'); $sub = $form->addContainer('first'); $sub->addText('name', 'Your name:', 35); $sub->addText('email', 'E-mail:', 35); $sub->addText('street', 'Street:', 35); $sub->addText('city', 'City:', 35); $sub->addSelect('country', 'Country:', $countries); // group Second person $form->addGroup('Second person'); $sub = $form->addContainer('second'); $sub->addText('name', 'Your name:', 35); $sub->addText('email', 'E-mail:', 35); $sub->addText('street', 'Street:', 35); $sub->addText('city', 'City:', 35); $sub->addSelect('country', 'Country:', $countries); // group for buttons
* * - form definition using fluent interfaces * - form groups usage * - default rendering */ require '../../Nette/loader.php'; /*use Nette\Forms\Form;*/ /*use Nette\Debug;*/ /*use Nette\Web\Html;*/ Debug::enable(); $countries = array('Select your country', 'Europe' => array('CZ' => 'Czech Republic', 'FR' => 'France', 'DE' => 'Germany', 'GR' => 'Greece', 'HU' => 'Hungary', 'IE' => 'Ireland', 'IT' => 'Italy', 'NL' => 'Netherlands', 'PL' => 'Poland', 'SK' => 'Slovakia', 'ES' => 'Spain', 'CH' => 'Switzerland', 'UA' => 'Ukraine', 'GB' => 'United Kingdom'), 'AU' => 'Australia', 'CA' => 'Canada', 'EG' => 'Egypt', 'JP' => 'Japan', 'US' => 'United States', '?' => 'other'); $sex = array('m' => 'male', 'f' => 'female'); // Step 1: Define form with validation rules $form = new Form(); // group Personal data $form->addGroup('Personal data')->setOption('description', 'We value your privacy and we ensure that the information you give to us will not be shared to other entities.'); $form->addText('name', 'Your name:', 35)->addRule(Form::FILLED, 'Enter your name'); $form->addText('age', 'Your age:', 5)->addRule(Form::FILLED, 'Enter your age')->addRule(Form::INTEGER, 'Age must be numeric value')->addRule(Form::RANGE, 'Age must be in range from %d to %d', array(10, 100)); $form->addRadioList('gender', 'Your gender:', $sex); $form->addText('email', 'E-mail:', 35)->setEmptyValue('@')->addCondition(Form::FILLED)->addRule(Form::EMAIL, 'Incorrect E-mail Address'); // ... then check email // group Shipping address $form->addGroup('Shipping address')->setOption('embedNext', TRUE); $form->addCheckbox('send', 'Ship to address')->addCondition(Form::EQUAL, TRUE)->toggle('sendBox'); // toggle div #sendBox // subgroup $form->addGroup()->setOption('container', Html::el('div')->id('sendBox')); $form->addText('street', 'Street:', 35); $form->addText('city', 'City:', 35)->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
echo $form->display(); if ($form->validate()) { $sql = 'describe ' . $_REQUEST['table']; $cols = Database::singleton()->query_fetch_all($sql); @($table_form = new Form()); @$table_form->addElement('hidden', 'table'); $table_form->setConstants(array('table' => $_REQUEST['table'])); @$table_form->addElement('text', 'class_name', 'Class Name'); foreach ($cols as $col) { $group = array(); $group[] = @$table_form->createElement('text', 'name', 'Name of ' . $col['Field']); if ($col['Key'] == "PRI") { $priKey = $col['Field']; } $group[] = @$table_form->createElement('select', 'type', 'Type of ' . $col['Field'], $classnames); $table_form->addGroup($group, 'cols[' . $col['Field'] . ']', 'Name of ' . $col['Field'], ' '); } @$table_form->addElement('submit', 'submit', 'Submit'); echo $table_form->display(); if ($table_form->validate() && @$_REQUEST['class_name']) { $smarty->assign('class', @$_REQUEST['class_name']); $cols = array(); foreach ($_REQUEST['cols'] as $key => $col) { $cols[$key] = $col['name']; } $smarty->assign('cols', @$_REQUEST['cols']); $smarty->assign('table', @$_REQUEST['table']); $smarty->assign('primary_key', $priKey); $php = $smarty->fetch('codegen.tpl'); echo '<div style="background-color: #fff; border-left: 2px solid #000; padding-left: 10px; position: absolute; top: 5px; left: 500px;">'; echo '<h1>Class Code <a href="#" onclick="new Effect.toggle(\'code\'); return false;">[+]</a></h1>';