public function editAction() { $this->view->messages = $this->_helper->flashMessenger->getMessages(); $testId = $this->_request->getParam('id'); // Creates form instance $testFrom = new TestForm(); $this->view->testForm = $testFrom; // Loads data from database $technologies = My_Model::get('Technologies')->fetchAll(); $seniorities = My_Model::get('Seniorities')->fetchAll(); // Fills form selects $testFrom->getElement('id_technologie')->setMultiOptions($this->transformTechnologies($technologies)); $testFrom->getElement('id_seniorita')->setMultiOptions($this->transformSeniorities($seniorities)); // Edit test page if (!empty($testId)) { $this->view->testId = $testId; $this->view->title = 'Edit Test'; $test = My_Model::get('Tests')->getById($testId); $testData = $test->get_data(); $testFrom->setDefaults($testData); // Loads questions with options $questions = $test->getQuestions(); $questionForms = array(); foreach ($questions as $q) { $questionForm = new QuestionForm(array('questionId' => $q->getid_otazka())); $questionForm->setName('q' . strval($q->getid_otazka())); $questionForm->setAction($this->view->url(array('controller' => 'test', 'action' => 'save-question', 'testId' => $testId, 'questionId' => $q->getid_otazka()), 'default', true)); $questionForms[] = $questionForm; } $newQuestionForm = new QuestionForm(array('count' => 3)); $newQuestionForm->setName('new'); $newQuestionForm->setAction($this->view->url(array('controller' => 'test', 'action' => 'save-question', 'testId' => $testId), 'default', true)); $questionForms[] = $newQuestionForm; $this->view->questionForms = $questionForms; } else { $this->view->title = 'Add new Test'; } // ########################### POST ########################### // Handles form submission if ($this->_request->isPost()) { if ($testFrom->isValid($this->_request->getPost())) { $formValues = $testFrom->getValues(); $test; // Editing existing test if (!empty($testId)) { $test = My_Model::get('Tests')->getById($testId); } else { date_default_timezone_set('UTC'); $formValues['datum_vytvoreni'] = date("Y-n-j"); $formValues['id_kdo_vytvoril'] = $this->getUser()->id_uzivatel; $test = My_Model::get('Tests')->createRow(); } // Updates test object in DB $test->updateFromArray($formValues); $this->_helper->redirector->gotoRoute(array('controller' => 'test', 'action' => 'edit', 'id' => $test->id_test), 'default', true); } } }
function test_fieldsetbalance() { $form = new TestForm(); $form->addFieldsetOpen(); $form->addHTML('ignored'); $form->addFieldsetClose(); $form->balanceFieldsets(); $this->assertEquals(array('fieldsetopen', 'html', 'fieldsetclose'), $form->getElementTypeList()); $form = new TestForm(); $form->addHTML('ignored'); $form->addFieldsetClose(); $form->balanceFieldsets(); $this->assertEquals(array('fieldsetopen', 'html', 'fieldsetclose'), $form->getElementTypeList()); $form = new TestForm(); $form->addFieldsetOpen(); $form->addHTML('ignored'); $form->balanceFieldsets(); $this->assertEquals(array('fieldsetopen', 'html', 'fieldsetclose'), $form->getElementTypeList()); $form = new TestForm(); $form->addHTML('ignored'); $form->addFieldsetClose(); $form->addHTML('ignored'); $form->addFieldsetOpen(); $form->addHTML('ignored'); $form->balanceFieldsets(); $this->assertEquals(array('fieldsetopen', 'html', 'fieldsetclose', 'html', 'fieldsetopen', 'html', 'fieldsetclose'), $form->getElementTypeList()); }
/** * test default validation of DateInput and DateRangeInput */ public function testDateValidation() { // confirm when nothing posted nothing will be invalidated $this->assertTrue($this->form->validate()); $this->assertEmpty($this->form->oDate->getInvalidations()); // create invalid posted values /* * TODO commented these tests, because I currently can't properly simulate a POST of xsrftoken * $_POST = array('date' => '123-123', 'dateRange-from' => '1', 'dateRange-to' => '2', 'xsrftoken' => 'boo'); $form = new TestForm(); $this->assertFalse($form->validate()); $this->assertNotEmpty($form->oDate->getInvalidations()); $this->assertNotEmpty($form->oDateRange->getInvalidations()); */ }
/** * @dataProvider invalidDataProvider */ public function testHandleRequestWithExistingEntityAndInvalidData($data) { $initialName = 'Initial namn'; $this->entity->setId(1)->setName($initialName); $this->request->setData($data); $this->form->handleRequest($this->request); $this->assertFalse($this->form->isValid()); $this->assertEquals($this->entity->getName(), $initialName); }
public function editAction() { $record = null; $id = $this->_request->getParam('id'); $this->view->id = $id; if (!empty($id)) { $record = My_Model::get('Tests')->getById($id); } $form = new TestForm(); $form->setAction($this->_helper->url->url()); if ($record === null) { $this->view->title = 'Add Test'; } else { $this->view->title = 'Edit Test: ' . $record->getName() . ' [' . $record->getId() . ']'; $form->setModifyMode(); } if ($this->_request->isPost()) { if ($form->isValid($this->_request->getPost())) { $formValues = $form->getValues(); if ($record === null) { $record = My_Model::get('Tests')->createRow(); $record->updateFromArray($formValues, true); } else { $record->updateFromArray($formValues, false); } $this->_helper->flashMessenger->setNamespace("success")->addMessage("Your changes have been saved!"); $this->_helper->redirector->gotoUrl('/test/'); } } else { if ($record !== null) { $form->populate($record->toArray()); } } $this->view->form = $form; }
<?php // require the library require_once '../vendor/autoload.php'; // force English version test form $t = \WinkForm\Translation\Translator::getInstance(); $t->setLocale('en'); // typically your own autoloader should be configured to look in your forms directory require_once 'TestForm.php'; // create the form $form = new TestForm(); if ($form->isPosted() && !$form->validate()) { echo error('Can\'t process form, because not all fields are filled correctly'); } // for sake of the example the html and css is inline here ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Form Example</title> <!-- DateInput uses jquery ui date picker --> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"> <!-- ChainedDropdowns uses this jquery plugin --> <script src="js/jquery.chained.min.js"></script> <!-- This script is required to use the placeholder attribute in IE -->
function testMixedInit() { $app = new WebApplication(__DIR__ . "/app", "/"); $form = new TestForm(new TestEntity()); $elements = $form->getFormElements(); $this->assertArrayHasKey("formelement_key_1", $elements); $this->assertArrayHasKey("formelement_key_2", $elements); $this->assertArrayNotHasKey("non_formelement_key", $elements); $this->assertArrayHasKey("formelement_key_1b", $elements); $this->assertArrayHasKey("formelement_key_2b", $elements); $this->assertArrayNotHasKey("non_formelement_keyb", $elements); $this->assertInstanceOf("Loops\\Form\\Element\\Text", $elements["formelement_key_1b"]); $this->assertInstanceOf("Loops\\Form\\Element\\Number", $elements["formelement_key_2b"]); $this->assertInstanceOf("Loops\\Form\\Element\\Text", $elements["formelement_key_1"]); $this->assertInstanceOf("Loops\\Form\\Element\\Number", $elements["formelement_key_2"]); }
$dispatcher->connect('form.post_configure', array($listener, 'listen')); $dispatcher->connect('form.filter_values', array($listener, 'filter')); $dispatcher->connect('form.validation_error', array($listener, 'listen')); sfFormSymfony::setEventDispatcher($dispatcher); class TestForm extends sfFormSymfony { public function configure() { $this->setValidators(array('first_name' => new sfValidatorString(), 'last_name' => new sfValidatorString())); } } // ->__construct() $t->diag('->__construct()'); $listener->reset(); $form = new TestForm(); $t->is(count($listener->events), 1, '->__construct() notifies one event'); $t->is($listener->events[0][0]->getName(), 'form.post_configure', '->__construct() notifies the "form.post_configure" event'); // ->bind() $t->diag('->bind()'); $form = new TestForm(); $listener->reset(); $form->bind(array('first_name' => 'John', 'last_name' => 'Doe')); $t->is(count($listener->events), 1, '->bind() notifies one event when validation is successful'); $t->is($listener->events[0][0]->getName(), 'form.filter_values', '->bind() notifies the "form.filter_values" event'); $t->is_deeply($listener->events[0][1], array('first_name' => 'John', 'last_name' => 'Doe'), '->bind() filters the tainted values'); $form = new TestForm(); $listener->reset(); $form->bind(); $t->is(count($listener->events), 2, '->bind() notifies two events when validation fails'); $t->is($listener->events[1][0]->getName(), 'form.validation_error', '->bind() notifies the "form.validation_error" event'); $t->isa_ok($listener->events[1][0]['error'], 'sfValidatorErrorSchema', '->bind() notifies the error schema');
public function init() { parent::init(); $form1 = new TestFormRandom(); $form2 = new TestSubForm(); $this->addSubForms(array('form1' => $form1, 'form2' => $form2)); $this->moveElement('submit', 'after', 'form2'); }
function testPrePostValidation() { $f = new TestForm(); $this->assertFalse($f->validate(array('one' => 1)), 'Форма без полей не валидна'); $f->addInput('one'); $this->assertTrue($f->validate(array('one' => 1)), 'Форма валидна'); $this->assertFalse($f->validate(array('before' => 1)), 'Пре-валидация: форма невалидна'); $this->assertFalse($f->validate(array('after' => 1)), 'Пост-валидация: форма невалидна'); }
$reset = new AnewtFormControlButtonReset('reset'); $reset->set('label', 'Reset to default values'); $reset->set('help', 'Reset the values of the form fields to their original values'); $button_fieldset->add_control($reset); $extra = new AnewtFormControlButton('extra-button'); $extra->set('label', 'Extra button that does not do anything'); $button_fieldset->add_control($extra); $this->add_fieldset($button_fieldset); } } /* Show a page and test the form */ $page = new AnewtPage(); $page->set('title', 'Anewt form test page'); $page->add_stylesheet(ax_stylesheet($css)); $page->append(ax_h1('Test form')); $form = new TestForm(); assert('$form->get_control_value("text5") === "7"'); $form->set_control_value('text5', '8'); if ($form->autofill()) { if ($form->process()) { $page->append(ax_p('Form succesfully processed!')); } else { $page->append(ax_p('Error while processing form!')); } } else { $page->append(ax_p('Form not processed.')); } $fr = new AnewtFormRendererDefault(); $fr->set_form($form); $page->append(ax_h2('The form')); $page->append($fr);