Pop\Form\Form::setFieldValues PHP Method

setFieldValues() public method

Set the field values. Optionally, you can apply filters to the passed values via callbacks and their parameters
public setFieldValues ( array $values = null, array $filters = null ) : Form
$values array
$filters array
return 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;
    }

Usage Example

Example #1
0
 /**
  * Set the field values
  *
  * @param  array $values
  * @return UserEmail
  */
 public function setFieldValues(array $values = null)
 {
     parent::setFieldValues($values);
     if ($_POST && null !== $this->email) {
         // Check for dupe email
         $user = null;
         $email = null;
         if (null !== $this->email) {
             $user = Table\Users::findBy(['username' => $this->email]);
             if (isset($user->id) && $this->id != $user->id) {
                 $this->getElement('email')->addValidator(new Validator\NotEqual($this->email, 'That email already exists.'));
             } else {
                 $email = Table\Users::findBy(['email' => $this->email]);
                 if (isset($email->id) && $this->id != $email->id) {
                     $this->getElement('email')->addValidator(new Validator\NotEqual($this->email1, 'That email already exists.'));
                 }
             }
         }
         // If existing user
         if ((int) $_POST['id'] > 0) {
             if (!empty($this->password1)) {
                 $this->getElement('password2')->setRequired(true)->addValidator(new Validator\Equal($this->password1, 'The passwords do not match.'));
             }
             // Else, if new user, check email and password matches
         } else {
             $this->getElement('password2')->setRequired(true)->addValidator(new Validator\Equal($this->password1, 'The passwords do not match.'));
         }
     }
     return $this;
 }
All Usage Examples Of Pop\Form\Form::setFieldValues