Skip to content

Commit

Permalink
Fix #59 - Add asset validation
Browse files Browse the repository at this point in the history
  • Loading branch information
riasvdv committed Mar 11, 2022
1 parent 9ecef00 commit 6938e4c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/js/responsive.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion resources/js/ResponsiveFieldtype.vue
Expand Up @@ -9,6 +9,7 @@
:read-only="isReadOnly"
class="form-group bg-white border-t border-b"
:class="field.type === 'section' ? 'mt-px -mb-px' : ''"
:errors="errors(field.handle)"
@meta-updated="metaUpdated(field.handle, $event)"
@focus="$emit('focus')"
@blur="$emit('blur')"
Expand All @@ -33,10 +34,18 @@ export default {
})
.values()
.value();
}
},
storeState() {
return this.$store.state.publish['base'] || {};
},
},
methods: {
errors(handle) {
return this.storeState.errors[this.handle + '.' + handle] || [];
},
updateKey(handle, value) {
let responsiveValue = this.value;
Vue.set(responsiveValue, handle, value);
Expand Down
2 changes: 2 additions & 0 deletions src/Fieldtypes/ResponsiveFields.php
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\ResponsiveImages\Fieldtypes;

use Statamic\Facades\AssetContainer;
use Statamic\Fieldtypes\Assets\ImageRule;

class ResponsiveFields
{
Expand Down Expand Up @@ -74,6 +75,7 @@ public function getConfig(): array
? ($this->config['allow_ratio'] ? ($this->config['allow_fit'] ? 25 : 33) : 66)
: ($this->config['allow_ratio'] ? ($this->config['allow_fit'] ? 50 : 66) : 100),
'required' => in_array('required', $this->validate ?? []) && $index === 0,
'validate' => [new ImageRule()],
],
];

Expand Down
25 changes: 24 additions & 1 deletion src/Fieldtypes/ResponsiveFieldtype.php
Expand Up @@ -6,10 +6,14 @@
use Spatie\ResponsiveImages\Fieldtypes\ResponsiveFields as ResponsiveFields;
use Spatie\ResponsiveImages\GraphQL\ResponsiveFieldType as GraphQLResponsiveFieldtype;
use Spatie\ResponsiveImages\Responsive;
use Spatie\ResponsiveImages\Tags\ResponsiveTag;
use Statamic\Assets\Asset;
use Statamic\Facades\Blueprint;
use Statamic\Facades\GraphQL;
use Statamic\Fields\Fields as BlueprintFields;
use Statamic\Fields\Fieldtype;
use Statamic\Fieldtypes\Assets\Assets;
use Statamic\Fieldtypes\Assets\ImageRule;
use Statamic\Support\Arr;
use Statamic\Tags\Context;
use Statamic\Tags\Parameters;
Expand Down Expand Up @@ -114,6 +118,25 @@ protected function fieldConfig()
return ResponsiveFields::new($this->config())->getConfig();
}

public function extraRules(): array
{
$rules = collect($this->fieldConfig())->mapWithKeys(function ($field) {
if ($field['field']['required'] ?? false) {
$rules = ['required'];
} else {
$rules = ['nullable'];
}

$prefixedHandle = $this->field()->handle() . '.' . $field['handle'];

return [
$prefixedHandle => array_merge($rules, $field['field']['validate'] ?? [])
];
});

return $rules->toArray();
}

public function preProcess($data)
{
return $this->fields()->addValues($data ?? [])->preProcess()->values()->all();
Expand All @@ -123,7 +146,7 @@ public function preProcessIndex($data)
{
$data = $this->augment($data);

if (! isset($data['src'])) {
if (!isset($data['src'])) {
return [];
}

Expand Down

0 comments on commit 6938e4c

Please sign in to comment.