/** * Sets the fields to MODX placeholders * @return void */ public function setFieldsAsPlaceholders() { $fields = $this->dictionary->toArray(); /* better handling of checkbox values when input name is an array[] */ $fs = array(); /** @var mixed $v */ foreach ($fields as $k => $v) { if (is_array($v) && !isset($_FILES[$k])) { foreach ($v as $sk => $sv) { $fs[$k . '.' . $sk] = $this->convertMODXTags($sv); } $v = $this->modx->toJSON($v); } /* str_replace to prevent showing of placeholders */ $fs[$k] = $this->convertMODXTags($v); } $this->modx->setPlaceholders($fs, $this->config['placeholderPrefix']); }
/** * Validates an array of fields. Returns the field names and values, with * the field names stripped of their validators. * * The key names can be in this format: * * name:validator=param:anotherValidator:oneMoreValidator=`param` * * @access public * @param fiDictionary $dictionary * @param string $validationFields * @return array An array of field name => value pairs. */ public function validateFields(fiDictionary $dictionary, $validationFields = '', $validationSeparator = ',') { $keys = $dictionary->toArray(); $this->fields = $keys; /* process the list of fields that will be validated */ $validationFields = explode($validationSeparator, $validationFields); $fieldValidators = array(); foreach ($validationFields as $idx => $v) { $v = trim(ltrim($v), ' '); /* allow multi-line definitions */ $key = explode(':', $v); /* explode into list separated by : */ if (!empty($key[0])) { $field = $key[0]; array_splice($key, 0, 1); /* remove the field name from validator list */ $fieldValidators[$field] = $key; if (!isset($this->fields[$field]) && strpos($field, '.') === false) { /* prevent someone from bypassing a required field by removing it from the form */ $keys[$field] = !empty($this->fields[$v]) ? $this->fields[$v] : ''; } } } /** @var string|array $v */ foreach ($keys as $k => $v) { /* is a array field, ie contact[name] */ if (is_array($v) && !isset($_FILES[$k]) && is_string($k) && intval($k) == 0 && $k !== 0) { $isCheckbox = false; foreach ($v as $key => $val) { if (!is_string($key)) { $isCheckbox = true; continue; } $subKey = $k . '.' . $key; $this->_validate($subKey, $val, $fieldValidators); } if ($isCheckbox) { $this->_validate($k, $v, $fieldValidators); } } else { $this->_validate($k, $v, $fieldValidators); } } /* remove fields that have . in name */ foreach ($this->fields as $field => $v) { if (strpos($field, '.') !== false || strpos($field, ':')) { unset($this->fields[$field]); } } /* add fields back into dictionary */ foreach ($this->fields as $k => $v) { $dictionary->set($k, $v); } return $this->fields; }
/** * @return void */ public function testToArray() { $array = $this->dictionary->toArray(); $this->assertNotEmpty($array); }