/** * Invalid Fields * * @param array $options Options * @return array Errors */ public function invalidFields($options = array()) { $errors = parent::invalidFields($options); foreach ($errors as $key => $value) { $model = false; if (is_array($value)) { $values = each($value); $model = $values['key']; $value = $values['value']; } $rule = array(); if (!empty($this->validate[$key][$value]['rule'])) { $rule = $this->validate[$key][$value]['rule']; } if (array_key_exists($value, $this->error)) { $error = vsprintf(__($this->error[$value], true), $rule); } else { $error = __($value, true); } if (!empty($model)) { $error = array($model => $error); } $errors[$key] = $error; } $this->validationErrors = $errors; return $errors; }
/** * Saves a records, either add or edit. * See $this->_defaults['useOnlyDefinedFields'] for an explanation * * @param $model the model (set by cake) * @param $data the data to save (first user argument) * @param $options the save options * @return returns the result of the save operation */ public function saveFields(Model $model, $data = null, $options = array()) { // overwrite config for this commit $config = $this->settings[$this->model->alias]['useOnlyDefinedFields']; $this->settings[$this->model->alias]['useOnlyDefinedFields'] = true; // this should never be the case, cause Bancha cannot handle validation errors currently // We expect to automatically send validation errors to the client in the right format in version 1.1 if ($data) { $model->set($data); } if (!$model->validates()) { $msg = "The record doesn't validate. Since Bancha can't send validation errors to the " . "client yet, please handle this in your application stack."; if (Configure::read('debug') > 0) { $msg .= "<br/><br/><pre>Validation Errors:\n" . print_r($model->invalidFields(), true) . "</pre>"; } throw new BadRequestException($msg); } $result = $model->save($model->data, $options); // set back $this->settings[$this->model->alias]['useOnlyDefinedFields'] = $config; return $result; }