Пример #1
0
 /**
  * Validates input data. Only fields present in the $fields array
  * will be validated. Rules must be defined in the form model's
  * static $rules array.
  *
  * <code>
  * 		// define rules within the form model
  * 		public static $rules = array( 'first_name' => 'required' );
  * 
  *		// validate fields from the controller
  *		$is_valid = ExampleForm::is_valid( array( 'first_name', 'last_name' ) );
  * </code>
  *
  * @param  array   $fields
  * @param  array   $input
  * @return bool
  */
 public static function is_valid($fields = null, $input = null)
 {
     // $fields must be an array or null, a null value represents
     // that all fields should be validated
     if (!is_array($fields) && !is_null($fields)) {
         return false;
     }
     // if input is null then pull all input from the input class
     if (is_null($input)) {
         $input = Input::all();
     }
     // if $fields is an array then we need to walk through the
     // rules defined in the form model and pull out any that
     // apply to the fields that were defined
     // if $fields isn't an array then apply all rules
     if (is_array($fields)) {
         $field_rules = array();
         foreach ($fields as $field_name) {
             if (array_key_exists($field_name, static::$rules)) {
                 $field_rules[$field_name] = static::$rules[$field_name];
             }
         }
     } else {
         $field_rules = static::$rules;
     }
     // if no rules apply to the fields that we're validating then
     // validation passes
     if (empty($field_rules)) {
         return true;
     }
     // generate the validator and return its success status
     static::$validation = Validator::make($input, $field_rules);
     return static::$validation->passes();
 }
Пример #2
0
 /**
  * Save the model instance to the database.
  *
  * @return bool
  */
 public function save()
 {
     if (!$this->dirty()) {
         return true;
     }
     if (static::$timestamps) {
         $this->timestamp();
     }
     $this->fire_event('saving');
     if (static::$rules) {
         $validator = Validator::make($this->attributes, static::$rules);
         if (!$validator->valid()) {
             $this->errors = $validator->errors;
             return false;
         }
     }
     // If the model exists, we only need to update it in the database, and the update
     // will be considered successful if there is one affected row returned from the
     // fluent query instance. We'll set the where condition automatically.
     if ($this->exists) {
         $query = $this->query()->where(static::$key, '=', $this->get_key());
         $result = $query->update($this->get_dirty()) === 1;
         if ($result) {
             $this->fire_event('updated');
         }
     } else {
         $id = $this->query()->insert_get_id($this->attributes, $this->sequence());
         $this->set_key($id);
         $this->exists = $result = is_numeric($this->get_key());
         if ($result) {
             $this->fire_event('created');
         }
     }
     // After the model has been "saved", we will set the original attributes to
     // match the current attributes so the model will not be viewed as being
     // dirty and subsequent calls won't hit the database.
     $this->original = $this->attributes;
     if ($result) {
         $this->fire_event('saved');
     }
     return $result;
 }
Пример #3
0
 public static function validate($data)
 {
     return Validator::make($data, Static::$rules);
 }
Пример #4
0
 private function _validate_request($type)
 {
     // A hook before we go any further.
     if (method_exists($this, '_before_validation')) {
         $this->_before_validation();
     }
     // Set rules.
     if ($type == 'create') {
         $rules = $this->rules_create;
     } else {
         $rules = $this->rules_update;
     }
     // If we have rules we validate.
     if (is_array($rules) && count($rules > 0)) {
         // Time to validate.
         $validation = Validator::make(Input::get(), $rules, $this->rules_message);
         if ($validation->fails()) {
             if (Input::get('redirect_fail')) {
                 return Redirect::to(Input::get('redirect_fail'))->with_errors($validation)->with('data', Input::get());
             } else {
                 return $this->api_response(null, 0, $validation->errors->messages);
             }
         }
     }
     return false;
 }