Merge pull request #10628 from inietov/fixes/default_values_dont_allow_checkbox_radio

Fixes #10299 Default Values do not allow Checkbox/Radio Buttons
This commit is contained in:
snipe 2022-02-09 14:39:46 -08:00 committed by GitHub
commit 6b25b53462
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 6 deletions

View file

@ -155,7 +155,6 @@ class AssetModelsController extends Controller
$model->requestable = $request->input('requestable', '0'); $model->requestable = $request->input('requestable', '0');
$this->removeCustomFieldsDefaultValues($model); $this->removeCustomFieldsDefaultValues($model);
if ($request->input('custom_fieldset')=='') { if ($request->input('custom_fieldset')=='') {
@ -168,7 +167,6 @@ class AssetModelsController extends Controller
} }
} }
if ($model->save()) { if ($model->save()) {
return redirect()->route("models.index")->with('success', trans('admin/models/message.update.success')); return redirect()->route("models.index")->with('success', trans('admin/models/message.update.success'));
} }
@ -463,7 +461,9 @@ class AssetModelsController extends Controller
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues) private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
{ {
foreach ($defaultValues as $customFieldId => $defaultValue) { foreach ($defaultValues as $customFieldId => $defaultValue) {
if ($defaultValue) { if(is_array($defaultValue)){
$model->defaultValues()->attach($customFieldId, ['default_value' => implode(', ', $defaultValue)]);
}elseif ($defaultValue) {
$model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]); $model->defaultValues()->attach($customFieldId, ['default_value' => $defaultValue]);
} }
} }

View file

@ -46,6 +46,14 @@
<input v-if="field.type == 'text'" class="form-control m-b-xs" type="text" :value="getValue(field)" :id="'default-value' + field.id" :name="'default_values[' + field.id + ']'"> <input v-if="field.type == 'text'" class="form-control m-b-xs" type="text" :value="getValue(field)" :id="'default-value' + field.id" :name="'default_values[' + field.id + ']'">
<textarea v-if="field.type == 'textarea'" class="form-control" :value="getValue(field)" :id="'default-value' + field.id" :name="'default_values[' + field.id + ']'"></textarea><br> <textarea v-if="field.type == 'textarea'" class="form-control" :value="getValue(field)" :id="'default-value' + field.id" :name="'default_values[' + field.id + ']'"></textarea><br>
<div v-if="field.type == 'checkbox'" v-for="field_value in field.field_values_array">
<input v-if="field.type == 'checkbox'" class="" type="checkbox" :name="'default_values[' + field.id + '][]'" :value="field_value" :checked="getValue(field).split(', ').includes(field_value)"> <label>{{ field_value }}</label>
</div>
<div v-if="field.type == 'radio'" v-for="field_value in field.field_values_array">
<input v-if="field.type == 'radio'" class="" type="radio" :name="'default_values[' + field.id + ']'" :value="field_value" :checked="getValue(field).split(', ').includes(field_value)"> <label>{{ field_value }}</label>
</div>
<select v-if="field.type == 'listbox'" class="form-control m-b-xs" :name="'default_values[' + field.id + ']'"> <select v-if="field.type == 'listbox'" class="form-control m-b-xs" :name="'default_values[' + field.id + ']'">
<option value=""></option> <option value=""></option>
<option v-for="field_value in field.field_values_array" :value="field_value" :selected="getValue(field) == field_value">{{ field_value }}</option> <option v-for="field_value in field.field_values_array" :value="field_value" :selected="getValue(field) == field_value">{{ field_value }}</option>

View file

@ -18,10 +18,9 @@
@elseif ($field->element=='checkbox') @elseif ($field->element=='checkbox')
<!-- Checkboxes --> <!-- Checkboxes -->
@foreach ($field->formatFieldValuesAsArray() as $key => $value) @foreach ($field->formatFieldValuesAsArray() as $key => $value)
<div> <div>
<label> <label>
<input type="checkbox" value="{{ $value }}" name="{{ $field->db_column_name() }}[]" class="minimal" {{ isset($item) ? (in_array($key, explode(', ', $item->{$field->db_column_name()})) ? ' checked="checked"' : '') : (Request::old($field->db_column_name()) != '' ? ' checked="checked"' : '') }}> <input type="checkbox" value="{{ $value }}" name="{{ $field->db_column_name() }}[]" class="minimal" {{ isset($item) ? (in_array($value, explode(', ', $item->{$field->db_column_name()})) ? ' checked="checked"' : '') : (Request::old($field->db_column_name()) != '' ? ' checked="checked"' : (in_array($key, explode(', ', $field->defaultValue($model->id))) ? ' checked="checked"' : '')) }}>
{{ $value }} {{ $value }}
</label> </label>
</div> </div>
@ -32,7 +31,7 @@
<div> <div>
<label> <label>
<input type="radio" value="{{ $value }}" name="{{ $field->db_column_name() }}" class="minimal" {{ isset($item) ? ($item->{$field->db_column_name()} == $value ? ' checked="checked"' : '') : (Request::old($field->db_column_name()) != '' ? ' checked="checked"' : '') }}> <input type="radio" value="{{ $value }}" name="{{ $field->db_column_name() }}" class="minimal" {{ isset($item) ? ($item->{$field->db_column_name()} == $value ? ' checked="checked"' : '') : (Request::old($field->db_column_name()) != '' ? ' checked="checked"' : (in_array($value, explode(', ', $field->defaultValue($model->id))) ? ' checked="checked"' : '')) }}>
{{ $value }} {{ $value }}
</label> </label>
</div> </div>