Validation Verification with Multi Select

I am making an element with several selection elements for updating schools and specialties in the school_specialty pivot table. The problem is that when I change only something in the multisecond selection of no other inputs or text fields, I can not listen to model events, so I can not synchronize the school_specialty table. But when I fill in any other input, it works fine. Here is my multi-choice from the blade:

{{Form::select('specialties[]', $specialties_data, $school->specialties, array('multiple' => 'true', 'id' => 'multi-select'))}} 

This is my update method from the school controller:

 public function update($id) { $data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id'); $school = School::find($id); $school->name = $data['name']; $school->type_id = $data['type_id']; $school->description = $data['description']; $school->info_specialties = $data['info_specialties']; $school->contacts = $data['contacts']; $school->cover_photo = Input::file('cover_photo'); $school->set_specialties = $data['specialties']; $school->financing_id = $data['financing_id']; $school->set_district_id = $data['district_id']; $school->city_id = $data['city_id']; try { $school->save(); } catch (ValidationException $errors) { return Redirect::route('admin.schools.edit', array($id)) ->withErrors($errors->getErrors()) ->withInput(); } return Redirect::route('admin.schools.edit', array($id)) ->withErrors(array('mainSuccess' => 'School was created.')); } 

And here is my sample school model:

 <?php class School extends Eloquent { protected $table = 'schools'; protected $fillable = array('name', 'type_id', 'description', 'city'); protected $guarded = array('id'); protected $appends = array('specialties'); public $set_specialties; public $set_district_id; protected static function boot() { parent::boot(); static::updating(function($model) { $data = array( 'name' => $model->name, 'type_id' => $model->type_id, 'description' => $model->description, 'specialties' => $model->set_specialties, 'city_id' => $model->city_id ); $rules = array( 'name' => 'required|min:3|max:50', 'type_id' => 'required|min:1|max:300000', 'description' => 'required|min:10', 'specialties' => 'required|array', 'city_id' => 'required|min:1|max:300000' ); $validator = Validator::make($data, $rules); if ($validator->fails()) { throw new ValidationException(null, null, null, $validator->messages()); } else { return true; } }); static::updated(function($model) { if ( $model->set_specialties != null ) { $model->specialty()->sync($model->set_specialties); } }); } public function specialty() { return $this->belongsToMany('Specialty', 'school_specialty'); } } ?> 
+5
source share
1 answer

When updating only school specialties, the events of the school model do not start, because the school model remains the same.

I think the simplest and most elegant solution is to touch a copy of the school model. This will change the updated_at field for the School object and thus trigger model events. To do this, add the following lines before the try / catch block:

 if ($school->set_specialties !== null) { $school->touch(); } 

In addition, validation should not be processed in model observers. Checking the validation of the request form here: https://laravel.com/docs/5.6/validation#form-request-validation .

0
source

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


All Articles