/** * Set the field values. Optionally, you can apply filters * to the passed values via callbacks and their parameters * * @param array $values * @param array $filters * @return \Pop\Form\Form */ public function setFieldValues(array $values = null, $filters = null) { // Filter values if passed if (null !== $values && null !== $filters) { $values = $this->filterValues($values, $filters); } // Loop through the initial fields values and build the fields // based on the _initFieldsValues property. if (count($this->initFieldsValues) > 0) { // If the fields are a group of fields $keys = array_keys($this->initFieldsValues); if (is_numeric($keys[0])) { $fields = array(); foreach ($this->initFieldsValues as $ary) { $k = array_keys($ary); if (isset($k[0])) { $this->groups[] = $k[0]; } $fields = array_merge($fields, $ary); } $this->initFieldsValues = $fields; } foreach ($this->initFieldsValues as $name => $field) { if (is_array($field) && isset($field['type'])) { $type = $field['type']; $label = isset($field['label']) ? $field['label'] : null; $required = isset($field['required']) ? $field['required'] : null; $attributes = isset($field['attributes']) ? $field['attributes'] : null; $validators = isset($field['validators']) ? $field['validators'] : null; $expire = isset($field['expire']) ? $field['expire'] : 300; $captcha = isset($field['captcha']) ? $field['captcha'] : null; $data = isset($field['data']) ? $field['data'] : null; if ($type == 'file') { $this->hasFile = true; } if (isset($field['error'])) { $error = array('container' => 'div', 'attributes' => array('class' => 'error'), 'pre' => false); foreach ($field['error'] as $key => $value) { if ($key != 'pre') { $error['container'] = $key; $error['attributes'] = $value; } else { if ($key == 'pre') { $error['pre'] = $value; } } } } else { $error = null; } if (null !== $values && array_key_exists($name, $values)) { if ($type == 'checkbox' || $type == 'radio' || $type == 'select') { $value = isset($field['value']) ? $field['value'] : null; $marked = $values[$name]; } else { $value = $values[$name]; $marked = isset($field['marked']) ? $field['marked'] : null; } } else { $value = isset($field['value']) ? $field['value'] : null; $marked = isset($field['marked']) ? $field['marked'] : null; } // Initialize the form element. switch (strtolower($type)) { case 'checkbox': $elem = new Element\Checkbox($name, $value, $marked); break; case 'radio': $elem = new Element\Radio($name, $value, $marked); break; case 'select': $elem = new Element\Select($name, $value, $marked, null, $data); break; case 'textarea': $elem = new Element\Textarea($name, $value, $marked); break; case 'csrf': $elem = new Element\Csrf($name, $value, $expire); break; case 'captcha': $elem = new Element\Captcha($name, $value, $expire, $captcha); break; default: $elem = new Element($type, $name, $value, $marked); } // Set the label. if (null !== $label) { $elem->setLabel($label); } // Set if required. if (null !== $required) { $elem->setRequired($required); } // Set if error display. if (null !== $error) { $elem->setErrorDisplay($error['container'], $error['attributes'], $error['pre']); } // Set any attributes. if (null !== $attributes) { foreach ($attributes as $a => $v) { $elem->setAttributes($a, $v); } } // Set any validators. if (null !== $validators) { if (is_array($validators)) { foreach ($validators as $val) { $elem->addValidator($val); } } else { $elem->addValidator($validators); } } $this->addElements($elem); } } // Else, set the passed values to the elements that // are already added to the form object } else { $fields = $this->getElements(); if (null !== $values && count($fields) > 0) { foreach ($fields as $field) { $fieldName = str_replace('[]', '', $field->getName()); if (isset($values[$fieldName])) { // If a multi-value form element if ($field->hasChildren()) { $field->setMarked($values[$fieldName]); $this->fields[$fieldName] = $values[$fieldName]; // Loop through the field's children if ($field->hasChildren()) { $children = $field->getChildren(); foreach ($children as $key => $child) { // If checkbox or radio if ($child->getAttribute('type') == 'checkbox' || $child->getAttribute('type') == 'radio') { if (is_array($field->getMarked()) && in_array($child->getAttribute('value'), $field->getMarked())) { $field->getChild($key)->setAttributes('checked', 'checked'); } else { if ($child->getAttribute('value') == $field->getMarked()) { $field->getChild($key)->setAttributes('checked', 'checked'); } } // If select option } else { if ($child->getNodeName() == 'option') { if (is_array($field->getMarked()) && in_array($child->getAttribute('value'), $field->getMarked())) { $field->getChild($key)->setAttributes('selected', 'selected'); } else { if ($child->getAttribute('value') == $field->getMarked()) { $field->getChild($key)->setAttributes('selected', 'selected'); } } } } } } // Else, if a single-value form element } else { $field->setValue($values[$fieldName]); $this->fields[$fieldName] = $values[$fieldName]; if ($field->getNodeName() == 'textarea') { $field->setNodeValue($values[$fieldName]); } else { $field->setAttributes('value', $values[$fieldName]); } } } } } } if (null !== $this->errorDisplay) { $this->setErrorDisplay($this->errorDisplay['container'], $this->errorDisplay['attributes'], $this->errorDisplay['pre']); } return $this; }
public function testRenderWithTemplate() { $e = new Element('text', 'username', 'Username'); $e->setLabel('Username'); $s = new Element('submit', 'submit', 'Submit'); $f = new Form('/submit', 'post'); $f->addElements(array($e, $s)); $f->setTemplate("[{username}] [{submit}]"); $f->username = '******'; $form = $f->render(true); $this->assertContains('<form ', $form); $this->assertEquals('My Username', $f->username); }
if (strlen($value) < 6) { return 'The password value must be greater than or equal to 6'; } } } $form = new Form($_SERVER['PHP_SELF'], 'post', null, ' '); $username = new Element('text', 'username', 'Username here...'); $username->setLabel('Username:'******'size', 40)->addValidator(new Validator\AlphaNumeric())->addValidator(function ($value) { if (strlen($value) < 6) { return 'The username value must be greater than or equal to 6.'; } }); $email = new Element('text', 'email'); $email->setLabel('Email:')->setRequired(true)->setAttributes('size', 40)->addValidator(new Validator\Email()); $password = new Element('password', 'password'); $password->setLabel('Password:'******'size', 40)->addValidator(array(new MyValidator(), 'validate')); $checkbox = new Checkbox('colors', array('Red' => 'Red', 'Green' => 'Green', 'Blue' => 'Blue')); $checkbox->setLabel('Colors:')->setRequired(true); $radio = new Radio('answer', array('Yes' => 'Yes', 'No' => 'No', 'Maybe' => 'Maybe')); $radio->setLabel('Answer:')->setRequired(true); $select = new Select('days', Select::DAYS_OF_WEEK); $select->setLabel('Day:'); $textarea = new Textarea('comments', 'Please type a comment...'); $textarea->setAttributes('rows', '5')->setAttributes('cols', '40')->setLabel('Comments:'); $submit = new Element('submit', 'submit', 'SUBMIT'); $submit->setAttributes('style', 'padding: 5px; border: solid 2px #000; background-color: #00f; color: #fff; font-weight: bold;'); $form->addElements(array($username, $email, $password, $checkbox, $radio, $select, $textarea, $submit)); if ($_POST) { $form->setFieldValues($_POST, array('strip_tags' => null, 'htmlentities' => array(ENT_QUOTES, 'UTF-8'))); if (!$form->isValid()) { $form->render();
public function testSetAndGetLabelAttributes() { $e = new Element('text', 'email'); $e->setLabel('Email:'); $e->setLabelAttributes(array('class' => 'label-class')); $attribs = $e->getLabelAttributes(); $this->assertEquals('label-class', $attribs['class']); }
/** * Set the label of the form element object. * * @param string $label * @return \Pop\Form\Element */ public function setLabel($label) { parent::setLabel($label); if (isset($this->token['captcha'])) { if (strpos($this->token['captcha'], '<img') === false && (strpos($this->token['captcha'], ' + ') !== false || strpos($this->token['captcha'], ' - ') !== false || strpos($this->token['captcha'], ' * ') !== false || strpos($this->token['captcha'], ' / ') !== false)) { $this->label = $this->label . '(' . str_replace(array(' * ', ' / '), array(' × ', ' ÷ '), $this->token['captcha'] . ')'); } else { $this->label = $this->label . $this->token['captcha']; } } return $this; }