簡體   English   中英

Laravel required_if不起作用

[英]Laravel required_if not working

我的required_if驗證無效。 選擇“雙打”但未選擇“ team_mate2和對手2”時,不會輸出錯誤。 當雙打被選為比賽類型時,我需要表格要求第二個隊友和對手。

Match.php(模型)

<?php

class Match extends Eloquent{

    protected $table = 'matches';

    public static $rules = array(
        'match_type'=>'required',
        'associate'=>'alpha|min:2',
        'location'=>'required|alpha|min:2',
        'opponent'=>'numeric',
        'team_mate2'=>'required_if:match_type,doubles',
        'opponent2'=>'required_with:team_mate2'
    );

    public static $messages = array(
        'opponent.numeric'=>'You must select a player.'
    );

}

MatchController.php

public function postCreate()
{

    $validator = Validator::make(Input::all(), Match::$rules, Match::$messages);

    if ($validator->passes()) 
    {

        $datetime = Input::get('year')."-".Input::get('month')."-".Input::get('day')." ".Input::get('time').":00";

        $match = new Match;
        $match->type = Input::get('match_type');
        $match->associate = Input::get('associate');
        $match->date = $datetime;
        $match->location = Input::get('location');
        $match->save();

        $matchp1 = new Matchplayer;
        $matchp1->user_id = Input::get('id');
        $matchp1->match_id = $match->id;
        $matchp1->team = 1;
        $matchp1->save();

        $matchp2 = new Matchplayer;
        $matchp2->user_id = Input::get('opponent');
        $matchp2->match_id = $match->id;
        $matchp2->team = 2;
        $matchp2->save();

        return Redirect::to('members/matches/create')->with('message', 'Match created!');

    }else{
        return Redirect::to('members/matches/create')->withErrors($validator)->withInput();
    }
}

匹配創建視圖

        {{ Form::open(array('url'=>'members/matches/create', 'action' => 'post', 'class'=>'form-horizontal')) }}
        {{ form::hidden('id', Auth::user()->id) }}
        <div class="form-group">
            <label class="col-sm-5 control-label col-sm-pad">Match Type</label>
            <div class="col-sm-7 col-sm-pad">
                {{ $errors->first('match_type', '<span class="help-block">:message</span>') }}
                {{ Form::select('match_type', array('singles' => 'Singles', 'doubles' => 'Doubles'), 'singles', array('id' => 'match_type', 'class' => 'form-control input')) }}
            </div>
        </div>
        <div class="form-group">
            <label for="gender" class="col-sm-5 control-label col-sm-pad">League Associate</label>
            <div class="col-sm-7 col-sm-pad">
                {{ $errors->first('associate', '<span class="help-block">:message</span>') }}
                {{ Form::text('associate', Input::old('associate'), array('class' => 'form-control input')) }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-5 control-label col-sm-pad">Team Mate #1<br><small></small></label>
            <div class="col-sm-7 col-sm-pad">
                <p class="form-control-static">{{ Auth::user()->first_name." ".Auth::user()->last_name }}</p>
            </div>
        </div>
        <div class="form-group{{ $errors->first('opponent', ' has-error') }}">
            <label class="col-sm-5 control-label col-sm-pad">Opponent #1</label>
            <div class="col-sm-7 col-sm-pad">
                {{ Form::select('opponent', $users, 'key', array('class' => 'form-control input')) }}
                {{ $errors->first('opponent', '<span class="help-block">:message</span>') }}
            </div>
        </div>
        <div class="form-group{{ $errors->first('team_mate2', ' has-error') }}">
            <label class="col-sm-5 control-label col-sm-pad">Team Mate #2<br><small>(Doubles Only)</small></label>
            <div class="col-sm-7 col-sm-pad">
                {{ Form::select('team_mate2', $users, 'key', array('class' => 'form-control input')); }}
                {{ $errors->first('team_mate2', '<span class="help-block">:message</span>') }}
            </div>
        </div>
        <div class="form-group{{ $errors->first('opponent2', ' has-error') }}">
            <label class="col-sm-5 control-label col-sm-pad">Opponent #2<br><small>(Doubles Only)</small></label>
            <div class="col-sm-7 col-sm-pad">
                {{ Form::select('opponent2', $users, 'key', array('class' => 'form-control input')) }}
                {{ $errors->first('opponent2', '<span class="help-block">:message</span>') }}
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-5 control-label col-sm-pad">Date</label>
                {{ $errors->first('day', '<span class="help-block">:message</span>') }}
                <div class="col-lg-2 col-sm-pad">
                    {{ Form::selectRange('day', 1, 31, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
                </div>
                <div class="col-lg-2 col-sm-pad">
                    {{ Form::selectRange('month', 1, 12, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
                </div>
                <div class="col-lg-3 col-sm-pad">
                    {{ Form::selectRange('year', 2014, 2015, 'a', array('class' => 'form-control input-sm input dob-form-control')) }}
                </div>
        </div>
        <div class="form-group">
            <label class="col-sm-5 control-label col-sm-pad">Time</label>
            <div class="col-lg-3 col-sm-pad">
                {{ Form::select('time', $times, 'key', array('class' => 'form-control input input-sm dob-form-control')) }}
            </div>
        </div>
        <div class="form-group{{ $errors->first('location', ' has-error') }}">
            <label class="col-sm-5 control-label col-sm-pad">Location</label>
            <div class="col-sm-7 col-sm-pad">
                {{ Form::text('location', Input::old('location'), array('class' => 'form-control input')) }}
                {{ $errors->first('location', '<span class="help-block has-error">:message</span>') }}
            </div>
        </div>
        {{ Form::submit('Create', array('class'=>'btn btn-success btn-block')) }}
        {{ Form::token() . Form::close() }}

當我不使用這樣的模型時:

$validator = Validator::make(
    array('match_type' => Input::get('match_type')),
    array('opponent' => 'required_if:match_type,doubles')
);

這似乎有效。 我仍然不確定為什么我的方法不起作用,在模型中時match_type值不會傳遞給required_if嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM