简体   繁体   English

Laravel验证“ required_without”

[英]Laravel Validation “required_without”

I can't seem to get the "required_without" validation working correctly on Laravel 4. I have a form with three fields. 我似乎无法在Laravel 4上正常运行“ required_without”验证。我有一个包含三个字段的表单。 I only want users to submit a player OR a ringer, not both at the same time. 我只希望用户同时提交一个播放器或一个铃声。

When I enter something into the "tournament_player" and "tournament_ringer" textboxes, the validation succeeds. 当我在“ tournament_player”和“ tournament_ringer”文本框中输入内容时,验证成功。

View 视图

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

One easy workaround is to use an XOR statement instead of the validator: 一种简单的解决方法是使用XOR语句而不是验证器:

if (Input::get('tournament_player') XOR Input::get('tournament_ringer')) {
    //Validator passes.
} else {
    //Create error message and redirect with it.
}

Is it a bug in your code? 这是您代码中的错误吗? I see you've got 'tournament_ringer' => 'required_without:tournament_ringer' in your rules - you probably mean, required_without:tournement_player . 我看到您的规则中有'tournament_ringer' => 'required_without:tournament_ringer'您可能是说, required_without:tournement_player

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM