laravel / nova-issues

554 stars 34 forks source link

BelongsTo field doesn't perform any validation #2692

Closed chrispage1 closed 4 years ago

chrispage1 commented 4 years ago

Description:

I've created a BelongsTo field which has validation rules required and exists, to check the existence of the key within the database before saving. However, if I leave the BelongsTo field empty, I don't get any validation errors but do get a database constraint violation because the database field cannot be null.

It seems to entirely pass the validation phase and straight to database insertion.

Steps To Reproduce:

Create a BelongsTo with validation rules:

\Laravel\Nova\Fields\BelongsTo::make('Series')
                ->required()
                ->rules(['required', 'exists:series,id'])

Hit save without populating the value and watch the errors roll in...

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'series_id' cannot be null (SQL: insert into `seasons` (`name`, `series_id`, `active`, `updated_at`, `created_at`) values (sdfsdfsdf, ?, 1, 2020-06-19 09:01:38, 2020-06-19 09:01:38))"`
alberto-bottarini commented 4 years ago

Please paste your model class

chrispage1 commented 4 years ago

@alberto-bottarini - thanks for your message. Here's the model -

<?php

namespace App\Domains\Series\Models;

use App\Domains\Locations\Models\Event;
use App\Domains\Series\Models\Series;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
 * Class Season
 *
 * @property Series  series
 * @property integer series_id
 *
 * @package App\Domains\Series\Models
 */
class Season extends Model
{
    public function series(): BelongsTo
    {
        return $this->belongsTo(Series::class);
    }

    public function events(): HasMany
    {
        return $this->hasMany(Event::class)
            ->orderBy('events.position', 'asc')
            ->orderBy('events.started_at', 'asc');
    }
}
alberto-bottarini commented 4 years ago

I got it!

rules method accepts strings, not an array: https://nova.laravel.com/docs/3.0/resources/validation.html#validation

alberto-bottarini commented 4 years ago

Is it fixed now?

jbrooksuk commented 4 years ago

@alberto-bottarini good try, but the rules field docblock says otherwise:

    /**
     * Set the validation rules for the field.
     *
     * @param  callable|array|string  $rules
     * @return $this
     */
alberto-bottarini commented 4 years ago

Ah ok, i'm sorry... I just read documentation and array syntax is never referenced

dniccum commented 4 years ago

I am currently experiencing this as well. With the code below:

BelongsTo::make(__('Jurisdiction'), 'jurisdiction')
    ->rules(function(NovaRequest $novaRequest) {
        $rules = [];

        if ($novaRequest->input('jurisdiction') === null && $novaRequest->input('role') !== Role::Jurisdiction) {
                $rules = [ 'required' ];
        }

                return $rules;
        })
        ->help('If the user is a jurisdiction user, select the jurisdiction (team) that this user will belong to.')

Nova will not evaluate the closure and will simply make the input required.

davidhemphill commented 4 years ago

I was not able to replicate your issue by using string validation or array validation:

image
github-actions[bot] commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.