From 9540db424aabcd6747058f48e9ff766747749553 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 6 Aug 2015 05:58:50 -0700 Subject: [PATCH] Fixes #948 - validate on model name+modelno+manufacturer combination --- app/controllers/admin/ModelsController.php | 29 +++++++++++++++++++--- app/lang/en/admin/models/message.php | 5 ++-- app/models/Model.php | 2 +- app/validators.php | 17 +++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/ModelsController.php b/app/controllers/admin/ModelsController.php index 4f41631a87..bb2279ffe0 100755 --- a/app/controllers/admin/ModelsController.php +++ b/app/controllers/admin/ModelsController.php @@ -63,6 +63,27 @@ class ModelsController extends AdminController // Create a new manufacturer $model = new Model; + + $validator = Validator::make( + // Validator data goes here + array( + 'unique_fields' => array(Input::get('name'), Input::get('modelno'), Input::get('manufacturer_id')) + ), + // Validator rules go here + array( + 'unique_fields' => 'unique_multiple:models,name,modelno,manufacturer_id' + ) + ); + + // attempt validation + if ($validator->fails()) + { + // The given data did not pass validation + return Redirect::back()->withInput()->with('error', Lang::get('admin/models/message.create.duplicate_set'));; + } + + + $validator = Validator::make(Input::all(), $model->validationRules()); // attempt validation @@ -80,7 +101,7 @@ class ModelsController extends AdminController $model->depreciation_id = e(Input::get('depreciation_id')); } - if ( e(Input::get('eol')) == '') { + if ( e(Input::get('eol')) == '') { $model->eol = 0; } else { $model->eol = e(Input::get('eol')); @@ -117,12 +138,12 @@ class ModelsController extends AdminController return Redirect::to('hardware/models/create')->with('error', Lang::get('admin/models/message.create.error')); } - + public function store() { //COPYPASTA!!!! FIXME $model = new Model; - + $settings=Input::all(); $settings['eol']=0; // @@ -138,7 +159,7 @@ class ModelsController extends AdminController $model->category_id=e(Input::get('category_id')); $model->user_id=Sentry::getUser()->id; $model->eol=0; - + if($model->save()) { return JsonResponse::create($model); } else { diff --git a/app/lang/en/admin/models/message.php b/app/lang/en/admin/models/message.php index e69a316dd0..fe51cb8d4c 100755 --- a/app/lang/en/admin/models/message.php +++ b/app/lang/en/admin/models/message.php @@ -8,7 +8,8 @@ return array( 'create' => array( 'error' => 'Model was not created, please try again.', - 'success' => 'Model created successfully.' + 'success' => 'Model created successfully.', + 'duplicate_set' => 'An asset model with that name, manufacturer and model number already exists.', ), 'update' => array( @@ -21,7 +22,7 @@ return array( 'error' => 'There was an issue deleting the model. Please try again.', 'success' => 'The model was deleted successfully.' ), - + 'restore' => array( 'error' => 'Model was not restored, please try again', 'success' => 'Model restored successfully.' diff --git a/app/models/Model.php b/app/models/Model.php index 2c3b5ef018..55a4601c0f 100755 --- a/app/models/Model.php +++ b/app/models/Model.php @@ -8,7 +8,7 @@ class Model extends Elegant // Declare the rules for the form validation protected $rules = array( - 'name' => 'required|alpha_space|min:2|max:255|unique:models,name,{id},id,deleted_at,NULL', + 'name' => 'required|alpha_space|min:2|max:255|unique:models,deleted_at,NULL', 'modelno' => 'alpha_space|min:1|max:255', 'category_id' => 'required|integer', 'manufacturer_id' => 'required|integer', diff --git a/app/validators.php b/app/validators.php index eb8b80e497..bc6c47506f 100755 --- a/app/validators.php +++ b/app/validators.php @@ -7,3 +7,20 @@ Validator::extend('alpha_space', function ($attribute,$value,$parameters) { // patterm must be matched at least once (empty strings will not pass) // (ungreedy?!) ,multiline }); + + +Validator::extend('unique_multiple', function ($attribute, $value, $parameters) +{ + // Get table name from first parameter + $table = array_shift($parameters); + + // Build the query + $query = DB::table($table); + + // Add the field conditions + foreach ($parameters as $i => $field) + $query->where($field, $value[$i]); + + // Validation result will be false if any rows match the combination + return ($query->count() == 0); +});