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..2cf98e6928 --- /dev/null +++ b/src/app/View/Components/Dataform.php @@ -0,0 +1,71 @@ +getOperation(); + } + + $this->crud = CrudManager::setupCrudPanel($controller, $operation); + + if (isset($previousOperation)) { + $this->crud->setOperation($previousOperation); + } + + 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); + } + + /** + * Get the view / contents that represent the component. + * + * @return \Illuminate\Contracts\View\View|\Closure|string + */ + public function render() + { + return view('crud::components.dataform.form', [ + 'crud' => $this->crud, + 'saveAction' => $this->crud->getSaveAction(), + 'id' => $this->id, + '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 new file mode 100644 index 0000000000..e72b4ab5ce --- /dev/null +++ b/src/resources/views/crud/components/dataform/form.blade.php @@ -0,0 +1,52 @@ +
+ @include('crud::inc.grouped_errors', ['id' => $id]) + + +
+ {!! csrf_field() !!} + + + + + @if($method !== 'post') + @method($method) + @endif + {{-- Include the form fields --}} + @include('crud::form_content', ['fields' => $crud->fields(), 'action' => $operation, 'id' => $id]) + + {{-- 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/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 705a3785ad..c99f394222 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') @@ -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(){ @@ -245,4 +249,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/inc/grouped_errors.blade.php b/src/resources/views/crud/inc/grouped_errors.blade.php index 60b581b910..a6ca4be78e 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') ?? null; + $currentFormId = $id ?? null; + @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 diff --git a/src/resources/views/crud/widgets/dataform.blade.php b/src/resources/views/crud/widgets/dataform.blade.php new file mode 100644 index 0000000000..d20006511b --- /dev/null +++ b/src/resources/views/crud/widgets/dataform.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