From b7285578889bb8f315edef9b48d88d88c0dba041 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 13:52:41 -0700 Subject: [PATCH 01/23] basic functionality --- src/Assets/AssetContainer.php | 7 +++++++ src/Http/Controllers/CP/Assets/AssetsController.php | 6 ++++-- src/Stache/Stores/AssetContainersStore.php | 3 ++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index 020d6f7833..8067ece8a3 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -50,6 +50,8 @@ class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract, protected $sortField; protected $sortDirection; + protected $validate; + public function id($id = null) { // For files, the handle is the ID. @@ -99,6 +101,11 @@ public function title($title = null) ->args(func_get_args()); } + public function validate($validate = null) + { + return $this->fluentlyGetOrSet('validate')->args(func_get_args()); + } + public function diskPath() { return rtrim($this->disk()->path('/'), '/'); diff --git a/src/Http/Controllers/CP/Assets/AssetsController.php b/src/Http/Controllers/CP/Assets/AssetsController.php index 1a76894d29..9e0788f07a 100644 --- a/src/Http/Controllers/CP/Assets/AssetsController.php +++ b/src/Http/Controllers/CP/Assets/AssetsController.php @@ -69,15 +69,17 @@ public function store(Request $request) $request->validate([ 'container' => 'required', 'folder' => 'required', - 'file' => ['file', new AllowedFile], ]); $container = AssetContainer::find($request->container); abort_unless($container->allowUploads(), 403); - $this->authorize('store', [AssetContract::class, $container]); + $request->validate([ + 'file' => array_merge(['file', new AllowedFile], $container->validate()), + ]); + $file = $request->file('file'); $path = ltrim($request->folder.'/'.$file->getClientOriginalName(), '/'); diff --git a/src/Stache/Stores/AssetContainersStore.php b/src/Stache/Stores/AssetContainersStore.php index 536ea7b000..5cbb7f8bce 100644 --- a/src/Stache/Stores/AssetContainersStore.php +++ b/src/Stache/Stores/AssetContainersStore.php @@ -30,6 +30,7 @@ public function makeItemFromFile($path, $contents) ->warmPresets(Arr::get($data, 'warm_presets')) ->searchIndex(Arr::get($data, 'search_index')) ->sortField(Arr::get($data, 'sort_by')) - ->sortDirection(Arr::get($data, 'sort_dir')); + ->sortDirection(Arr::get($data, 'sort_dir')) + ->validate(Arr::get($data, 'validate')); } } From 358cc54b824ebe44b64fe1759466c9f8d07fca5e Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 14:57:53 -0700 Subject: [PATCH 02/23] tidy --- src/Assets/AssetContainer.php | 12 +++++++++--- src/Contracts/Assets/AssetContainer.php | 8 ++++++++ src/Http/Controllers/CP/Assets/AssetsController.php | 2 +- src/Stache/Stores/AssetContainersStore.php | 2 +- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index 8067ece8a3..7da97fc1ec 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -50,7 +50,7 @@ class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract, protected $sortField; protected $sortDirection; - protected $validate; + protected $validation; public function id($id = null) { @@ -101,9 +101,15 @@ public function title($title = null) ->args(func_get_args()); } - public function validate($validate = null) + /** + * Get or set the validation rules. + * + * @param null|array $id + * @return array|null + */ + public function validation($rules = null) { - return $this->fluentlyGetOrSet('validate')->args(func_get_args()); + return $this->fluentlyGetOrSet('validation')->args(func_get_args()); } public function diskPath() diff --git a/src/Contracts/Assets/AssetContainer.php b/src/Contracts/Assets/AssetContainer.php index 756e74e3c6..f54e74ec58 100644 --- a/src/Contracts/Assets/AssetContainer.php +++ b/src/Contracts/Assets/AssetContainer.php @@ -101,4 +101,12 @@ public function accessible(); * @return bool */ public function private(); + + /** + * Get or set the validation rules. + * + * @param null|array $rules + * @return array|null + */ + public function validation($rules = null); } diff --git a/src/Http/Controllers/CP/Assets/AssetsController.php b/src/Http/Controllers/CP/Assets/AssetsController.php index 9e0788f07a..dba84519e4 100644 --- a/src/Http/Controllers/CP/Assets/AssetsController.php +++ b/src/Http/Controllers/CP/Assets/AssetsController.php @@ -77,7 +77,7 @@ public function store(Request $request) $this->authorize('store', [AssetContract::class, $container]); $request->validate([ - 'file' => array_merge(['file', new AllowedFile], $container->validate()), + 'file' => array_merge(['file', new AllowedFile], $container->validation()), ]); $file = $request->file('file'); diff --git a/src/Stache/Stores/AssetContainersStore.php b/src/Stache/Stores/AssetContainersStore.php index 5cbb7f8bce..fcba3465da 100644 --- a/src/Stache/Stores/AssetContainersStore.php +++ b/src/Stache/Stores/AssetContainersStore.php @@ -31,6 +31,6 @@ public function makeItemFromFile($path, $contents) ->searchIndex(Arr::get($data, 'search_index')) ->sortField(Arr::get($data, 'sort_by')) ->sortDirection(Arr::get($data, 'sort_dir')) - ->validate(Arr::get($data, 'validate')); + ->validation(Arr::get($data, 'validate')); } } From b2e7a40e7c4a91f51e185da8a5d3d9dcdeb92c86 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 14:58:05 -0700 Subject: [PATCH 03/23] =?UTF-8?q?show=20the=20first=20message=20if=20it?= =?UTF-8?q?=E2=80=99s=20a=20validation=20error?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resources/js/components/assets/Uploader.vue | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/resources/js/components/assets/Uploader.vue b/resources/js/components/assets/Uploader.vue index 9b18d9def4..2936ae1bfb 100644 --- a/resources/js/components/assets/Uploader.vue +++ b/resources/js/components/assets/Uploader.vue @@ -186,7 +186,7 @@ export default { response.status === 200 ? this.handleUploadSuccess(id, json) - : this.handleUploadError(id, status, json); + : this.handleUploadError(id, response.status, json); }); }, @@ -204,6 +204,10 @@ export default { } else { msg = __('Upload failed. The file might be larger than is allowed by your server.'); } + } else { + if (status === 422) { + msg = Object.values(response.errors)[0][0]; + } } upload.errorMessage = msg; this.$emit('error', upload, this.uploads); From 19816d4cde9ec79b0ee5a5fb92b049380353f268 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 15:02:01 -0700 Subject: [PATCH 04/23] comment --- resources/js/components/assets/Uploader.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/js/components/assets/Uploader.vue b/resources/js/components/assets/Uploader.vue index 2936ae1bfb..5606865188 100644 --- a/resources/js/components/assets/Uploader.vue +++ b/resources/js/components/assets/Uploader.vue @@ -206,7 +206,7 @@ export default { } } else { if (status === 422) { - msg = Object.values(response.errors)[0][0]; + msg = Object.values(response.errors)[0][0]; // ugly I know, but this gets the first error message when we don't know the key (might not be `file`) } } upload.errorMessage = msg; From 9b4df71dfa6ee82e40464dd98c7bf07610f684ba Mon Sep 17 00:00:00 2001 From: Erin Dalzell Date: Wed, 29 May 2024 15:06:22 -0700 Subject: [PATCH 05/23] spacing --- src/Assets/AssetContainer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index 7da97fc1ec..f42bb731ee 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -49,7 +49,6 @@ class AssetContainer implements Arrayable, ArrayAccess, AssetContainerContract, protected $withEvents = true; protected $sortField; protected $sortDirection; - protected $validation; public function id($id = null) From 1638b22395fa115b4ca4957d09d24d9b67e314ea Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 15:23:14 -0700 Subject: [PATCH 06/23] set default --- src/Assets/AssetContainer.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index f42bb731ee..2721cfdc07 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -108,7 +108,10 @@ public function title($title = null) */ public function validation($rules = null) { - return $this->fluentlyGetOrSet('validation')->args(func_get_args()); + return $this + ->fluentlyGetOrSet('validation') + ->getter(fn ($rules) => $rules ?? []) + ->args(func_get_args()); } public function diskPath() From bf82d1e2fd328a8278b10c5641dd75c15f5f4014 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 15:33:29 -0700 Subject: [PATCH 07/23] tests --- tests/Assets/AssetContainerTest.php | 12 ++++++++++++ tests/Feature/Assets/StoreAssetTest.php | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/tests/Assets/AssetContainerTest.php b/tests/Assets/AssetContainerTest.php index 40c2b53996..5e76b1794d 100644 --- a/tests/Assets/AssetContainerTest.php +++ b/tests/Assets/AssetContainerTest.php @@ -230,6 +230,18 @@ public function it_gets_and_sets_whether_downloading_is_allowed() $this->assertFalse($container->allowDownloading()); } + /** @test */ + public function it_gets_and_sets_the_validation_rules() + { + $container = new AssetContainer; + $this->assertNull($container->validation()); + + $return = $container->validation(['max:5120']); + + $this->assertEquals($container, $return); + $this->assertEquals(['max:5120'], $container->validation()); + } + /** @test */ public function it_gets_and_sets_glide_source_preset_for_upload_processing() { diff --git a/tests/Feature/Assets/StoreAssetTest.php b/tests/Feature/Assets/StoreAssetTest.php index 5c059e8243..acd11648ec 100644 --- a/tests/Feature/Assets/StoreAssetTest.php +++ b/tests/Feature/Assets/StoreAssetTest.php @@ -93,6 +93,18 @@ public function it_doesnt_upload_without_a_folder() ])->assertStatus(422); } + /** @test */ + public function it_doesnt_upload_when_validation_fails() + { + + $this->container->validation(['extensions:png'])->save(); + + $this + ->actingAs($this->userWithPermission()) + ->submit() + ->assertStatus(422); + } + private function submit($overrides = []) { return $this->postJson(cp_route('assets.store'), $this->validPayload($overrides)); From 3a76e0df48c64870107b45d82355515330270955 Mon Sep 17 00:00:00 2001 From: edalzell Date: Wed, 29 May 2024 15:40:06 -0700 Subject: [PATCH 08/23] this is always an array --- tests/Assets/AssetContainerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Assets/AssetContainerTest.php b/tests/Assets/AssetContainerTest.php index 5e76b1794d..2902e2e57b 100644 --- a/tests/Assets/AssetContainerTest.php +++ b/tests/Assets/AssetContainerTest.php @@ -234,7 +234,7 @@ public function it_gets_and_sets_whether_downloading_is_allowed() public function it_gets_and_sets_the_validation_rules() { $container = new AssetContainer; - $this->assertNull($container->validation()); + $this->assertEmpty($container->validation()); $return = $container->validation(['max:5120']); From 7cfca2c8fe299d7afe9eb912cbdbc8b28cf71098 Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 30 May 2024 11:54:11 -0700 Subject: [PATCH 09/23] wip --- .../components/asset-containers/EditForm.vue | 82 ++++-- .../js/components/field-validation/Rules.vue | 265 ++++++++++++++++++ src/Assets/AssetContainer.php | 1 + .../CP/Assets/AssetContainersController.php | 5 +- 4 files changed, 332 insertions(+), 21 deletions(-) create mode 100644 resources/js/components/field-validation/Rules.vue diff --git a/resources/js/components/asset-containers/EditForm.vue b/resources/js/components/asset-containers/EditForm.vue index 0ba93c9854..2009cf3bae 100644 --- a/resources/js/components/asset-containers/EditForm.vue +++ b/resources/js/components/asset-containers/EditForm.vue @@ -1,30 +1,63 @@ diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index 2721cfdc07..c6854dd5c8 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -640,6 +640,7 @@ public function fileData() 'create_folders' => $this->createFolders, 'source_preset' => $this->sourcePreset, 'warm_presets' => $this->warmPresets, + 'validate' => $this->validation, ]; $array = Arr::removeNullValues(array_merge($array, [ diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index 57f0a50fa5..d380eb461d 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -74,7 +74,7 @@ public function edit($container) return view('statamic::assets.containers.edit', [ 'blueprint' => $blueprint->toPublishArray(), - 'values' => $fields->values(), + 'values' => $fields->values()->merge(['rules' => $container->validation()]), 'meta' => $fields->meta(), 'container' => $container, ]); @@ -99,7 +99,8 @@ public function update(Request $request, $container) ->allowUploads($values['allow_uploads']) ->createFolders($values['create_folders']) ->sourcePreset($values['source_preset']) - ->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']); + ->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']) + ->validation($request->get('rules')); $container->save(); From 42c95ae513fb83af3b61ccf12892baaf6db763ff Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 30 May 2024 12:06:52 -0700 Subject: [PATCH 10/23] tidy --- resources/js/components/asset-containers/EditForm.vue | 1 - 1 file changed, 1 deletion(-) diff --git a/resources/js/components/asset-containers/EditForm.vue b/resources/js/components/asset-containers/EditForm.vue index 2009cf3bae..0224693a7e 100644 --- a/resources/js/components/asset-containers/EditForm.vue +++ b/resources/js/components/asset-containers/EditForm.vue @@ -45,7 +45,6 @@ /> - From 8ac697e0868bf9388e08dbddcea3351fc8fa9322 Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 30 May 2024 12:06:57 -0700 Subject: [PATCH 11/23] make sure it saves --- tests/Assets/AssetContainerTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/Assets/AssetContainerTest.php b/tests/Assets/AssetContainerTest.php index 2902e2e57b..180508264e 100644 --- a/tests/Assets/AssetContainerTest.php +++ b/tests/Assets/AssetContainerTest.php @@ -289,6 +289,21 @@ public static function warmPresetProvider() ]; } + /** @test */ + public function it_saves_the_container_data_to_file() + { + $container = new AssetContainer; + + $container + ->handle('test') + ->validation(['max:10']) + ->save(); + + $return = Facades\AssetContainer::find('test'); + + $this->assertEquals($container, $return); + } + /** @test */ public function it_saves_the_container_through_the_api() { From f1baf629b3e787c00f903a249c44ef1c2bdb2903 Mon Sep 17 00:00:00 2001 From: Erin Dalzell Date: Thu, 30 May 2024 13:24:32 -0700 Subject: [PATCH 12/23] Discard changes to resources/js/components/asset-containers/EditForm.vue --- .../components/asset-containers/EditForm.vue | 83 +++++-------------- 1 file changed, 20 insertions(+), 63 deletions(-) diff --git a/resources/js/components/asset-containers/EditForm.vue b/resources/js/components/asset-containers/EditForm.vue index 0224693a7e..0ba93c9854 100644 --- a/resources/js/components/asset-containers/EditForm.vue +++ b/resources/js/components/asset-containers/EditForm.vue @@ -1,62 +1,30 @@ From 1cb6ce797f6839ccd21f474abb3f295b27ea8fee Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 30 May 2024 13:34:59 -0700 Subject: [PATCH 14/23] load/save the validation --- .../CP/Assets/AssetContainersController.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index d380eb461d..103ff6e92b 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -65,6 +65,7 @@ public function edit($container) 'source_preset' => $container->sourcePreset(), 'warm_intelligent' => $intelligent = $container->warmsPresetsIntelligently(), 'warm_presets' => $intelligent ? [] : $container->warmPresets(), + 'validation' => $container->validation(), ]; $fields = ($blueprint = $this->formBlueprint($container)) @@ -100,7 +101,7 @@ public function update(Request $request, $container) ->createFolders($values['create_folders']) ->sourcePreset($values['source_preset']) ->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']) - ->validation($request->get('rules')); + ->validation($values['validation'] ?? null); $container->save(); @@ -266,6 +267,11 @@ protected function formBlueprint($container = null) 'instructions' => __('statamic::messages.asset_container_quick_download_instructions'), 'default' => true, ], + 'validation' => [ + 'type' => 'taggable', + 'display' => __('Validation Rules'), + 'instructions' => __('statamic::messages.asset_container_quick_download_instructions'), + ], ], ], ]); @@ -316,7 +322,7 @@ private function expandedGlidePresetOptions() private function expandedGlidePresetLabel($handle, $params) { - $separator = '-'; + $separator = '-'; $params = collect($params) ->map(function ($value, $param) { From 91be15e0fd17184f32346cb40e3caafa6a7445a3 Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 30 May 2024 13:37:22 -0700 Subject: [PATCH 15/23] not needed now --- src/Http/Controllers/CP/Assets/AssetContainersController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index 103ff6e92b..dc1503591c 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -75,7 +75,7 @@ public function edit($container) return view('statamic::assets.containers.edit', [ 'blueprint' => $blueprint->toPublishArray(), - 'values' => $fields->values()->merge(['rules' => $container->validation()]), + 'values' => $fields->values(), 'meta' => $fields->meta(), 'container' => $container, ]); From c42a57c4fb316adb0581a6fd53f2820beaf937fc Mon Sep 17 00:00:00 2001 From: edalzell Date: Thu, 30 May 2024 13:42:16 -0700 Subject: [PATCH 16/23] instructions --- resources/lang/en/messages.php | 1 + src/Http/Controllers/CP/Assets/AssetContainersController.php | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 2f0aec2f17..1792d0a3b5 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -21,6 +21,7 @@ 'asset_container_title_instructions' => 'Usually a plural noun, like Images or Documents', 'asset_container_warm_intelligent_instructions' => 'Generate appropriate presets on upload.', 'asset_container_warm_presets_instructions' => 'Specify which presets to generate on upload.', + 'asset_container_validation_rules_instructions' => 'Add any file validation rules.', 'asset_folders_directory_instructions' => 'We recommend avoiding spaces and special characters to keep URLs clean.', 'asset_replace_confirmation' => 'References to this asset within content will be updated to the asset you select below.', 'asset_reupload_confirmation' => 'Are you sure you want to reupload this asset?', diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index dc1503591c..b8ab0796dc 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -270,7 +270,7 @@ protected function formBlueprint($container = null) 'validation' => [ 'type' => 'taggable', 'display' => __('Validation Rules'), - 'instructions' => __('statamic::messages.asset_container_quick_download_instructions'), + 'instructions' => __('statamic::messages.asset_container_validation_rules_instructions'), ], ], ], From a74a8c14f8a703deeb0ad5dde6f490a7c5b24405 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 13:06:12 -0400 Subject: [PATCH 17/23] can't add it now or it would be a breaking change --- src/Contracts/Assets/AssetContainer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/Assets/AssetContainer.php b/src/Contracts/Assets/AssetContainer.php index f54e74ec58..aefdba540b 100644 --- a/src/Contracts/Assets/AssetContainer.php +++ b/src/Contracts/Assets/AssetContainer.php @@ -108,5 +108,5 @@ public function private(); * @param null|array $rules * @return array|null */ - public function validation($rules = null); + // public function validation($rules = null); } From 65c05436fbc1eee98db377b01befb36d05aeba9e Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 13:11:06 -0400 Subject: [PATCH 18/23] boop --- src/Http/Controllers/CP/Assets/AssetContainersController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index b8ab0796dc..6b3f0eea0a 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -322,7 +322,7 @@ private function expandedGlidePresetOptions() private function expandedGlidePresetLabel($handle, $params) { - $separator = '-'; + $separator = '-'; $params = collect($params) ->map(function ($value, $param) { From b89e85572ebafb60837a343cd3de33116e851c30 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 13:37:32 -0400 Subject: [PATCH 19/23] consistent docblocks even though the return is technically only half right, but it matches the other fluent ones for now --- src/Assets/AssetContainer.php | 4 ++-- src/Contracts/Assets/AssetContainer.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index c6854dd5c8..f510a812b2 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -103,8 +103,8 @@ public function title($title = null) /** * Get or set the validation rules. * - * @param null|array $id - * @return array|null + * @param null|array $rules + * @return array */ public function validation($rules = null) { diff --git a/src/Contracts/Assets/AssetContainer.php b/src/Contracts/Assets/AssetContainer.php index aefdba540b..ea9eeb246b 100644 --- a/src/Contracts/Assets/AssetContainer.php +++ b/src/Contracts/Assets/AssetContainer.php @@ -106,7 +106,7 @@ public function private(); * Get or set the validation rules. * * @param null|array $rules - * @return array|null + * @return array */ // public function validation($rules = null); } From 4a8de8e6ca768182e1c0e28907f003b63e3c3d55 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 13:38:32 -0400 Subject: [PATCH 20/23] call it validationRules to be a bit more explicit --- src/Assets/AssetContainer.php | 2 +- src/Contracts/Assets/AssetContainer.php | 2 +- .../Controllers/CP/Assets/AssetContainersController.php | 4 ++-- src/Http/Controllers/CP/Assets/AssetsController.php | 2 +- src/Stache/Stores/AssetContainersStore.php | 2 +- tests/Assets/AssetContainerTest.php | 8 ++++---- tests/Feature/Assets/StoreAssetTest.php | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Assets/AssetContainer.php b/src/Assets/AssetContainer.php index f510a812b2..8322b8b649 100644 --- a/src/Assets/AssetContainer.php +++ b/src/Assets/AssetContainer.php @@ -106,7 +106,7 @@ public function title($title = null) * @param null|array $rules * @return array */ - public function validation($rules = null) + public function validationRules($rules = null) { return $this ->fluentlyGetOrSet('validation') diff --git a/src/Contracts/Assets/AssetContainer.php b/src/Contracts/Assets/AssetContainer.php index ea9eeb246b..138e071317 100644 --- a/src/Contracts/Assets/AssetContainer.php +++ b/src/Contracts/Assets/AssetContainer.php @@ -108,5 +108,5 @@ public function private(); * @param null|array $rules * @return array */ - // public function validation($rules = null); + // public function validationRules($rules = null); } diff --git a/src/Http/Controllers/CP/Assets/AssetContainersController.php b/src/Http/Controllers/CP/Assets/AssetContainersController.php index 6b3f0eea0a..86ed6351b9 100644 --- a/src/Http/Controllers/CP/Assets/AssetContainersController.php +++ b/src/Http/Controllers/CP/Assets/AssetContainersController.php @@ -65,7 +65,7 @@ public function edit($container) 'source_preset' => $container->sourcePreset(), 'warm_intelligent' => $intelligent = $container->warmsPresetsIntelligently(), 'warm_presets' => $intelligent ? [] : $container->warmPresets(), - 'validation' => $container->validation(), + 'validation' => $container->validationRules(), ]; $fields = ($blueprint = $this->formBlueprint($container)) @@ -101,7 +101,7 @@ public function update(Request $request, $container) ->createFolders($values['create_folders']) ->sourcePreset($values['source_preset']) ->warmPresets($values['warm_intelligent'] ? null : $values['warm_presets']) - ->validation($values['validation'] ?? null); + ->validationRules($values['validation'] ?? null); $container->save(); diff --git a/src/Http/Controllers/CP/Assets/AssetsController.php b/src/Http/Controllers/CP/Assets/AssetsController.php index dba84519e4..a276c56ba8 100644 --- a/src/Http/Controllers/CP/Assets/AssetsController.php +++ b/src/Http/Controllers/CP/Assets/AssetsController.php @@ -77,7 +77,7 @@ public function store(Request $request) $this->authorize('store', [AssetContract::class, $container]); $request->validate([ - 'file' => array_merge(['file', new AllowedFile], $container->validation()), + 'file' => array_merge(['file', new AllowedFile], $container->validationRules()), ]); $file = $request->file('file'); diff --git a/src/Stache/Stores/AssetContainersStore.php b/src/Stache/Stores/AssetContainersStore.php index fcba3465da..05a1ccc040 100644 --- a/src/Stache/Stores/AssetContainersStore.php +++ b/src/Stache/Stores/AssetContainersStore.php @@ -31,6 +31,6 @@ public function makeItemFromFile($path, $contents) ->searchIndex(Arr::get($data, 'search_index')) ->sortField(Arr::get($data, 'sort_by')) ->sortDirection(Arr::get($data, 'sort_dir')) - ->validation(Arr::get($data, 'validate')); + ->validationRules(Arr::get($data, 'validate')); } } diff --git a/tests/Assets/AssetContainerTest.php b/tests/Assets/AssetContainerTest.php index 180508264e..70eeebc959 100644 --- a/tests/Assets/AssetContainerTest.php +++ b/tests/Assets/AssetContainerTest.php @@ -234,12 +234,12 @@ public function it_gets_and_sets_whether_downloading_is_allowed() public function it_gets_and_sets_the_validation_rules() { $container = new AssetContainer; - $this->assertEmpty($container->validation()); + $this->assertEmpty($container->validationRules()); - $return = $container->validation(['max:5120']); + $return = $container->validationRules(['max:5120']); $this->assertEquals($container, $return); - $this->assertEquals(['max:5120'], $container->validation()); + $this->assertEquals(['max:5120'], $container->validationRules()); } /** @test */ @@ -296,7 +296,7 @@ public function it_saves_the_container_data_to_file() $container ->handle('test') - ->validation(['max:10']) + ->validationRules(['max:10']) ->save(); $return = Facades\AssetContainer::find('test'); diff --git a/tests/Feature/Assets/StoreAssetTest.php b/tests/Feature/Assets/StoreAssetTest.php index acd11648ec..93ff220137 100644 --- a/tests/Feature/Assets/StoreAssetTest.php +++ b/tests/Feature/Assets/StoreAssetTest.php @@ -97,7 +97,7 @@ public function it_doesnt_upload_without_a_folder() public function it_doesnt_upload_when_validation_fails() { - $this->container->validation(['extensions:png'])->save(); + $this->container->validationRules(['extensions:png'])->save(); $this ->actingAs($this->userWithPermission()) From 4b29543629a2729d33cf7f943c5327c9102265db Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 14:44:44 -0400 Subject: [PATCH 21/23] nitpick --- resources/js/components/assets/Uploader.vue | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/js/components/assets/Uploader.vue b/resources/js/components/assets/Uploader.vue index 5606865188..4c16383972 100644 --- a/resources/js/components/assets/Uploader.vue +++ b/resources/js/components/assets/Uploader.vue @@ -206,7 +206,7 @@ export default { } } else { if (status === 422) { - msg = Object.values(response.errors)[0][0]; // ugly I know, but this gets the first error message when we don't know the key (might not be `file`) + msg = Object.values(response.errors)[0][0]; // Get first validation message. } } upload.errorMessage = msg; From 100b01d773daed75f95927a2b4dfbd872b623fa3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 14:48:24 -0400 Subject: [PATCH 22/23] move test --- tests/Assets/AssetContainerTest.php | 15 --------------- tests/Stache/Stores/AssetContainersStoreTest.php | 5 ++++- 2 files changed, 4 insertions(+), 16 deletions(-) diff --git a/tests/Assets/AssetContainerTest.php b/tests/Assets/AssetContainerTest.php index 70eeebc959..41731c0459 100644 --- a/tests/Assets/AssetContainerTest.php +++ b/tests/Assets/AssetContainerTest.php @@ -289,21 +289,6 @@ public static function warmPresetProvider() ]; } - /** @test */ - public function it_saves_the_container_data_to_file() - { - $container = new AssetContainer; - - $container - ->handle('test') - ->validationRules(['max:10']) - ->save(); - - $return = Facades\AssetContainer::find('test'); - - $this->assertEquals($container, $return); - } - /** @test */ public function it_saves_the_container_through_the_api() { diff --git a/tests/Stache/Stores/AssetContainersStoreTest.php b/tests/Stache/Stores/AssetContainersStoreTest.php index b3ea2404d9..98fb48e9a3 100644 --- a/tests/Stache/Stores/AssetContainersStoreTest.php +++ b/tests/Stache/Stores/AssetContainersStoreTest.php @@ -125,12 +125,15 @@ public function it_saves_to_disk() EOT; $this->assertStringEqualsFile($this->tempDir.'/new.yaml', $expected); - $container->allowUploads(false)->createFolders(false)->save(); + $container->allowUploads(false)->createFolders(false)->validationRules(['max:150', 'mimes:jpg'])->save(); $expected = <<<'EOT' title: 'New Container' allow_uploads: false create_folders: false +validate: + - 'max:150' + - 'mimes:jpg' EOT; $this->assertStringEqualsFile($this->tempDir.'/new.yaml', $expected); From d494926721d58f4473dbbfd954cd4e1113e1abe7 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Tue, 4 Jun 2024 14:50:24 -0400 Subject: [PATCH 23/23] wording --- resources/lang/en/messages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index 1792d0a3b5..324478e73c 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -21,7 +21,7 @@ 'asset_container_title_instructions' => 'Usually a plural noun, like Images or Documents', 'asset_container_warm_intelligent_instructions' => 'Generate appropriate presets on upload.', 'asset_container_warm_presets_instructions' => 'Specify which presets to generate on upload.', - 'asset_container_validation_rules_instructions' => 'Add any file validation rules.', + 'asset_container_validation_rules_instructions' => 'These rules will be applied to uploaded files.', 'asset_folders_directory_instructions' => 'We recommend avoiding spaces and special characters to keep URLs clean.', 'asset_replace_confirmation' => 'References to this asset within content will be updated to the asset you select below.', 'asset_reupload_confirmation' => 'Are you sure you want to reupload this asset?',