我似乎無法在Laravel 4上正確運行「required_without」驗證。我有三個字段的表單。我只希望用戶提交玩家或鈴聲,而不是同時提交。Laravel驗證「required_without」
當我在「tournament_player」和「tournament_ringer」文本框中輸入內容時,驗證成功。
查看
<h3>Add Player</h3>
{{ Form::open(array('url'=>'members/tournament/addplayer', 'action' => 'post', 'class'=>'form-horizontal')) }}
{{ form::hidden('user_id', Auth::user()->id) }}
{{ form::hidden('tournament_id', $tournament->id) }}
<div class="form-group{{ $errors->first('side2_p1', ' has-error') }}">
<label class="col-sm-2 control-label col-sm-pad">Player</label>
<div class="col-sm-5 col-sm-pad">
{{ Form::select('tournament_player', array('' => 'Choose...') + $users, 'key', array('class' => 'form-control input')) }}
{{ $errors->first('tournament_player', '<span class="help-block">:message</span>') }}
</div>
<div class="col-sm-4 col-sm-pad">
{{ Form::text('tournament_ringer', '', array('class' => 'form-control input', 'placeholder' => 'Ringer')) }}
{{ $errors->first('tournament_ringer', '<span class="help-block">:message</span>') }}
</div>
</div>
<div class="form-group">
<label for="gender" class="col-sm-2 control-label col-sm-pad">Bracket</label>
<div class="col-sm-3 col-sm-pad">
{{ Form::selectRange('player_bracket', 1, ($tournament->size/2), '1', array('class' => 'form-control input')) }}
{{ $errors->first('player_bracket', '<span class="help-block">:message</span>') }}
</div>
</div>
{{ Form::submit('Add', array('class'=>'btn btn-success')) }}
{{ Form::token() . Form::close() }}
</div>
控制器
public function postAddPlayer()
{
$validator = Validator::make(
array(
'tournament_player' => Input::get('tournament_player'),
'tournament_ringer' => Input::get('tournament_ringer')),
array(
'tournament_player' => 'required_without:tournament_ringer',
'tournament_ringer' => 'required_without:tournament_ringer'),
array(
'required_without' => 'You can only add a player OR a ringer.')
);
if ($validator->passes())
{
if(Input::has('tournament_player'))
{
$tournament = Tournamentplayers::addPlayer(Input::get('tournament_player'));
}elseif(Input::has('tournament_ringer')){
$tournament = Tournamentplayers::addRinger(Input::get('tournament_ringer'));
}
}else{
return Redirect::to('members/tournament/'.Input::get('tournament_id').'/edit')
->withErrors($validator)
->withInput()
->with('message', 'Error! Something was wrong.')
->with('status', 'danger');
}
}
你有答案..? –
試試這個[相同類型的問題和答案](http://stackoverflow.com/questions/30635678/laravel-validation-required-only-and-only-one-field) –