From 212cf416c8bcb3c5dba7a75496ab05abbbbc2d75 Mon Sep 17 00:00:00 2001 From: pxpm Date: Fri, 20 Jun 2025 11:04:09 +0100 Subject: [PATCH 1/7] wip --- src/CrudPanelManager.php | 12 +++- src/app/View/Components/DataForm.php | 61 +++++++++++++++++++ .../views/crud/components/form/form.blade.php | 48 +++++++++++++++ .../views/crud/form_content.blade.php | 8 +-- .../crud/inc/form_fields_script.blade.php | 2 + .../views/crud/widgets/form.blade.php | 18 ++++++ 6 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 src/app/View/Components/DataForm.php create mode 100644 src/resources/views/crud/components/form/form.blade.php create mode 100644 src/resources/views/crud/widgets/form.blade.php diff --git a/src/CrudPanelManager.php b/src/CrudPanelManager.php index cb7af75a2f..8979e34169 100644 --- a/src/CrudPanelManager.php +++ b/src/CrudPanelManager.php @@ -69,7 +69,9 @@ public function setupCrudPanel(string $controller, ?string $operation = null): C $crud->setOperation($operation); $primaryControllerRequest = $this->cruds[array_key_first($this->cruds)]->getRequest(); - if (! $crud->isInitialized()) { + if (! $crud->isInitialized() || ! $this->isOperationInitialized($controller::class, $operation)) { + self::setActiveController($controller::class); + $crud->initialized = false; self::setActiveController($controller::class); $controller->initializeCrudPanel($primaryControllerRequest, $crud); self::unsetActiveController(); @@ -81,6 +83,14 @@ public function setupCrudPanel(string $controller, ?string $operation = null): C return $this->cruds[$controller::class]; } + /** + * Check if a specific operation has been initialized for a controller. + */ + public function isOperationInitialized(string $controller, string $operation): bool + { + return in_array($operation, $this->getInitializedOperations($controller), true); + } + /** * Record that an operation has been initialized for a controller. * diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/DataForm.php new file mode 100644 index 0000000000..63f35e9a9d --- /dev/null +++ b/src/app/View/Components/DataForm.php @@ -0,0 +1,61 @@ +getOperation(); + } + + $this->crud = CrudManager::setupCrudPanel($controller, $operation); + + if (isset($previousOperation)) { + $this->crud->setOperation($previousOperation); + } + + $this->operation = $operation; + $this->action = $action ?? url($this->crud->route); + $this->hasUploadFields = $this->crud->hasUploadFields($operation); + } + + /** + * Get the view / contents that represent the component. + * + * @return \Illuminate\Contracts\View\View|\Closure|string + */ + public function render() + { + return view('crud::components.form.form', [ + 'crud' => $this->crud, + 'saveAction' => $this->crud->getSaveAction(), + 'id' => $this->id, + 'operation' => $this->operation, + 'action' => $this->action, + 'method' => $this->method, + ]); + } +} \ No newline at end of file diff --git a/src/resources/views/crud/components/form/form.blade.php b/src/resources/views/crud/components/form/form.blade.php new file mode 100644 index 0000000000..94e51e78f2 --- /dev/null +++ b/src/resources/views/crud/components/form/form.blade.php @@ -0,0 +1,48 @@ +
+ @include('crud::inc.grouped_errors') + +
hasUploadFields($operation)) + enctype="multipart/form-data" + @endif + > + {!! csrf_field() !!} + @if($method !== 'post') + @formMethod($method) + @endif + + {{-- Include the form fields --}} + @include('crud::form_content', ['fields' => $crud->fields(), 'action' => $operation]) + + {{-- This makes sure that all field assets are loaded. --}} +
{{ json_encode(Basset::loaded()) }}
+ + @include('crud::inc.form_save_buttons') +
+
+ +@push('after_scripts') + +@endpush \ No newline at end of file diff --git a/src/resources/views/crud/form_content.blade.php b/src/resources/views/crud/form_content.blade.php index 705a3785ad..384da85dff 100644 --- a/src/resources/views/crud/form_content.blade.php +++ b/src/resources/views/crud/form_content.blade.php @@ -15,14 +15,14 @@ {{-- Define blade stacks so css and js can be pushed from the fields to these sections. --}} -@section('after_styles') +@push('after_styles') {{-- CRUD FORM CONTENT - crud_fields_styles stack --}} @stack('crud_fields_styles') -@endsection +@endpush -@section('after_scripts') +@push('after_scripts') {{-- CRUD FORM CONTENT - crud_fields_scripts stack --}} @stack('crud_fields_scripts') @@ -245,4 +245,4 @@ function preventUnload(event) { @include('crud::inc.form_fields_script') -@endsection +@endpush \ No newline at end of file diff --git a/src/resources/views/crud/inc/form_fields_script.blade.php b/src/resources/views/crud/inc/form_fields_script.blade.php index 17c7872874..5ed5180ea2 100644 --- a/src/resources/views/crud/inc/form_fields_script.blade.php +++ b/src/resources/views/crud/inc/form_fields_script.blade.php @@ -6,6 +6,7 @@ * javascript manipulations, and makes it easy to do custom stuff * too, by exposing the main components (name, wrapper, input). */ + if (typeof CrudField === 'undefined') { class CrudField { constructor(name) { this.name = name; @@ -197,4 +198,5 @@ class CrudField { // Create all fields from a given name list fields: names => names.map(window.crud.field), }; +} diff --git a/src/resources/views/crud/widgets/form.blade.php b/src/resources/views/crud/widgets/form.blade.php new file mode 100644 index 0000000000..d20006511b --- /dev/null +++ b/src/resources/views/crud/widgets/form.blade.php @@ -0,0 +1,18 @@ +@includeWhen(!empty($widget['wrapper']), backpack_view('widgets.inc.wrapper_start')) +
+ @if (isset($widget['content']['header'])) +
+
{!! $widget['content']['header'] !!}
+
+ @endif +
+ + {!! $widget['content']['body'] ?? '' !!} + +
+ +
+ +
+
+@includeWhen(!empty($widget['wrapper']), backpack_view('widgets.inc.wrapper_end')) \ No newline at end of file From 2582d46089d58db7b89581c14a996006bc710fab Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 20 Jun 2025 10:04:30 +0000 Subject: [PATCH 2/7] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/View/Components/DataForm.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/DataForm.php index 63f35e9a9d..5f983aca7b 100644 --- a/src/app/View/Components/DataForm.php +++ b/src/app/View/Components/DataForm.php @@ -58,4 +58,4 @@ public function render() 'method' => $this->method, ]); } -} \ No newline at end of file +} From 31c4318f53d729e6377c5a81dfa4a7321f8e8ce6 Mon Sep 17 00:00:00 2001 From: Cristian Tabacitu Date: Fri, 20 Jun 2025 15:17:03 +0300 Subject: [PATCH 3/7] renaming --- src/app/View/Components/DataForm.php | 4 ++-- .../views/crud/components/{form => dataform}/form.blade.php | 0 .../views/crud/widgets/{form.blade.php => dataform.blade.php} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/resources/views/crud/components/{form => dataform}/form.blade.php (100%) rename src/resources/views/crud/widgets/{form.blade.php => dataform.blade.php} (100%) diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/DataForm.php index 5f983aca7b..6fd6c6a72a 100644 --- a/src/app/View/Components/DataForm.php +++ b/src/app/View/Components/DataForm.php @@ -5,7 +5,7 @@ use Backpack\CRUD\CrudManager; use Illuminate\View\Component; -class DataForm extends Component +class Dataform extends Component { public $crud; @@ -49,7 +49,7 @@ public function __construct( */ public function render() { - return view('crud::components.form.form', [ + return view('crud::components.dataform.form', [ 'crud' => $this->crud, 'saveAction' => $this->crud->getSaveAction(), 'id' => $this->id, diff --git a/src/resources/views/crud/components/form/form.blade.php b/src/resources/views/crud/components/dataform/form.blade.php similarity index 100% rename from src/resources/views/crud/components/form/form.blade.php rename to src/resources/views/crud/components/dataform/form.blade.php diff --git a/src/resources/views/crud/widgets/form.blade.php b/src/resources/views/crud/widgets/dataform.blade.php similarity index 100% rename from src/resources/views/crud/widgets/form.blade.php rename to src/resources/views/crud/widgets/dataform.blade.php From a8e923bee9acb71d5111e26c4104110844f7a908 Mon Sep 17 00:00:00 2001 From: pxpm Date: Mon, 23 Jun 2025 16:06:01 +0100 Subject: [PATCH 4/7] only show errors in the current form --- src/app/View/Components/DataForm.php | 1 + src/resources/views/crud/components/dataform/form.blade.php | 4 +++- src/resources/views/crud/create.blade.php | 2 +- src/resources/views/crud/edit.blade.php | 4 ++-- src/resources/views/crud/form_content.blade.php | 4 ++++ src/resources/views/crud/inc/grouped_errors.blade.php | 6 ++++++ 6 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/DataForm.php index 6fd6c6a72a..21ca19807c 100644 --- a/src/app/View/Components/DataForm.php +++ b/src/app/View/Components/DataForm.php @@ -40,6 +40,7 @@ public function __construct( $this->operation = $operation; $this->action = $action ?? url($this->crud->route); $this->hasUploadFields = $this->crud->hasUploadFields($operation); + $this->id = $id.md5($this->action.$this->operation.$this->method.$this->controller); } /** diff --git a/src/resources/views/crud/components/dataform/form.blade.php b/src/resources/views/crud/components/dataform/form.blade.php index 94e51e78f2..7d9fb8385c 100644 --- a/src/resources/views/crud/components/dataform/form.blade.php +++ b/src/resources/views/crud/components/dataform/form.blade.php @@ -13,8 +13,10 @@ @formMethod($method) @endif + + {{-- Include the form fields --}} - @include('crud::form_content', ['fields' => $crud->fields(), 'action' => $operation]) + @include('crud::form_content', ['fields' => $crud->fields(), 'action' => $operation, 'id' => $id]) {{-- This makes sure that all field assets are loaded. --}}
{{ json_encode(Basset::loaded()) }}
diff --git a/src/resources/views/crud/create.blade.php b/src/resources/views/crud/create.blade.php index f6dd72ad85..f1fa0b1aca 100644 --- a/src/resources/views/crud/create.blade.php +++ b/src/resources/views/crud/create.blade.php @@ -33,7 +33,7 @@
{{-- Default box --}} - @include('crud::inc.grouped_errors') + @include('crud::inc.grouped_errors', ['id' => $id])
{{-- Default box --}} - @include('crud::inc.grouped_errors') + @include('crud::inc.grouped_errors', ['id' => $id]) exists('vendor.backpack.crud.form_content')) - @include('vendor.backpack.crud.form_content', ['fields' => $crud->fields(), 'action' => 'edit']) + @include('vendor.backpack.crud.form_content', ['fields' => $crud->fields(), 'action' => 'edit', 'id' => $id]) @else @include('crud::form_content', ['fields' => $crud->fields(), 'action' => 'edit']) @endif diff --git a/src/resources/views/crud/form_content.blade.php b/src/resources/views/crud/form_content.blade.php index 384da85dff..c99f394222 100644 --- a/src/resources/views/crud/form_content.blade.php +++ b/src/resources/views/crud/form_content.blade.php @@ -187,7 +187,10 @@ function preventUnload(event) { @if ($crud->inlineErrorsEnabled() && session()->get('errors')) window.errors = {!! json_encode(session()->get('errors')->getBags()) !!}; + var submittedFormId = "{{ old('_form_id') }}"; + var currentFormId = '{{ $id }}'; + if (!submittedFormId || submittedFormId === currentFormId) { $.each(errors, function(bag, errorMessages){ $.each(errorMessages, function (inputName, messages) { var normalizedProperty = inputName.split('.').map(function(item, index){ @@ -231,6 +234,7 @@ function preventUnload(event) { }); }); }); + } @endif $("a[data-bs-toggle='tab']").click(function(){ diff --git a/src/resources/views/crud/inc/grouped_errors.blade.php b/src/resources/views/crud/inc/grouped_errors.blade.php index 60b581b910..3346db4cd4 100644 --- a/src/resources/views/crud/inc/grouped_errors.blade.php +++ b/src/resources/views/crud/inc/grouped_errors.blade.php @@ -1,5 +1,10 @@ {{-- Show the errors, if any --}} @if ($crud->groupedErrorsEnabled() && session()->get('errors')) + @php + $submittedFormId = old('_form_id'); + $currentFormId = $id; + @endphp + @if (!$submittedFormId || $submittedFormId === $currentFormId)
    @foreach(session()->get('errors')->getBags() as $bag => $errorMessages) @@ -11,4 +16,5 @@ @endforeach
+ @endif @endif \ No newline at end of file From 65f2381464ed5c61d06ee982fa26ed9c8391ae47 Mon Sep 17 00:00:00 2001 From: pxpm Date: Mon, 23 Jun 2025 17:01:54 +0100 Subject: [PATCH 5/7] allow update operation --- src/app/View/Components/DataForm.php | 16 +++++++++++++--- .../crud/components/dataform/form.blade.php | 16 +++++++++------- .../views/crud/inc/grouped_errors.blade.php | 4 ++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/DataForm.php index 21ca19807c..dd973ec153 100644 --- a/src/app/View/Components/DataForm.php +++ b/src/app/View/Components/DataForm.php @@ -24,6 +24,7 @@ public function __construct( public ?string $action = null, public string $method = 'post', public bool $hasUploadFields = false, + public $entry = null, ) { // Get CRUD panel instance from the controller @@ -37,9 +38,16 @@ public function __construct( $this->crud->setOperation($previousOperation); } - $this->operation = $operation; - $this->action = $action ?? url($this->crud->route); - $this->hasUploadFields = $this->crud->hasUploadFields($operation); + + if ($this->entry && $this->operation === 'update') { + $this->action = $action ?? url($this->crud->route.'/'.$this->entry->getKey()); + $this->method = 'put'; + $this->crud->entry = $this->entry; + $this->crud->setOperationSetting('fields', $this->crud->getUpdateFields()); + } else { + $this->action = $action ?? url($this->crud->route); + } + $this->hasUploadFields = $this->crud->hasUploadFields($operation, $this->entry?->getKey()); $this->id = $id.md5($this->action.$this->operation.$this->method.$this->controller); } @@ -57,6 +65,8 @@ public function render() 'operation' => $this->operation, 'action' => $this->action, 'method' => $this->method, + 'hasUploadFields' => $this->hasUploadFields, + 'entry' => $this->entry, ]); } } diff --git a/src/resources/views/crud/components/dataform/form.blade.php b/src/resources/views/crud/components/dataform/form.blade.php index 7d9fb8385c..e72b4ab5ce 100644 --- a/src/resources/views/crud/components/dataform/form.blade.php +++ b/src/resources/views/crud/components/dataform/form.blade.php @@ -1,20 +1,22 @@
- @include('crud::inc.grouped_errors') + @include('crud::inc.grouped_errors', ['id' => $id]) - hasUploadFields($operation)) + @if($hasUploadFields) enctype="multipart/form-data" @endif > {!! csrf_field() !!} - @if($method !== 'post') - @formMethod($method) - @endif + + - + @if($method !== 'post') + @method($method) + @endif {{-- Include the form fields --}} @include('crud::form_content', ['fields' => $crud->fields(), 'action' => $operation, 'id' => $id]) diff --git a/src/resources/views/crud/inc/grouped_errors.blade.php b/src/resources/views/crud/inc/grouped_errors.blade.php index 3346db4cd4..a6ca4be78e 100644 --- a/src/resources/views/crud/inc/grouped_errors.blade.php +++ b/src/resources/views/crud/inc/grouped_errors.blade.php @@ -1,8 +1,8 @@ {{-- Show the errors, if any --}} @if ($crud->groupedErrorsEnabled() && session()->get('errors')) @php - $submittedFormId = old('_form_id'); - $currentFormId = $id; + $submittedFormId = old('_form_id') ?? null; + $currentFormId = $id ?? null; @endphp @if (!$submittedFormId || $submittedFormId === $currentFormId)
From 3e9c5478c4a8c7d3382ae2067f3e33463eb51974 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 23 Jun 2025 16:02:14 +0000 Subject: [PATCH 6/7] Apply fixes from StyleCI [ci skip] [skip ci] --- src/app/View/Components/DataForm.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/DataForm.php index dd973ec153..2cf98e6928 100644 --- a/src/app/View/Components/DataForm.php +++ b/src/app/View/Components/DataForm.php @@ -38,7 +38,6 @@ public function __construct( $this->crud->setOperation($previousOperation); } - if ($this->entry && $this->operation === 'update') { $this->action = $action ?? url($this->crud->route.'/'.$this->entry->getKey()); $this->method = 'put'; From a422d920be622fdd366e749feff307781169429e Mon Sep 17 00:00:00 2001 From: Cristian Tabacitu Date: Tue, 24 Jun 2025 11:42:04 +0300 Subject: [PATCH 7/7] rename dataform file --- src/app/View/Components/{DataForm.php => Dataform.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/app/View/Components/{DataForm.php => Dataform.php} (100%) diff --git a/src/app/View/Components/DataForm.php b/src/app/View/Components/Dataform.php similarity index 100% rename from src/app/View/Components/DataForm.php rename to src/app/View/Components/Dataform.php