diff --git a/app/Nova/MediaType.php b/app/Nova/MediaType.php index 2d93c14d3..0e61ecc07 100644 --- a/app/Nova/MediaType.php +++ b/app/Nova/MediaType.php @@ -3,10 +3,12 @@ namespace App\Nova; use Illuminate\Http\Request; +use Illuminate\Validation\Rule; use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\Select; use Laravel\Nova\Fields\Text; use Laravel\Nova\Http\Requests\NovaRequest; +use Validator; class MediaType extends Resource { @@ -71,6 +73,32 @@ public function fields(Request $request) ]; } + /** + * Handle any post-validation processing. + * + * @param \Laravel\Nova\Http\Requests\NovaRequest $request + * @param \Illuminate\Validation\Validator $validator + * @return void + * @throws \Illuminate\Validation\ValidationException + */ + protected static function afterValidation(NovaRequest $request, $validator) + { + $type = $request->post('type'); + $name = $request->post('name'); + + $unique = Rule::unique(\App\Models\MediaType::TABLE_NAME)->where(function ($query) use($type, $name) { + return $query->where('type', $type)->where('name', $name); + }); + + $uniqueValidator = Validator::make($request->only('name'), [ + 'name' => [$unique], + ], [ + 'name' => __('validation.unique') + ]); + + $uniqueValidator->validate(); + } + /** * Get the cards available for the request. * diff --git a/config/app.php b/config/app.php index 298b5c064..b7bbffbc5 100644 --- a/config/app.php +++ b/config/app.php @@ -38,7 +38,7 @@ | or any other location as required by the application or its packages. */ - 'version' => '1.2.0-alpha.33', + 'version' => '1.2.0-alpha.34', /* |-------------------------------------------------------------------------- diff --git a/database/migrations/2018_08_17_103101_create_media_types_table.php b/database/migrations/2018_08_17_103101_create_media_types_table.php index 0e6a7f09d..b018abf9d 100644 --- a/database/migrations/2018_08_17_103101_create_media_types_table.php +++ b/database/migrations/2018_08_17_103101_create_media_types_table.php @@ -21,6 +21,11 @@ public function up() $table->string('description'); $table->timestamps(); }); + + Schema::table(MediaType::TABLE_NAME, function (Blueprint $table) { + // Set unique index constraints + $table->unique(['type', 'name']); + }); } /**