Question

I can't seem to get the "required_without" validation working correctly on Laravel 4. I have a form with three fields. 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.

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

No correct solution

OTHER TIPS

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.

One easy workaround is to use an XOR statement instead of the validator:

if (Input::get('tournament_player') XOR Input::get('tournament_ringer')) {
    //Validator passes.
} else {
    //Create error message and redirect with it.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top