Check Laravel 4

I use the following rules to verify when creating a new user:

protected $rules= [ 'name' => 'required', 'email' => [ 'required', 'unique:user', 'email' ] ]; 

When updating an existing user, I use the same set of rules as above but do not want verification errors if the user has not changed his email address at all.

I am currently resolving this using the following:

 if (!User::changed('email')) { unset($user->email); } 

This seems like a dirty workaround to me, so I was wondering if there are any better alternatives.

Also note that the changed method is what I wrote myself. Does anyone know if this is Laravel 4's own method for checking whether a property of a model has changed?

Thanks!

+6
source share
6 answers

The only validation rule allows you to ignore this identifier, which in your case is the identifier of the data set that you are updating.

 'email' => 'unique:users,email_address,10' 

http://four.laravel.com/docs/validation#rule-unique

+9
source

One approach is to create a validation function in the model and call it using the controller passing in the input, script and id (to ignore).

 public function validate($input, $scenario, $id = null) { $rules = []; switch($scenario) { case 'store': $rules = [ 'name' => 'required|min:5|unique:users', 'email' => 'required|email|unique:users', 'password' => 'required|min:4|confirmed' ]; break; case 'update'; $rules = [ 'name' => 'required|min:5|unique:users' .',name,' . $id, 'email' => 'required|email|unique:users' .',email,' . $id, 'password' => 'min:4|confirmed' ]; break; } return Validator::make($input, $rules); } 

Then in the controller:

  $input = Input::all(); $validation = $user->validate($input, 'update', $user->id); if ($validation->fails()) { // Do stuff } else { // Validation passes // Do other stuff } 

As already mentioned, the third parameter of the unique rule specifies the id to ignore. You can add other cases, such as "login", to reuse the verification function.

Alternatively, Jeffrey Way at Tuts Premium has an excellent series of tutorials in β€œWhat New In Laravel 4,” which includes several other approaches for handling validation using services and listeners.

+4
source

See the documentation at http://four.laravel.com/docs/validation#rule-unique

You can exclude your own user ID

 protected $rules= [ 'name' => 'required', 'email' => [ 'required', 'unique:user,email,THE_USERS_USER_ID', 'email' ] ]; 
+1
source

Starting from 2014-01-14 you can use the sometimes attribute, I believe that Taylor added them 2 days ago in Laravel 4.1

 $v = Validator::make($data, array( 'email' => 'sometimes|required|email', )); 

sometimes only check input if it exists. this may or may not suit your specific scenario if you do not have a default value to insert.

http://laravel.com/docs/validation#conditionally-adding-rules

+1
source

I handle similar things in my validator function. My array of validators is configured as a class variable. Then I do something like this:

 public function validate() { //exclude the current user id from 'unqiue' validators if( $this->id > 0 ) { $usernameUnique = 'unique:users,username,'.$this->id; $emailUnique = 'unique:users,email,'.$this->id; $apiUnique = 'unique:users,api_key,'.$this->id; } else { $usernameUnique = 'unique:users,username'; $emailUnique = 'unique:users,email'; $apiUnique = 'unique:users,api_key'; } $this->validators['username'] = array('required', 'max:32', $usernameUnique); $this->validators['email'] = array('required', 'max:32', $emailUnique); $this->validators['api_key'] = array('required', 'max:32', $apiUnique); $val = Validator::make($this->attributes, $this->validators); if ($val->fails()) { throw new ValidationException($val); } } 
0
source

I solved this by having different rules for updating and creating on models that should do this, such as Users.

I have a Model class that extends Eloquent, where I define validation, and then all child models that extend the model can have both $ rules and $ update_rules defined. If you define only $ rules, it will be used for both creation and updating.

 class Model extends Eloquent { protected $errors; protected static $rules = array(); protected $validator; public function __construct(array $attributes = array(), Validator $validator = null) { parent::__construct($attributes); $this->validator = $validator ?: \App::make('validator'); } protected static function boot() { parent::boot(); # call validatie when createing static::creating(function($model) { return $model->validate(); }); # call validatie when updating with $is_update = true param static::updating(function($model) { return $model->validate(true); }); } public function validate($is_update = false) { # if we have $update_rules defined in the child model, and save is an update if ($is_update and isset(static::$update_rules)) { $v = $this->validator->make($this->attributes, static::$update_rules); } else { $v = $this->validator->make($this->attributes, static::$rules); } if ($v->passes()) { return true; } $this->setErrors($v->messages()); return false; } protected function setErrors($errors) { $this->errors = $errors; } public function getErrors() { return $this->errors; } public function hasErrors() { return ! empty($this->errors); } } 
0
source

Source: https://habr.com/ru/post/944851/


All Articles