public function testSetValueFromSubmitDataSource() { $form = new HTML_QuickForm2('image', 'post', null, false); $foo = $form->appendChild(new HTML_QuickForm2_Element_InputImage('foo')); $bar = $form->appendChild(new HTML_QuickForm2_Element_InputImage('bar[idx]')); $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array('foo_x' => '1234', 'foo_y' => '5678', 'bar' => array('idx' => array('98', '76'))))); $this->assertEquals(array('x' => '12', 'y' => '34'), $foo->getValue()); $this->assertEquals(array('x' => '56', 'y' => '78'), $bar->getValue()); $foo->setAttribute('disabled'); $this->assertNull($foo->getValue()); }
public function testSetValueFromSubmitDataSource() { $form = new HTML_QuickForm2('submit', 'post', null, false); $foo = $form->appendChild(new HTML_QuickForm2_Element_InputSubmit('foo')); $bar = $form->appendChild(new HTML_QuickForm2_Element_InputSubmit('bar')); $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array('foo' => 'Default for foo', 'bar' => 'Default for bar'))); $this->assertEquals('A button clicked', $foo->getValue()); $this->assertNull($bar->getValue()); $foo->setAttribute('disabled'); $this->assertNull($foo->getValue()); }
/** * Sets the default action invoked on page-form submit * * This is necessary as the user may just press Enter instead of * clicking one of the named submit buttons and then no action name will * be passed to the script. * * @param string Default action name * @param string Path to a 1x1 transparent GIF image * @return object Returns the image input used for default action */ public function setDefaultAction($actionName, $imageSrc = '') { require_once 'HTML/QuickForm2/Controller/DefaultAction.php'; if (0 == count($this->form)) { $image = $this->form->appendChild(new HTML_QuickForm2_Controller_DefaultAction($this->getButtonName($actionName), array('src' => $imageSrc))); // replace the existing DefaultAction } elseif ($image = $this->form->getElementById('_qf_default')) { $image->setName($this->getButtonName($actionName))->setAttribute('src', $imageSrc); // Inject the element to the first position to improve chances that // it ends up on top in the output } else { $it = $this->form->getIterator(); $it->rewind(); $image = $this->form->insertBefore(new HTML_QuickForm2_Controller_DefaultAction($this->getButtonName($actionName), array('src' => $imageSrc)), $it->current()); } return $image; }
public function testSelectMultipleNoOptionsSelectedOnSubmit() { $options = array('1' => 'Option 1', '2' => 'Option 2'); $formPost = new HTML_QuickForm2('multiple', 'post', null, false); $single1 = $formPost->appendChild(new HTML_QuickForm2_Element_Select('single1', null, array('options' => $options))); $single2 = $formPost->appendChild(new HTML_QuickForm2_Element_Select('single2', null, array('options' => $options))); $multiple = $formPost->appendChild(new HTML_QuickForm2_Element_Select('mult', array('multiple'), array('options' => $options))); $this->assertEquals('1', $single1->getValue()); $this->assertNull($single2->getValue()); $this->assertNull($multiple->getValue()); $formPost->addDataSource(new HTML_QuickForm2_DataSource_Array(array('single1' => '2', 'single2' => '2', 'mult' => array('1', '2')))); $this->assertEquals('1', $single1->getValue()); $this->assertEquals('2', $single2->getValue()); $this->assertNull($multiple->getValue()); $formGet = new HTML_QuickForm2('multiple2', 'get', null, false); $multiple2 = $formGet->appendChild(new HTML_QuickForm2_Element_Select('mult2', array('multiple'), array('options' => $options))); $this->assertNull($multiple2->getValue()); $formGet->addDataSource(new HTML_QuickForm2_DataSource_Array(array('mult2' => array('1', '2')))); $this->assertEquals(array('1', '2'), $multiple2->getValue()); }
public function testCannotAddToContainer() { $form1 = new HTML_QuickForm2('form1'); $form2 = new HTML_QuickForm2('form2'); try { $form1->appendChild($form2); } catch (HTML_QuickForm2_Exception $e) { return; } $this->fail('Expected HTML_QuickForm2_Exception was not thrown'); }
/** * If a form contained only non-submit data sources, 'checked' attribute was unlikely to be ever cleared */ public function testCheckedAttributeShouldBeCleared() { $formNoSubmit = new HTML_QuickForm2('neverSubmitted'); $box1 = new HTML_QuickForm2_Element_InputCheckbox('box1', 'checked'); $box2 = new HTML_QuickForm2_Element_InputCheckbox('box2'); $formNoSubmit->appendChild($box1); $formNoSubmit->appendChild($box2); $this->assertNotNull($box1->getAttribute('checked')); $this->assertNull($box2->getAttribute('checked')); $formNoSubmit->addDataSource(new HTML_QuickForm2_DataSource_Array(array('box2' => true))); $this->assertNotNull($box2->getAttribute('checked')); $this->assertNull($box1->getAttribute('checked')); $box2->setName('box3'); $this->assertNull($box2->getAttribute('checked')); }
/** * edit edits the entry * * @return string html-string */ private function edit() { // smarty-templates $sD = new JudoIntranetSmarty(); // check rights if (Rights::check_rights($this->get('cid'), 'calendar')) { // check cid and pid given if ($this->get('cid') !== false && $this->get('pid') !== false) { // check cid and pid exists if (Calendar::check_id($this->get('cid')) && Preset::check_preset($this->get('pid'), 'calendar')) { // pagecaption $this->tpl->assign('pagecaption', parent::lang('class.AnnouncementView#page#caption#edit')); // prepare return $return = ''; // get preset $preset = new Preset($this->get('pid'), 'calendar', $this->get('cid')); // get fields $fields = $preset->get_fields(); // formular $form = new HTML_QuickForm2('edit_announcement', 'post', array('name' => 'edit_announcement', 'action' => 'announcement.php?id=edit&cid=' . $this->get('cid') . '&pid=' . $this->get('pid'))); // values $datasource = array(); foreach ($fields as $field) { // read values $field->read_value(); // check type if ($field->get_type() == 'text') { // check defaults $datasource['calendar-' . $field->get_id()]['manual'] = ''; $datasource['calendar-' . $field->get_id()]['defaults'] = 0; if ($field->get_value() == '') { $datasource['calendar-' . $field->get_id()]['defaults'] = 'd' . $field->get_defaults(); } else { $datasource['calendar-' . $field->get_id()]['manual'] = $field->get_value(); } } elseif ($field->get_type() == 'dbhierselect') { // explode value list($v_first, $v_second) = explode('|', $field->get_value(), 2); // set values $datasource['calendar-' . $field->get_id()][0] = $v_first; $datasource['calendar-' . $field->get_id()][1] = $v_second; } elseif ($field->get_type() == 'dbselect') { // check multiple if (strpos($field->get_value(), '|') !== false) { // separate value $values = explode('|', $field->get_value()); foreach ($values as $i => $value) { $datasource['calendar-' . $field->get_id()][$i] = $value; } } else { $datasource['calendar-' . $field->get_id()] = $field->get_value(); } } else { $datasource['calendar-' . $field->get_id()] = $field->get_value(); } } $form->addDataSource(new HTML_QuickForm2_DataSource_Array($datasource)); // renderer $renderer = HTML_QuickForm2_Renderer::factory('default'); $renderer->setOption('required_note', parent::lang('class.AnnouncementView#entry#form#requiredNote')); // generate field-quickform and add to form foreach ($fields as $field) { // generate quickform $field_id = $field->read_quickform(array(), true); // check $field_id if ($field_id != '' && $field->get_type() == 'date') { // smarty $sD->assign('elementid', $field_id . '-0'); $sD->assign('dateFormat', 'yy-mm-dd'); $sD->assign('dateValue', $field->get_value()); $this->add_jquery($sD->fetch('smarty.js-datepicker.tpl')); } // add to form $form->appendChild($field->get_quickform()); } // submit-button $form->addSubmit('submit', array('value' => parent::lang('class.AnnouncementView#edit#form#submitButton'))); // validate if ($form->validate()) { // get calendar $calendar = new Calendar($this->get('cid')); // prepare marker-array $announcement = array('version' => date('dmy')); // get data $data = $form->getValue(); // insert values foreach ($fields as $field) { // values to db $field->value($data[$field->get_table() . '-' . $field->get_id()]); $field->write_db('update'); } // add calendar-fields to array $calendar->add_marks($announcement); // add field-names and -values to array $preset->add_marks($announcement); // get field name and value $values = array(); foreach ($fields as $field) { $values[] = $field->value_to_html(); } // smarty $sAe = new JudoIntranetSmarty(); $sAe->assign('a', $announcement); for ($i = 0; $i < count($values); $i++) { if (preg_match('/\\{\\$a\\..*\\}/U', $values[$i]['value'])) { $values[$i]['value'] = $sAe->fetch('string:' . $values[$i]['value']); } } $sAe->assign('v', $values); return $sAe->fetch('smarty.announcement.edit.tpl'); } else { return $form->render($renderer); } } else { // error $errno = $GLOBALS['Error']->error_raised('WrongParams', 'entry:cid_or_pid', 'cid_or_pid'); $GLOBALS['Error']->handle_error($errno); return $GLOBALS['Error']->to_html($errno); } } else { // error $errno = $GLOBALS['Error']->error_raised('MissingParams', 'entry:cid_or_pid', 'cid_or_pid'); $GLOBALS['Error']->handle_error($errno); return $GLOBALS['Error']->to_html($errno); } } else { // error $errno = $GLOBALS['Error']->error_raised('NotAuthorized', 'entry:' . $this->get('id'), $this->get('id')); $GLOBALS['Error']->handle_error($errno); return $GLOBALS['Error']->to_html($errno); } }
public function testCheckboxUncheckedOnSubmit() { $formPost = new HTML_QuickForm2('boxed', 'post', null, false); $box1 = $formPost->appendChild(new HTML_QuickForm2_Element_InputCheckbox('box1')); $box2 = $formPost->appendChild(new HTML_QuickForm2_Element_InputCheckbox('box2')); $this->assertEquals('1', $box1->getValue()); $this->assertNull($box2->getValue()); $formPost->addDataSource(new HTML_QuickForm2_DataSource_Array(array('box2' => '1'))); $this->assertEquals('1', $box1->getValue()); $this->assertNull($box2->getValue()); $formGet = new HTML_QuickForm2('boxed2', 'get', null, false); $box3 = $formGet->appendChild(new HTML_QuickForm2_Element_InputCheckbox('box3')); $this->assertNull($box3->getValue()); $formGet->addDataSource(new HTML_QuickForm2_DataSource_Array(array('box3' => '1'))); $this->assertEquals('1', $box3->getValue()); }
/** * File should check that the form has POST method, set enctype to multipart/form-data * @see http://pear.php.net/bugs/bug.php?id=16807 */ public function testRequest16807() { $form = new HTML_QuickForm2('broken', 'get'); try { $form->addFile('upload', array('id' => 'upload')); $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); } catch (HTML_QuickForm2_InvalidArgumentException $e) { } $group = HTML_QuickForm2_Factory::createElement('group', 'fileGroup'); $group->addFile('upload', array('id' => 'upload')); try { $form->appendChild($group); $this->fail('Expected HTML_QuickForm2_InvalidArgumentException was not thrown'); } catch (HTML_QuickForm2_InvalidArgumentException $e) { } $post = new HTML_QuickForm2('okform', 'post'); $this->assertNull($post->getAttribute('enctype')); $post->addFile('upload'); $this->assertEquals('multipart/form-data', $post->getAttribute('enctype')); }
public function testUpdateValueOnNameChange() { $form = new HTML_QuickForm2('form1'); $elFoo = $form->appendChild(new HTML_QuickForm2_ElementImpl('foo')); $elFoo->setName('fooReborn'); $this->assertEquals('another value', $elFoo->getValue()); }
public function testBug15708() { $form = new HTML_QuickForm2('bug15708'); $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array('aRadio' => 1))); $aRadio = $form->appendChild(new HTML_QuickForm2_Element_InputCheckable('aRadio'))->setAttribute('value', 1); $this->assertContains('checked', $aRadio->__toString()); }
/** * take creates the form to take an inventoryitem from somebody else * * @param int $did entry-id for the inventoryitem * @return string html-string with the form */ private function take($did) { // check rights if (Rights::check_rights($did, 'inventory')) { // pagecaption $this->tpl->assign('pagecaption', parent::lang('class.InventoryView#page#caption#take')); // get db-object $db = Db::newDb(); // get inventory-object $inventory = new Inventory($did); // check owned if ($inventory->get_owned() == 'given') { // smarty-template $sT = new JudoIntranetSmarty(); // prepare return $return = ''; // get preset $preset = $inventory->get_preset(); // get fields $fields = $preset->get_fields(); // add headline $sT->assign('caption', parent::lang('class.InventoryView#take#page#headline') . ': ' . $inventory->get_name() . ' (' . $inventory->get_inventory_no() . ')'); // add take from $movements = Inventory::movement_last_row($db, $inventory->get_id(), 'user_id', 2); $user = new User(); $user->change_user($movements[1], false, 'id'); $sT->assign('takefrom', parent::lang('class.InventoryView#take#page#TakeFrom') . ': ' . $user->get_userinfo('name')); // add accessory info $sT->assign('accessoryinfo', parent::lang('class.InventoryView#take#page#accessory.required')); // formular $form = new HTML_QuickForm2('inventory_take', 'post', array('name' => 'inventory_take', 'action' => 'inventory.php?id=take&did=' . $this->get('did'))); // renderer $renderer = HTML_QuickForm2_Renderer::factory('default'); $renderer->setOption('required_note', parent::lang('class.InventoryView#entry#form#requiredNote')); // generate field-quickform and add to form foreach ($fields as $field) { // check if given if ($inventory->movement_last_accessories($field) === true || $field->get_type() == 'text') { // generate quickform $field->read_quickform(); } else { // generate quickform $field->read_quickform(array('disabled' => 'disabled')); } // add to form $form->appendChild($field->get_quickform()); } // submit-button $form->addSubmit('submit', array('value' => parent::lang('class.InventoryView#take#form#submitButton'))); // validate if ($form->validate()) { // values $values = $form->getValue(); // write to db $insert_id = $this->movement_to_db('taken', $inventory->get_id(), $_SESSION['user']->userid()); // accessory to db $this->values_to_db($insert_id, $fields, $values); // headline $sT->assign('action', $inventory->get_name() . ' (' . $inventory->get_inventory_no() . ') ' . parent::lang('class.InventoryView#take#page#headline.taken')); // accessory $sT->assign('accessoryaction', parent::lang('class.InventoryView#take#page#accessory.taken')); // walk through fields $data = array(); foreach ($fields as $field) { // check value if (isset($values['inventory-' . $field->get_id()])) { $field_value = $values['inventory-' . $field->get_id()]; } else { $field_value = 0; } // return field and value as HTML $field->value($field_value); $data[] = $field->value_to_html(); } $sT->assign('form', ''); $sT->assign('data', $data); } else { $sT->assign('form', $form->render($renderer)); } // return return $sT->fetch('smarty.inventory.takegive.tpl'); } else { // error $errno = $GLOBALS['Error']->error_raised('NotGivenTo', $this->get('id'), $did); $GLOBALS['Error']->handle_error($errno); return $GLOBALS['Error']->to_html($errno); } } else { // error $errno = $GLOBALS['Error']->error_raised('NotAuthorized', $this->get('id'), $did); $GLOBALS['Error']->handle_error($errno); return $GLOBALS['Error']->to_html($errno); } }
/** * edit_row edits the row for the given rid * * @param string $table table to work on * @param int $rid id of row to edit * @return string HTML-string for the form or message */ private function edit_row($table, $rid) { // prepare return $return = ''; // get url-parameters $link = ''; if ($table == 'defaults') { $link = 'administration.php?id=' . $this->get('id'); } else { $link = 'administration.php?id=' . $this->get('id') . '&field=' . $table; } // get db-object $db = Db::newDb(); // prepare statement $sql = "SELECT * FROM {$table} WHERE id={$rid}"; // execute $result = $db->query($sql); // fetch result $row = $result->fetch_array(MYSQL_ASSOC); // prepare form $form = new HTML_QuickForm2('edit_field', 'post', array('name' => 'edit_field', 'action' => $link . '&action=edit&rid=' . $rid)); // renderer $renderer = HTML_QuickForm2_Renderer::factory('default'); $renderer->setOption('required_note', parent::lang('class.AdministrationView#edit_row#form#requiredNote')); // get values and fields $i = 0; $datasource = array(); $fields = array(); foreach ($row as $col => $value) { // check translation $translated_name = ''; if (parent::lang('class.AdministrationView#tableRows#name#' . $col) != "class.AdministrationView#tableRows#name#{$col} not translated") { $translated_col = parent::lang('class.AdministrationView#tableRows#name#' . $col); } else { $translated_col = $col; } // check category if ($col == 'category') { // get options $cat_sql = "SELECT id,name FROM category WHERE valid=1"; $cat_result = $db->query($cat_sql); $options = array('--'); while (list($id, $name) = $cat_result->fetch_array(MYSQL_NUM)) { $options[$id] = $name; } // add value $datasource[$col] = $value; // select $field = $form->addElement('select', $col, array()); $field->setLabel($translated_col . ':'); // load options $field->loadOptions($options); // add rules if ($table == 'defaults') { $field->addRule('required', parent::lang('class.AdministrationView#edit_row#rule#requiredSelect')); $field->addRule('callback', parent::lang('class.AdministrationView#edit_row#rule#checkSelect'), array($this, 'callback_check_select')); } } else { // check id or valid if ($col != 'id' && $col != 'valid') { // get fieldconfig // 252 = text, 253 = varchar; 1 = tinyint(boolean); 3 = int $field_config = $result->fetch_field_direct($i); // add value $datasource[$col] = $value; // add field $field = null; // check type if ($field_config->type == 252) { // textarea $field = HTML_QuickForm2_Factory::createElement('textarea', $col, array()); $field->setLabel($translated_col . ':'); // add rule $field->addRule('regex', parent::lang('class.AdministrationView#edit_row#rule#regexp.allowedChars') . ' [' . $_SESSION['GC']->get_config('textarea.desc') . ']', $_SESSION['GC']->get_config('textarea.regexp')); // required if ($table == 'defaults') { $field->addRule('required', parent::lang('class.AdministrationView#edit_row#rule#required')); } } elseif ($field_config->type == 253 || $field_config->type == 3) { // input $field = HTML_QuickForm2_Factory::createElement('text', $col, array()); $field->setLabel($translated_col . ':'); // add rule $field->addRule('regex', parent::lang('class.AdministrationView#edit_row#rule#regexp.allowedChars') . ' [' . $_SESSION['GC']->get_config('textarea.desc') . ']', $_SESSION['GC']->get_config('textarea.regexp')); // required if ($table == 'defaults') { $field->addRule('required', parent::lang('class.AdministrationView#edit_row#rule#required')); } } elseif ($field_config->type == 1) { // input $field = HTML_QuickForm2_Factory::createElement('checkbox', $col, array()); $field->setLabel($translated_col . ':'); } $fields[] = $field; } } // increment field-counter $i++; } // add datasource $form->addDataSource(new HTML_QuickForm2_DataSource_Array($datasource)); // add fields foreach ($fields as $field) { $form->appendChild($field); } // submit-button $form->addSubmit('submit', array('value' => parent::lang('class.AdministrationView#edit_row#form#submitButton'))); // validate if ($form->validate()) { // set output $return .= $this->p(' class="edit_caption"', parent::lang('class.AdministrationView#edit_row#caption#done') . ': "' . $rid . '"'); // get data $data = $form->getValue(); // prepare statement $sql = "UPDATE {$table} SET "; foreach ($data as $field => $value) { // check translation $translated_field = ''; if (parent::lang('class.AdministrationView#tableRows#name#' . $field) != "class.AdministrationView#tableRows#name#{$field} not translated") { $translated_field = parent::lang('class.AdministrationView#tableRows#name#' . $field); } else { $translated_field = $field; } // check field if (substr($field, 0, 5) != '_qf__' && $field != 'submit') { // add fields to sql $sql .= "{$field}='{$value}', "; // add fields to output $return .= $this->p('', "{$translated_field} = '" . nl2br(htmlentities(utf8_decode($value))) . "'"); } } $sql = substr($sql, 0, -2); $sql .= " WHERE id={$rid}"; // execute $result = $db->query($sql); // add table content $return .= $this->list_table_content($table, $this->get('page')); } else { $return .= $this->p('', parent::lang('class.AdministrationView#edit_row#caption#edit') . ': "' . $rid . '"'); $return .= $form->render($renderer); } // return return $return; }
public function testServerSideValidationErrors() { $ds = new HTML_QuickForm2_DataSource_Session(array('foo' => array('', 'blah', ''))); $form = new HTML_QuickForm2('repeatValidate'); $form->addDataSource($ds); $fieldset = new HTML_QuickForm2_Container_Fieldset(); $text = new HTML_QuickForm2_Element_InputText('foo'); $repeat = new HTML_QuickForm2_Container_Repeat(null, null, array('prototype' => $fieldset)); $fieldset->appendChild($text); $form->appendChild($repeat); $text->addRule('required', 'a message'); $this->assertFalse($form->validate()); $ary = $repeat->render(HTML_QuickForm2_Renderer::factory('array'))->toArray(); $this->assertEquals('a message', $ary['elements'][1]['elements'][0]['error']); $this->assertArrayNotHasKey('error', $ary['elements'][2]['elements'][0]); $this->assertEquals('a message', $ary['elements'][3]['elements'][0]['error']); $text->setId('blah-:idx:'); $ary = $repeat->render(HTML_QuickForm2_Renderer::factory('array'))->toArray(); $this->assertEquals('a message', $ary['elements'][1]['elements'][0]['error']); $this->assertArrayNotHasKey('error', $ary['elements'][2]['elements'][0]); $this->assertEquals('a message', $ary['elements'][3]['elements'][0]['error']); }
public function testErrorMessageLocalization() { $form = new HTML_QuickForm2('upload', 'post', null, false); $local = $form->appendChild(new HTML_QuickForm2_Element_InputFile('local', array(), array('language' => 'zz', 'errorMessages' => array('zz' => array(UPLOAD_ERR_CANT_WRITE => 'Blah-blah-blah'))))); $this->assertFalse($form->validate()); $this->assertEquals('Blah-blah-blah', $local->getError()); }
/** * If data source contains explicitly provided null values, those should be used * @link http://pear.php.net/bugs/bug.php?id=20295 */ public function testBug20295() { $form = new HTML_QuickForm2('bug20295'); $el = $form->appendChild(new HTML_QuickForm2_ElementImpl('foo')); $el->setValue('not empty'); $form->addDataSource(new HTML_QuickForm2_DataSource_Array(array('foo' => null))); $this->assertNull($el->getValue()); }
public function testInputCheckable() { $form = new HTML_QuickForm2('filters', 'post', null, false); $check = $form->appendChild(new HTML_QuickForm2_Element_InputCheckable('bar')); $check->setAttribute('value', 'VALUE'); $check->addFilter('strtolower'); $this->assertEquals('value', $check->getValue()); // in order to be set, the value must be equal to the one in // the value attribute $check->setValue('value'); $this->assertNull($check->getValue()); $check->setValue('VALUE'); $this->assertEquals('value', $check->getValue()); }