Appends errors generated from fields during the execution of an Event

@param XMLElement $result
@param array $fields
@param array $errors
@param object $post_values
@throws Exception
@return XMLElement
491133
public static function appendErrors(XMLElement $result, array $fields, $errors, $post_values)
    {
        $result->setAttribute('result', 'error');
        $result->appendChild(new XMLElement('message', __('Entry encountered errors when saving.'), array(
            'message-id' => EventMessages::ENTRY_ERRORS
        )));

        foreach ($errors as $field_id => $message) {
            $field = FieldManager::fetch($field_id);

            // Do a little bit of a check for files so that we can correctly show
            // whether they are 'missing' or 'invalid'. If it's missing, then we
            // want to remove the data so `__reduceType` will correctly resolve to
            // missing instead of invalid.
            // @see https://github.com/symphonists/s3upload_field/issues/17
            if (isset($_FILES['fields']['error'][$field->get('element_name')])) {
                $upload = $_FILES['fields']['error'][$field->get('element_name')];

                if ($upload === UPLOAD_ERR_NO_FILE) {
                    unset($fields[$field->get('element_name')]);
                }
            }

            if (is_array($fields[$field->get('element_name')])) {
                $type = array_reduce($fields[$field->get('element_name')], array('SectionEvent', '__reduceType'));
            } else {
                $type = ($fields[$field->get('element_name')] == '') ? 'missing' : 'invalid';
            }

            $error = self::createError($field, $type, $message);
            $result->appendChild($error);
        }

        if (isset($post_values) && is_object($post_values)) {
            $result->appendChild($post_values);
        }

        return $result;
    }
994318
public static function buildResultDataDefault(
        $vendor_name = null,
        $vendor_id = null,
        $success = null,
        $errors = null
    ) {
        return [
            self::P_RESULT_DATA_VENDOR_NAME => $vendor_name,
            self::P_RESULT_DATA_VENDOR_ID   => $vendor_id,
            self::P_RESULT_DATA_SUCCESS     => $success,
            self::P_RESULT_DATA_ERRORS      => $errors,
        ];
    }
309026
private function renderElements(
        FieldsetInterface $elements, $formClass, $output, $submitElements, $offset = 2
    ) {
        foreach ($elements as $element) {
            if ($element instanceof FieldsetInterface) {
                list($output, $submitElements) = $this->renderElements(
                    $element, $formClass, $output, $submitElements
                );
            } elseif ($element instanceof Submit
                || $element instanceof Button
            ) {
                $submitElements[] = $element;
            } elseif (
                $element instanceof Csrf || $element instanceof Hidden
            ) {
                $output .= $this->getView()->formElement($element);
            } elseif ($element instanceof Checkbox) {
                $viewModel = new ViewModel();
                $viewModel->setVariable('element', $element);
                $viewModel->setVariable('offset', $offset);
                $viewModel->setTemplate(
                    'travello-view-helper/widget/bootstrap-form-checkbox'
                );

                $output .= $this->getView()->render($viewModel);
            } else {
                if ($element instanceof File) {
                    $element->setAttributes(
                        ['class' => 'form-control-static']
                    );
                } else {
                    $element->setAttributes(['class' => 'form-control']);
                }

                if ($formClass == 'form-inline') {
                    $element->setLabelAttributes(
                        ['class' => 'control-label']
                    );

                    $template = 'bootstrap-form-group-inline';
                } else {
                    $element->setLabelAttributes(
                        ['class' => 'col-sm-' . $offset . ' control-label']
                    );

                    $template = 'bootstrap-form-group';
                }

                $viewModel = new ViewModel();
                $viewModel->setVariable('element', $element);
                $viewModel->setVariable('offset', $offset);
                $viewModel->setTemplate(
                    'travello-view-helper/widget/' . $template
                );

                $output .= $this->getView()->render($viewModel);
            }
        }

        return [$output, $submitElements];
    }
458476
public function addError($field, $msg, $params = [])
    {
        $msg = $this->formatMessage($field, $msg, $params);
        $values = [];
        foreach ($params as $param) {
            if (is_array($param)) {
                $param = "['" . implode("', '", $param) . "']";
            }
            if ($param instanceof DateTime) {
                $param = $param->format('Y-m-d');
            } else {
                if (is_object($param)) {
                    $param = get_class($param);
                }
            }
            if (isset($params[0]) && is_string($params[0])) {
                if (isset($this->_labels[$param])) {
                    $param = $this->_labels[$param];
                }
            }
            $values[] = $param;
        }
        $this->_errors[$field][] = vsprintf($msg, $values);
    }
181009
protected function processFields(array $fields, array $results = [])
    {
        $input = $this->input;
        $output = $this->output;
        $helper = $this->helperSet->get('question');

        foreach ($fields as $field) {
            if (!$field instanceof FieldDefinitionInterface) {
                continue;
            }
            $field_name = $field->getName();

            if (isset($results[$field_name])) {
                continue;
            }

            if (!$field instanceof FieldGroup) {
                if ($field->hasCondition()
                    && !$this->fieldConditionMet($field, $results)) {
                    continue;
                }

                if ($field->hasFieldCallback()) {
                    $field->onFieldCallback($results);
                }

                if ($input->isInteractive()) {
                    try {
                        $value = $helper->ask($input, $output, $field->asQuestion());

                        if ($field->hasSubform()) {
                            $subform = new static();
                            $field->onSubformProcess($subform, $value);

                            $results[$field_name] = $subform
                                ->setInput($input)
                                ->setOutput($output)
                                ->setHelperSet($this->helperSet)
                                ->process()
                                ->getResults();
                        } else {
                            $results[$field_name] = $field->formattedValue($value);
                        }
                    } catch (\Exception $e) {
                        throw new FormException(trim($e->getMessage()));
                    }
                }
            } else {
                $groups = [];

                do {
                    $result = $this->processFields(
                        $field->getGroupFields()
                    );
                    $continue = false;

                    if ($field->hasLoopUntil()) {
                        $continue = $field->loopUntil([$result]);
                        if ($continue) {
                            $groups[] = $result;
                        }
                    } else {
                        $groups[] = $result;
                    }
                } while ($continue);

                $results[$field_name] = $groups;
            }
        }

        return $results;
    }
364251
static function fail($value, $params, $defaults = []) {
    $params   = array_merge(['message' => 'value is invalid'], $defaults, $params);
    $show_val = is_scalar($value) ? ' [' . $value . ']' : '';

    throw new \RoyalMail\Exception\ValidatorException($params['message'] . $show_val);
  }
694762
protected function setErrorField($name, $value, $messages, $arrayIndex = null)
    {
        // Loop through all errors
        foreach ($messages as $errorType => $error) {
            // When the validation messages are about an array, they are set in a sub array (validation_errors)
            if (is_numeric($errorType) && is_array($error) && isset($error['validation_errors'])) {
                // Array index is 1 based so deduct one to create 0 based index
                $arrayIndex = ((int) $errorType) - 1;
                $this->setErrorField(
                    $name,
                    isset($value[$arrayIndex]) ? $value[$arrayIndex] : null,
                    $error['validation_errors'],
                    $arrayIndex
                );
            } elseif (is_array($error)) {
                // When there is an array of errors returned, handle them recursively
                $this->setErrorField($name, $value, $error, $arrayIndex);
            } else {
                // Add error field with all information
                $this->errorFields[] = new ErrorField($name, $value, $errorType, $error, $arrayIndex);
            }
        }
    }
1229933
public function getCreateErrors(array $data, array &$errors = [], $fieldset = null)
    {
        $schema = $this->schema;

        if ($fieldset) {
            $schema = $fieldset;
        }

        $fields = $schema->getFields();
        $table = $schema->getName();
        foreach ($fields as $field) {
            $name = $table . '_' . $field['name'];

            if ($field['field']['type'] === 'fieldset' && isset($data[$name])) {
                if (!isset($errors[$name])) {
                    $errors[$name] = [];
                }

                if (isset($field['field']['attributes']['data-multiple'])
                    && !$field['field']['attributes']['data-multiple']
                ) {
                    $errors[$name] = $this->getCreateErrors(
                        $data[$name],
                        $errors[$name],
                        Fieldset::i($field['field']['parameters'])
                    );

                    if (empty($errors[$name])) {
                        unset($errors[$name]);
                    }

                    continue;
                }

                foreach($data[$name] as $index => $row) {
                    if (!isset($errors[$name][$index])) {
                        $errors[$name][$index] = [];
                    }

                    $errors[$name][$index] = $this->getCreateErrors(
                        $row,
                        $errors[$name][$index],
                        Fieldset::i($field['field']['parameters'])
                    );

                    if (empty($errors[$name][$index])) {
                        unset($errors[$name][$index]);
                    }
                }

                if (empty($errors[$name])) {
                    unset($errors[$name]);
                }

                continue;
            }

            if (isset($field['validation'])) {
                foreach ($field['validation'] as $validation) {
                    if ($validation['method'] === 'required'
                        && (
                            !isset($data[$name])
                            || (
                                is_array($data[$name])
                                && empty($data[$name])
                            )
                            || (
                                !is_array($data[$name])
                                && !strlen('' . $data[$name])
                            )
                        )
                    ) {
                        $errors[$name] = $validation['message'];
                    }
                }
            }
        }

        // is this checker not fieldset specific?
        // if yes let's check relations
        if (!$fieldset) {
            $relations = $this->schema->getRelations();
            foreach ($relations as $relation) {
                if ($relation['many'] != 1) {
                    continue;
                }

                if (!isset($data[$relation['primary2']]) || !is_numeric($data[$relation['primary2']])) {
                    $errors[$relation['primary2']] = sprintf('%s should be valid', $relation['singular']);
                }
            }
        }

        return $this->getOptionalErrors($data, $errors, $fieldset);
    }
494747
function(err, results) {
  if(err) {
    this.success = false;
    this.error = err;
    this.results = null;

    // customize error for error types
    if(util.isError(err)) {
      this.error = {
        message: err.message,
        type: err.type,
        arguments: err.arguments
      };
      if(NODE_ENV === 'development') {
        this.error.stack = err.stack;
      }
    }    

  } else {
    this.success = true;
    this.error = null;      
    this.results = convertToClient(results);    
  }
}
335893
private function buildFields(array $fields, $indent, array &$result)
    {
        $stringified_field_names = [];
        $fields_with_default_value = [];

        foreach ($fields as $field) {
            if ($field instanceof ScalarField && $field->getShouldBeAddedToModel()) {
                $stringified_field_names[] = var_export($field->getName(), true);

                if ($field->getName() != 'id' && $field->getDefaultValue() !== null) {
                    $fields_with_default_value[$field->getName()] = $field->getDefaultValue();
                }
            }
        }

        $result[] = '';
        $result[] = $indent . '/**';
        $result[] = $indent . ' * Table fields that are managed by this entity.';
        $result[] = $indent . ' *';
        $result[] = $indent . ' * @var array';
        $result[] = $indent . ' */';
        $result[] = $indent . 'protected $fields = [' . implode(', ', $stringified_field_names) . '];';

        if (count($fields_with_default_value)) {
            $result[] = '';
            $result[] = $indent . '/**';
            $result[] = $indent . ' * List of default field values.';
            $result[] = $indent . ' *';
            $result[] = $indent . ' * @var array';
            $result[] = $indent . ' */';
            $result[] = $indent . 'protected $default_field_values = [';

            foreach ($fields_with_default_value as $field_name => $default_value) {
                $result[] = $indent . '   ' . var_export($field_name, true) . ' => ' . var_export($default_value, true) . ',';
            }

            $result[] = $indent . '];';
        }
    }
1148631
private void addResult(ValidationResult result) {
		if (result == null || result.count() == 0) {
			return;
		}
		this.results.add(result);
	}
134439
public function checkResult(Pjb62\Exception\JavaException $result): void
    {
        $exception = $this->getExceptionFromResult($result);
        throw $exception;
    }
949521
protected function arrayToXml($result, &$xml)
	{
		foreach ($result as $key => $value) {
			if (is_array($value)) {
				if (!is_numeric($key)) {
					$subnode = $xml->addChild((string) $key);
				} else {
					$subnode = $xml->addChild('item_' . $key);
				}
				$this->arrayToXml($value, $subnode);
			} else {
				$xml->addChild($key, $value);
			}
		}
	}
745026
function(results) {        
        for(var i = 0; i < results.rows.length; i++) {
            console.log("Result " + (i + 1) + ": ");
            var row = results.rows[i];
            for(var j = 0; j < results.fields.length; j++) {
                var field = results.fields[j];
                var value = row[j];
                
                console.log("  " + field + " = " + value);
            }
        }
    }
913640
private void listErrors(final Result result) {
        for (JSError warning : result.warnings) {
            getLog().warn(warning.toString());
        }

        for (JSError error : result.errors) {
            getLog().error(error.toString());
        }
    }
265600
private function addErrors($field, $errors)
	{
		$this->errors->$field = array_merge((array)$this->errors->$field, $errors);
	}
83696
public function errorsAsXML()
    {
        $dom = new DOMDocument('1.0', 'UTF-8');
        $root = $dom->createElement("phpsysinfo");
        $dom->appendChild($root);
        $xml = new SimpleXMLExtended(simplexml_import_dom($dom), 'UTF-8');
        $generation = $xml->addChild('Generation');
        $generation->addAttribute('version', PSI_VERSION_STRING);
        $generation->addAttribute('timestamp', time());
        $xmlerr = $xml->addChild("Errors");
        foreach ($this->_arrErrorList as $arrLine) {
//            $error = $xmlerr->addCData('Error', $arrLine['message']);
            $error = $xmlerr->addChild('Error');
            $error->addAttribute('Message', $arrLine['message']);
            $error->addAttribute('Function', $arrLine['command']);
        }
        header("Cache-Control: no-cache, must-revalidate\n");
        header("Content-Type: text/xml\n\n");
        echo $xml->getSimpleXmlElement()->asXML();
        exit();
    }