Fixes #948 - validate on model name+modelno+manufacturer combination
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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.'
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user