From bea3a7b9821cc677239b3a04b67187cc8abbe0e3 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 24 Apr 2020 00:39:32 -0700 Subject: [PATCH] Fixed #7987 - allow toggle of required/optional in custom fields/fieldsets Signed-off-by: snipe --- .../Controllers/CustomFieldsetsController.php | 41 ++++++++++++++++++- .../custom_fields/fieldsets/view.blade.php | 15 ++++++- routes/web/fields.php | 13 ++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/CustomFieldsetsController.php b/app/Http/Controllers/CustomFieldsetsController.php index f28548fddc..f484931325 100644 --- a/app/Http/Controllers/CustomFieldsetsController.php +++ b/app/Http/Controllers/CustomFieldsetsController.php @@ -189,7 +189,7 @@ class CustomFieldsetsController extends Controller } } - $results = $set->fields()->attach(Input::get('field_id'), ["required" => ($request->input('required') == "on"),"order" => $request->input('order', 1)]); + $results = $set->fields()->attach($request->input('field_id'), ["required" => ($request->input('required') == "on"),"order" => $request->input('order', 1)]); return redirect()->route("fieldsets.show", [$id])->with("success", trans('admin/custom_fields/message.field.create.assoc_success')); } @@ -197,4 +197,43 @@ class CustomFieldsetsController extends Controller } + + /** + * Set the field in a fieldset to required + * + * @author [A. Gianotto] [] + * @since [v5.0] + */ + public function makeFieldRequired($fieldset_id, $field_id) + { + + $this->authorize('update', CustomFieldset::class); + $field = CustomField::findOrFail($field_id); + $fieldset = CustomFieldset::findOrFail($fieldset_id); + $fields[$field->id] = ['required' => 1]; + $fieldset->fields()->syncWithoutDetaching($fields); + + return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id]) + ->with("success", trans('Field successfully set to required')); + + } + + /** + * Set the field in a fieldset to optional + * + * @author [A. Gianotto] [] + * @since [v5.0] + */ + public function makeFieldOptional($fieldset_id, $field_id) + { + $this->authorize('update', CustomFieldset::class); + $field = CustomField::findOrFail($field_id); + $fieldset = CustomFieldset::findOrFail($fieldset_id); + $fields[$field->id] = ['required' => 0]; + $fieldset->fields()->syncWithoutDetaching($fields); + + return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id]) + ->with("success", trans('Field successfully set to optional')); + + } } diff --git a/resources/views/custom_fields/fieldsets/view.blade.php b/resources/views/custom_fields/fieldsets/view.blade.php index 21ba1b4a01..d6f6c0c7fe 100644 --- a/resources/views/custom_fields/fieldsets/view.blade.php +++ b/resources/views/custom_fields/fieldsets/view.blade.php @@ -56,10 +56,21 @@ {{$field->format}} {{$field->element}} {{ $field->field_encrypted=='1' ? trans('general.yes') : trans('general.no') }} - {{$field->pivot->required ? "REQUIRED" : "OPTIONAL"}} + + @if ($field->pivot->required) + + + Required - click to make optional + + @else + + + Optional - click to make required + + @endif @can('update', $custom_fieldset) - Remove + Remove @endcan diff --git a/routes/web/fields.php b/routes/web/fields.php index 18a403aae1..d95f8e5546 100644 --- a/routes/web/fields.php +++ b/routes/web/fields.php @@ -7,6 +7,17 @@ Route::group([ 'prefix' => 'fields','middleware' => ['auth'] ], function () { + Route::get('required/{fieldset_id}/{field_id}', + ['uses' => 'CustomFieldsetsController@makeFieldRequired', + 'as' => 'fields.required'] + ); + + Route::get('optional/{fieldset_id}/{field_id}', + ['uses' => 'CustomFieldsetsController@makeFieldOptional', + 'as' => 'fields.optional'] + ); + + Route::get('{field_id}/fieldset/{fieldset_id}/disassociate', ['uses' => 'CustomFieldsController@deleteFieldFromFieldset', 'as' => 'fields.disassociate'] @@ -20,6 +31,8 @@ Route::group([ 'prefix' => 'fields','middleware' => ['auth'] ], function () { Route::resource('fieldsets', 'CustomFieldsetsController', [ 'parameters' => ['fieldset' => 'field_id', 'field' => 'field_id'] ]); + + }); Route::resource('fields', 'CustomFieldsController', [