/** * 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 testErrorDisplay() { $e = new Element('text', 'email'); $e->setErrorDisplay('h3', array('class' => 'error-class'), true); }