Skip to content

Commit

Permalink
Merge pull request #15565 from spencerrlongg/bug/sc-25921
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe authored Dec 9, 2024
2 parents f726359 + 4f9f035 commit 0149ed7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
12 changes: 10 additions & 2 deletions app/Http/Controllers/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Support\Facades\Log;
use \Illuminate\Contracts\View\View;
use \Illuminate\Http\RedirectResponse;
use Illuminate\Support\MessageBag;


/**
Expand All @@ -29,6 +30,7 @@
*/
class AssetModelsController extends Controller
{
protected MessageBag $validatorErrors;
/**
* Returns a view that invokes the ajax tables which actually contains
* the content for the accessories listing, which is generated in getDatatable.
Expand Down Expand Up @@ -158,7 +160,7 @@ public function update(StoreAssetModelRequest $request, $modelId) : RedirectResp

if ($this->shouldAddDefaultValues($request->input())) {
if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))) {
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.fieldset_default_value.error'));
return redirect()->back()->withInput()->withErrors($this->validatorErrors);
}
}

Expand Down Expand Up @@ -481,9 +483,15 @@ private function assignCustomFieldsDefaultValues(AssetModel|SnipeModel $model, a
$rules[$field] = $validation;
}

$validator = Validator::make($data, $rules);
$attributes = [];
foreach ($model->fieldset->fields as $field) {
$attributes[$field->db_column] = trim(preg_replace('/_+|snipeit|\d+/', ' ', $field->db_column));
}

$validator = Validator::make($data, $rules)->setAttributeNames($attributes);

if($validator->fails()){
$this->validatorErrors = $validator->errors();
return false;
}

Expand Down
8 changes: 8 additions & 0 deletions app/Livewire/CustomFieldSetDefaultValuesForModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ public function mount($model_id = null)
$this->fieldset_id = $this->model?->fieldset_id;
$this->add_default_values = ($this->model?->defaultValues->count() > 0);


$this->initializeSelectedValuesArray();
if (session()->has('errors')) {
$errors = session('errors')->keys();
$selectedValuesKeys = array_keys($this->selectedValues);
if (count(array_intersect($selectedValuesKeys, $errors)) > 0) {
$this->add_default_values = true;
};
}
$this->populatedSelectedValuesArray();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<span>

<div class="form-group{{ $errors->has('custom_fieldset') ? ' has-error' : '' }}">
<label for="custom_fieldset" class="col-md-3 control-label">
{{ trans('admin/models/general.fieldset') }}
Expand All @@ -11,6 +10,7 @@
<div class="col-md-3">
@if ($fieldset_id)
<label class="form-control">

{{ Form::checkbox('add_default_values', 1, old('add_default_values', $add_default_values), ['data-livewire-component' => $this->getId(), 'id' => 'add_default_values', 'wire:model.live' => 'add_default_values', 'disabled' => $this->fields->isEmpty()]) }}
{{ trans('admin/models/general.add_default_values') }}
</label>
Expand All @@ -19,12 +19,13 @@
</div>

@if ($add_default_values)
@if ($this->fields)

@if ($this->fields)

@foreach ($this->fields as $field)
<div class="form-group" wire:key="field-{{ $field->id }}">

<label class="col-md-3 control-label{{ $errors->has($field->name) ? ' has-error' : '' }}">{{ $field->name }}</label>
<label class="col-md-3 control-label{{ $errors->has($field->db_column_name()) ? ' has-error' : '' }}">{{ $field->name }}</label>

<div class="col-md-7">

Expand Down Expand Up @@ -123,10 +124,16 @@ class="form-control"
Unknown field element: {{ $field->element }}
</span>
@endif
<?php
$errormessage = $errors->first($field->db_column_name());
if ($errormessage) {
print('<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> '.$errormessage.'</span>');
}
?>
</div>
</div>

@endforeach
@endforeach

@endif

Expand Down
14 changes: 7 additions & 7 deletions resources/views/models/custom_fields_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@
<p class="help-block">{{ $field->help_text }}</p>
@endif

<?php
$errormessage=$errors->first($field->db_column_name());
if ($errormessage) {
$errormessage=preg_replace('/ snipeit /', '', $errormessage);
print('<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> '.$errormessage.'</span>');
}
?>
<?php
$errormessage = $errors->first($field->db_column_name());
if ($errormessage) {
$errormessage = preg_replace('/ snipeit /', '', $errormessage);
print('<span class="alert-msg" aria-hidden="true"><i class="fas fa-times" aria-hidden="true"></i> '.$errormessage.'</span>');
}
?>
</div>

@if ($field->field_encrypted)
Expand Down
2 changes: 1 addition & 1 deletion resources/views/models/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<!-- Custom Fieldset -->
<!-- If $item->id is null we are cloning the model and we need the $model_id variable -->
@livewire('custom-field-set-default-values-for-model',["model_id" => $item->id ?? $model_id ?? null ])
@livewire('custom-field-set-default-values-for-model', ["model_id" => $item->id ?? $model_id ?? null])

@include ('partials.forms.edit.notes')
@include ('partials.forms.edit.requestable', ['requestable_text' => trans('admin/models/general.requestable')])
Expand Down

0 comments on commit 0149ed7

Please sign in to comment.