125 lines
4.8 KiB
PHP
125 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Rules\PublicFileExists;
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
|
|
class StoreEntryRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
$entry = $this->route('entry');
|
|
if( $entry )
|
|
return $this->user()->can('update', $entry);
|
|
|
|
return $this->user()->can('create', '\App\Models\Entry');
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function prepareForValidation(): void
|
|
{
|
|
|
|
$newGameTitle = trim((string) $this->input('new-game-title', ''));
|
|
$newGamePlatform = $this->input('new-game-platform');
|
|
$newGameGenre = $this->input('new-game-genre');
|
|
|
|
$this->merge([
|
|
'game_id' => $this->input('game_id') !== '' ? $this->input('game_id') : null,
|
|
'new-game-title' => $newGameTitle !== '' ? $newGameTitle : null,
|
|
'new-game-platform' => $newGamePlatform !== '' ? $newGamePlatform : null,
|
|
'new-game-genre' => $newGameGenre !== '' ? $newGameGenre : null,
|
|
'gallery' => $this->input('gallery') !== '' ? $this->input('gallery') : null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$rules = [];
|
|
$section = $this->route('section');
|
|
|
|
$rules['files_uuid'] = 'array|required|min:1';
|
|
$rules['files_uuid.*'] = 'string';
|
|
|
|
if( section_must_not_be( 'translations', $section ) ){
|
|
$rules['entry_title'] = "required|string|max:255";
|
|
} else {
|
|
$rules['entry_title'] = "nullable|string|max:255";
|
|
}
|
|
|
|
if( section_must_be( 'romhacks', $section ) ){
|
|
$rules['modifications'] = 'array|required|min:1';
|
|
$rules['modifications.*'] = 'integer|exists:modifications,id';
|
|
}
|
|
|
|
$rules['version'] = 'required|string|max:50';
|
|
$rules['release-date'] = 'required|date';
|
|
$rules['status'] = 'required|integer|exists:statuses,id';
|
|
$rules['description'] = 'required|string';
|
|
|
|
if( section_must_be( ['romhacks', 'translations' ], $section ) ){
|
|
$rules['game_id'] = 'required_without:new-game-title|nullable|integer|exists:games,id';
|
|
$rules['new-game-title'] = 'required_without:game_id|nullable|string|max:255';
|
|
$rules['new-game-platform'] = 'required_with:new-game-title|nullable|integer|exists:platforms,id';
|
|
$rules['new-game-genre'] = 'required_with:new-game-title|integer|nullable|exists:genres,id';
|
|
}
|
|
|
|
|
|
$rules['hashes'] = 'array|required|min:1';
|
|
$rules['hashes.*.filename'] = 'required|string|max:512';
|
|
$rules['hashes.*.hash_crc32'] = 'required|string|max:512';
|
|
$rules['hashes.*.hash_sha1'] = 'required|string|max:512';
|
|
$rules['hashes.*.verified'] = 'required|string|max:512';
|
|
|
|
$rules['languages'] = 'array|required|min:1';
|
|
$rules['languages.*'] = 'integer|exists:languages,id';
|
|
|
|
$rules['main-image'] = [ 'required', 'string', new PublicFileExists ];
|
|
$rules['gallery'] = 'array|required|min:1';
|
|
$rules['gallery.*'] = [ 'string', new PublicFileExists ];
|
|
|
|
$rules['authors'] = 'array|required_without:new-authors|min:1';
|
|
$rules['authors.*'] = 'integer|exists:authors,id';
|
|
$rules['new-authors'] = 'array|required_without:authors|min:1';
|
|
$rules['new-authors.*'] = 'string|max:255';
|
|
|
|
$rules['staff_credits'] = 'nullable|json';
|
|
$rules['release_site'] = 'nullable|url|max:500';
|
|
$rules['youtube_video'] = 'nullable|url|max:500';
|
|
|
|
$rules['submit-state'] = 'required|string|in:draft,pending,published';
|
|
|
|
return $rules;
|
|
}
|
|
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'entry_title.required' => 'Please provide an entry title.',
|
|
'slug.unique' => 'An entry with this title already exists. Please choose another title.',
|
|
'file_ids.required' => 'Please upload at least one file.',
|
|
'file_ids.min' => 'Please upload at least one file.',
|
|
'hashes.required' => 'Please add at least one hash.',
|
|
'hashes.min' => 'Please add at least one hash.',
|
|
'gallery.required' => 'Please add at least one screenshot.',
|
|
'gallery.min' => 'Please add at least one screenshot.',
|
|
'new-game-platform.required_without' => 'Please choose a platform for the new game.',
|
|
'new-game-genre.required_without' => 'Please choose a genre for the new game.',
|
|
];
|
|
}
|
|
}
|