php - Laravel required_if not working

981

My required_if validation does not work. No error is output when "doubles" is selected but "team_mate2 and opponent2" are not selected. I need the form to require a second team mate and opponent when doubles is selected as the match type.

Match.php (Model)

<?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();
    }
}

Match Create View

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

Answer

Solution:

When I don't use a model like so:

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

This seems to work. I still am unsure on why my method does not work, does the match_type value not pass over to required_if when in a Model?

People are also looking for solutions to the problem: PHP Making Simplest query with MySQL using OOP MySQLi

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.