Example #1
0
 public function validates($options = array())
 {
     //     debug($this->controllerInvalidatesFields);
     $valide = parent::validates($options);
     $this->validationErrors = array_merge_recursive($this->validationErrors, $this->controllerInvalidatesFields);
     return $valide && count($this->controllerInvalidatesFields) == 0;
 }
Example #2
0
 /**
  * beforeSave
  *
  * @param Model $model
  * @param Model $options
  * @return void
  * @access public
  */
 function beforeSave(&$model, $options)
 {
     if (!$model->exists()) {
         $ret = $this->PluginContent->find(array('PluginContent.name' => $model->data[$model->alias]['name']));
         if ($ret) {
             // 新規登録で既に登録されている場合は、重複エラーとする
             $model->invalidate('name', '既に登録されています。');
             return false;
         }
         $pluginContent = $this->_generatePluginContentData($model);
         $this->PluginContent->create($pluginContent);
     } else {
         $pluginContent = $this->_generatePluginContentData($model, $model->data[$model->alias]['id']);
         $this->PluginContent->set($pluginContent);
     }
     // バリデーション
     return $this->PluginContent->validates();
 }
Example #3
0
 /**
  * Convenience method allowing to validate data and return the result
  *
  * @param Model $Model Model being tested
  * @param array $data Profile data
  * @param array $validationErrors Validation errors: this variable will be updated with validationErrors (sorted by key) in case of validation fail
  * @return boolean Return value of Model::validate()
  */
 protected function _validData(Model $Model, $data, &$validationErrors = array())
 {
     $valid = true;
     $Model->create($data);
     if (!$Model->validates()) {
         $validationErrors = $Model->validationErrors;
         ksort($validationErrors);
         $valid = false;
     } else {
         $validationErrors = array();
     }
     return $valid;
 }
Example #4
0
 /**
  * setSettingToDatasource
  *
  * @param $key, $value = null
  */
 public function setSettingToDatasource(Model $model, $key, $value = null)
 {
     $data = $key;
     if (is_string($key)) {
         $data = array($key => $value);
     }
     $settings = Configure::read('Setting.settings');
     foreach ($data as $k => $v) {
         if (!in_array($k, array_keys($settings))) {
             return false;
         }
         $model->validate[$k] = $settings[$k];
     }
     $model->set($data);
     if (!$model->validates()) {
         return false;
     }
     $model->begin();
     foreach ($data as $k => $v) {
         if (is_array($v) || is_object($v)) {
             $model->rollback();
             return false;
         }
         if (in_array($k, array_keys($settings))) {
             $d = $model->find('first', array('conditions' => array("{$model->alias}.key" => $k)));
             if (empty($d)) {
                 $d = array($model->alias => array('key' => $k, 'value' => $v));
             } else {
                 $d[$model->alias]['value'] = $v;
                 unset($d[$model->alias]['modified']);
             }
             $model->create();
             $model->set($d);
             if (!$model->save($d, false)) {
                 $model->rollback();
                 return false;
             }
         }
     }
     $model->commit();
     return true;
 }
Example #5
0
 /**
  * routineAdd
  *
  */
 public function routineAdd(Model $model, $data)
 {
     if (empty($data)) {
         return;
     }
     $model->create();
     $model->set($data);
     $result = $model->validates();
     if ($result === false) {
         throw new ValidationException();
     }
     $result = $model->save($data);
     if ($result !== false) {
         $model->data = array_merge($data, $result);
         return true;
     } else {
         throw new OutOfBoundsException(__('Could not save, please check your inputs.', true));
     }
     return;
 }
 /**
  * beforeValidate callback
  *
  * @param Model $Model Model using this behavior
  * @return boolean False or null will abort the operation. Any other result will continue.
  */
 public function beforeValidate(Model $Model)
 {
     if (isset($Model->data[$Model->alias][self::TAG_FIELD])) {
         $tags = $this->TagCollection->parseTags($Model->data[$Model->alias][self::TAG_FIELD]);
         if (empty($tags)) {
             return true;
         }
         // check number of tags
         if (isset($this->settings[$Model->alias]['maxTags']) && count($tags) > $this->settings[$Model->alias]['maxTags']) {
             $Model->invalidate(self::TAG_FIELD, __('Too many tags. Maximum number of tags: %d', $this->settings[$Model->alias]['maxTags']));
             return false;
         }
         // check length of tags
         foreach ($tags as $tag) {
             $this->Tag->create();
             $this->Tag->set('name', $tag);
             if (!$this->Tag->validates()) {
                 $Model->invalidate(self::TAG_FIELD, __('Invalid tag: %s. Max length: %d', $tag, Tag::NAME_MAX_LENGTH));
                 return false;
             }
         }
     }
     return true;
 }
 /**
  * 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;
 }
Example #8
0
 /**
  * UserのValidateチェック
  *
  * @param Model $model ビヘイビア呼び出し元モデル
  * @param array $data data
  * @return bool True:正常、False:不正
  */
 public function validateUser(Model $model, $data)
 {
     $model->prepare();
     //バリデーション
     $model->set($data);
     return $model->validates();
 }