自定义表单验证由于某些原因无法正常工作

时间:2019-07-11 03:44:05

标签: php laravel

我正在使用toastr对自定义表单验证进行答复。出于某种原因,确实可以通过显示烤面包机通知example来启动验证。但这不会阻止用户创建回复。

我尝试了覆盖库存验证功能,例如: formatValidationErrors()failedValidation() 第二个功能是唯一可以使用的功能(显示烤面包机通知),但是它给了我上面的问题。

App \ Http \ Controllers \ ReplyController.php

这是存储方法。 ThreadReceivedReply事件包含在文件的顶部。

public function store(Thread $thread, ReplyForm $form)
{
    if (Gate::denies('create', new Reply)) {
        toastr()->error('You are creating too many replies. Please wait 30 seconds', 'Slow Down');
        return back();
    }

    $reply = Reply::create([
        'body' => Purifier::clean(request('body')),
        'user_steamid' => auth()->user()->steamid
    ]);

    event(new ThreadReceivedReply($reply));

    if($reply->user_steamid != $thread->user_steamid) {
        $thread->user->notify(new RepliedToThread($thread));
    };

    $thread->replies()->save($reply);

    Cache::forget('replies');

    toastr()->success('Reply created. Nice job');
    return back();
}

App \ Http \ Forms \ ReplyForm.php

<?php

namespace App\Http\Forms;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'body' => 'required|max:1000|spamfree'
        ];
    }

    public function messages()
    {
        return [
            'body.required' => 'Your reply needs body text!'
        ];
    }

    protected function failedValidation(Validator $validator)
    {

        $messages = $validator->messages();

        foreach ($messages->all() as $message)
        {
            toastr()->error($message, 'Woops');
        }

        return $validator->errors()->all();
    }
}

我有点想this post来解决此问题,但并没有使我走得太远。 预期的结果是,当您不输入身体信息时,它将显示验证错误作为祝酒词。我会向您显示错误,但未显示任何错误。如果您需要更多信息,请随时询问我。谢谢您的提前帮助!

0 个答案:

没有答案