mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-26 06:04:08 -08:00
Adds validation to custom fields' default values
This commit is contained in:
parent
2f02eee69b
commit
321367b974
|
@ -8,6 +8,7 @@ use App\Models\AssetModel;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use Illuminate\Support\Facades\View;
|
use Illuminate\Support\Facades\View;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Redirect;
|
use Redirect;
|
||||||
use Request;
|
use Request;
|
||||||
use Storage;
|
use Storage;
|
||||||
|
@ -90,7 +91,9 @@ class AssetModelsController extends Controller
|
||||||
// Was it created?
|
// Was it created?
|
||||||
if ($model->save()) {
|
if ($model->save()) {
|
||||||
if ($this->shouldAddDefaultValues($request->input())) {
|
if ($this->shouldAddDefaultValues($request->input())) {
|
||||||
$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'));
|
if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){
|
||||||
|
return redirect()->back()->withInput()->withErrors($model->getErrors());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Redirect to the new model page
|
// Redirect to the new model page
|
||||||
|
@ -163,7 +166,9 @@ class AssetModelsController extends Controller
|
||||||
$model->fieldset_id = $request->input('custom_fieldset');
|
$model->fieldset_id = $request->input('custom_fieldset');
|
||||||
|
|
||||||
if ($this->shouldAddDefaultValues($request->input())) {
|
if ($this->shouldAddDefaultValues($request->input())) {
|
||||||
$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'));
|
if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){
|
||||||
|
return redirect()->back()->withInput()->withErrors("Error");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,6 +456,21 @@ class AssetModelsController extends Controller
|
||||||
*/
|
*/
|
||||||
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
|
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
|
||||||
{
|
{
|
||||||
|
$data = array();
|
||||||
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
||||||
|
$customField = \App\Models\CustomField::find($customFieldId);
|
||||||
|
|
||||||
|
$data[$customField->db_column] = $defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rules = $model->fieldset->validation_rules();
|
||||||
|
|
||||||
|
$validator = Validator::make($data, $rules);
|
||||||
|
|
||||||
|
if($validator->fails()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
foreach ($defaultValues as $customFieldId => $defaultValue) {
|
||||||
if(is_array($defaultValue)){
|
if(is_array($defaultValue)){
|
||||||
$model->defaultValues()->attach($customFieldId, ['default_value' => implode(', ', $defaultValue)]);
|
$model->defaultValues()->attach($customFieldId, ['default_value' => implode(', ', $defaultValue)]);
|
||||||
|
@ -458,6 +478,7 @@ class AssetModelsController extends Controller
|
||||||
$model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]);
|
$model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue