validation - Laravel 5 Validate many to many data -


when creating new role administrator can select different rights he'd assign role. after saving freshly created role database, selected rights sync'd right_role table.

public function store(createrolerequest $request) {     $role = new role(['name' => $request->get('name')]);      $rights = [];     foreach ($request->get('rights') $id => $enabled) {         if ($enabled) {             $rights[] = $id;         }     }      $role->save();     $role->rights()->sync($rights);      return redirect()->route('users.index'); } 

but how validate submitted rights against not existing values? can within createrolerequest?

this can done custom validator. here example how custom validators can used in createrolerequest

public function __construct() {     validator::extend("valid_rights", function($attribute, $value, $parameters) {         $rules = [             'right_id' => 'exists:rights,id'         ];         foreach ($value $rightid) {                 $data = [                     'right_id' => $rightid                 ];                 $validator = validator::make($data, $rules);                 if ($validator->fails()) {                     return false;                 }             }         return true;     }); }  public function rules() {     return [         'containers' => 'required|valid_rights',     ]; } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -