Skip to content

Commit

Permalink
[4.x] Only namespace asset validation attributes when on a CP route (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Nov 14, 2023
1 parent 0862875 commit 4861762
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Fieldtypes/Assets/DimensionsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Validation\Rule;
use Statamic\Facades\Asset;
use Statamic\Statamic;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class DimensionsRule implements Rule
Expand Down Expand Up @@ -63,7 +64,7 @@ public function passes($attribute, $value)
*/
public function message()
{
return __('statamic::validation.dimensions');
return __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.dimensions');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Fieldtypes/Assets/ImageRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Validation\Rule;
use Statamic\Facades\Asset;
use Statamic\Statamic;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class ImageRule implements Rule
Expand Down Expand Up @@ -46,6 +47,6 @@ public function passes($attribute, $value)
*/
public function message()
{
return __('statamic::validation.image');
return __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.image');
}
}
4 changes: 3 additions & 1 deletion src/Fieldtypes/Assets/MaxRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Fieldtypes\Assets;

use Statamic\Statamic;

class MaxRule extends SizeBasedRule
{
/**
Expand All @@ -22,6 +24,6 @@ public function sizePasses($size)
*/
public function message()
{
return str_replace(':max', $this->parameters[0], __('statamic::validation.max.file'));
return str_replace(':max', $this->parameters[0], __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.max.file'));
}
}
3 changes: 2 additions & 1 deletion src/Fieldtypes/Assets/MimesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Validation\Rule;
use Statamic\Facades\Asset;
use Statamic\Statamic;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class MimesRule implements Rule
Expand Down Expand Up @@ -48,6 +49,6 @@ public function passes($attribute, $value)
*/
public function message()
{
return str_replace(':values', implode(', ', $this->parameters), __('statamic::validation.mimes'));
return str_replace(':values', implode(', ', $this->parameters), __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.mimes'));
}
}
3 changes: 2 additions & 1 deletion src/Fieldtypes/Assets/MimetypesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Contracts\Validation\Rule;
use Statamic\Facades\Asset;
use Statamic\Statamic;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class MimetypesRule implements Rule
Expand Down Expand Up @@ -43,6 +44,6 @@ public function passes($attribute, $value)
*/
public function message()
{
return str_replace(':values', implode(', ', $this->parameters), __('statamic::validation.mimetypes'));
return str_replace(':values', implode(', ', $this->parameters), __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.mimetypes'));
}
}
4 changes: 3 additions & 1 deletion src/Fieldtypes/Assets/MinRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Fieldtypes\Assets;

use Statamic\Statamic;

class MinRule extends SizeBasedRule
{
/**
Expand All @@ -22,6 +24,6 @@ public function sizePasses($size)
*/
public function message()
{
return str_replace(':min', $this->parameters[0], __('statamic::validation.min.file'));
return str_replace(':min', $this->parameters[0], __((Statamic::isCpRoute() ? 'statamic::' : '').'validation.min.file'));
}
}
14 changes: 14 additions & 0 deletions tests/Fieldtypes/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public function it_shallow_augments_to_a_single_asset_when_max_files_is_one()
/** @test */
public function it_replaces_dimensions_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['dimensions:width=180,height=180']])->fieldRules();

$this->assertIsArray($replaced);
Expand All @@ -117,6 +119,8 @@ public function it_replaces_dimensions_rule()
/** @test */
public function it_replaces_image_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['image']])->fieldRules();

$this->assertIsArray($replaced);
Expand All @@ -128,6 +132,8 @@ public function it_replaces_image_rule()
/** @test */
public function it_replaces_mimes_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['mimes:jpg,png']])->fieldRules();

$this->assertIsArray($replaced);
Expand All @@ -139,6 +145,8 @@ public function it_replaces_mimes_rule()
/** @test */
public function it_replaces_mimestypes_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['mimetypes:image/jpg,image/png']])->fieldRules();

$this->assertIsArray($replaced);
Expand All @@ -150,6 +158,8 @@ public function it_replaces_mimestypes_rule()
/** @test */
public function it_replaces_min_filesize_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['min_filesize:100']])->fieldRules();

$this->assertIsArray($replaced);
Expand All @@ -161,6 +171,8 @@ public function it_replaces_min_filesize_rule()
/** @test */
public function it_replaces_max_filesize_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['max_filesize:100']])->fieldRules();

$this->assertIsArray($replaced);
Expand All @@ -172,6 +184,8 @@ public function it_replaces_max_filesize_rule()
/** @test */
public function it_doesnt_replace_non_image_related_rule()
{
config()->set('statamic.cp.route', '/');

$replaced = $this->fieldtype(['validate' => ['file']])->fieldRules();

$this->assertIsArray($replaced);
Expand Down

0 comments on commit 4861762

Please sign in to comment.