如何根据发布请求处理验证消息

时间:2018-10-05 09:56:45

标签: php laravel routes

将通过验证规则检查发布请求。如果发生验证错误,则响应将返回到Route之后。

Route::post('invitation/result', 'UsernameController@result')->name('result.username');

在此之后,我返回一个错误no Message,因为在视图中需要两个参数($ group,$ parameter)。

<div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
            <div class="panel-heading">In welche Gruppe wollen sie den User {{$user->name}} einladen?</div>
                <div class="panel-body">
                @foreach($errors->all() as $error)
                    <div class="alert alert-danger">
                        {{$error}}
                    </div>
                @endforeach

                    {!! Form::open(array('route'=>'store.invitation')) !!} 
                        <div class="form-group">
                            <input type="hidden" name="iduser" value="{{ $user->id }} "/>
                            <select class="form-control" name="idgroup">
                                @foreach ($groups as $group)
                                    <option value="{{ $group->id }}">{{ $group->name }}</option>
                                @endforeach
                            </select>
                            {{ csrf_field() }}
                        </div>
                        <div>
                            {{Form::submit('User einladen',['class' => 'btn btn-primary'])}}
                        </div>
                    {!! Form::close() !!} 

                </div>
            </div>
        </div>
    </div>

是否可以将我在请求中提供的参数(如“ required”)退回?

$request->validate([
        'idgroup' => [
            'required'
        ],

谢谢

编辑:这不是“必需的”验证,而是我在这种情况下需要的另一个验证。

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use App\Invitation;
use App\Group;
use App\User;

class AlreadyInvitation implements Rule
{
    private $groupname;
    private $username;


    public function passes($attribute, $value)
    {
        $idgroup = request('idgroup');
        $alreadyinvitation = Invitation::where([['iduser', $value], ['idgroup', $idgroup], ['active', 1]])->select('active')->first();
        if(empty($alreadyinvitation)){
            return true;
        }else{
            $this->groupname = Group::where('id', $idgroup)->select('name')->first();
            $this->username = User::where('id', $value)->select('name')->first();
            return false;
        }
    }


    public function message()
    {

        return 'The User ' .$this->username->name. ' has an active request for Group ' .$this->groupname->name. ';
    }
}

0 个答案:

没有答案