Laravel - 更新日期的验证规则

时间:2016-11-28 09:40:37

标签: laravel

我有这个验证规则:

'event_start' => 'required|date|after:today',

并且在创建模型时一切都很好,这个日期不能在今天之前......

但是当用户尝试更新事件时,开始日期是在今天之前,并且弹出验证错误....有没有办法调整此更新模型?因此,当用户更新此规则时,仅检查它是否为日期?

更新

这是我对创建和更新的唯一验证检查:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class EventsForm extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        if( auth()->check() )
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [

            'title' => 'required|min:2|max:255',
            'event_start' => 'required|date|after:today',

        ];

        return $rules;
    }
}

4 个答案:

答案 0 :(得分:6)

您应该有不同的表单请求类来创建和更新事件。但这并不是说你不能使用继承。

如果您的更新方法只有与create方法不同,那么您可以扩展该方法的表单请求并修改更新规则:

class CreateEventRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name' => 'required|unique:events',
            'start_date' => 'required|date|after:today',
            'door_time' => 'required|date_format:"H:i:s",
            'location' => 'required',
        ];
    }
}

和...

class UpdateEventRequest extends CreateEventRequest
{
    public function rules()
    {
        // Get ID of event if using route–model binding
        $id = $this->route('event')->getKey();

        // Use array merge to override create event form request’s rules
        return array_merge(parent::rules(), [
            'name' => 'required|unique:events,name,'.$id,
            'start_date' => 'required|date',
        ]);
    }
}

您甚至可以将rules方法推送到抽象类,并让您的创建和更新表单请求类扩展 类:

abstract class EventFormRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        // Base event rules
    }
}

class CreateEventRequest extends EventFormRequest
{
    public function rules()
    {
        // Create-specific rules
    }
}

class UpdateEventRequest extends EventFormRequest
{
    public function rules()
    {
        // Update-specific rules
    }
}

答案 1 :(得分:1)

如果创建和更新逻辑不同,只需对每种方法使用不同的验证规则:

Title

答案 2 :(得分:1)

我假设验证规则位于HttpRequest上,然后您可以将验证规则更改为:

'event_start' => 'required|date',

在你的控制器上:

public function store(EventRequest $request)
{
       $validator = Validator::make($request->all(), [
            'event_start' => 'date|after:today',
       ]);

       if($validator->fails())
       {
           return redirect('event/create')
                    ->withErrors($validator)
                    ->withInput();
       }

       // Save the event
}

答案 3 :(得分:0)

使用单一规则方法来检查是否创建或更新并返回基于

的数组
public function rules()
{
    $rules = [

        'title' => 'required|min:2|max:255',
        'event_start' => 'required|date|after:today',

    ];
    // $this->route('event') will be null if its create so the defualt one is ok
    if($this->route('event')) {
       // modify rules for edit here 
       $rules['event_start'] => 'required|date';
       $rules['name'] => 'required|unique:events,name,'.$this->route('event')->id;
        // above rules will only be applied if update.
    }

    return $rules;
}