gpt4 book ai didi

php - Laravel 验证 "required_without"

转载 作者:行者123 更新时间:2023-12-05 01:21:11 25 4
gpt4 key购买 nike

我似乎无法让“required_without”验证在 Laravel 4 上正常工作。我有一个包含三个字段的表单。我只希望用户提交播放器或铃声,不能同时提交。

当我在“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>

Controller

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');
}
}

最佳答案

这是您代码中的错误吗?我看到您的规则中有 'tournament_ringer' => 'required_without:tournament_ringer' - 您的意思可能是 required_without:tournement_player

关于php - Laravel 验证 "required_without",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21998063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com