0

I'm trying to add a new match into the database but if I purposely don't enter anything into the form, no errors are displayed. I have based this on my registration form which works perfectly. If I correctly fill out the form all data goes into the database correctly.

MatchController.php

public function postCreate()
{

    $validator = Validator::make(Input::all(), Match::$rules);

    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 Model (Match.php)

    class Match extends Eloquent{

        protected $table = 'matches';

        public static $rules = array(
            'match_type'=>'required',
            'associate'=>'alpha|min:2'
            'location'=>'required|alpha|min:2',
            'team_mate2'=>'required_if:match_type,doubles',
            'opponent2'=>'required_with:team_mate2'
        );

    }

Match Creation View (matchCreate.php)

<div class="row clearfix">
        <!-- Error Message Box -->
            @if(Session::has('message'))
            <div class="col-md-8 col-md-offset-2">
                <div class="alert alert-info">{{ Session::get('message') }}</div>
            </div>
            @endif
        <div class="col-md-9">
            <!-- Top Menu BEGIN -->
            <ul class="nav nav-pills nav-justified">
                <li>{{ HTML::link('members/logout', 'Profiles') }}</li>
                <li>{{ HTML::link('members/logout', 'Leagues') }}</li>
                <li class="active">{{ HTML::link('members/logout', 'Add Match') }}</li>
                <li>{{ HTML::link('members/logout', 'Results &amp; Fixtures') }}</li>
                <li>{{ HTML::link('members/logout', 'Matchboard') }}</li>
            </ul>
            <!-- Top Menu END -->
            <div class="page-header">
                <h3>Create a Match</h3>
            </div>
            <div class="col-sm-6">
                {{ Form::open(array('url'=>'members/matches/create', 'action' => 'post', 'class'=>'form-horizontal')) }}
                {{ form::hidden('id', Auth::user()->id) }}
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Match Type</label>
                    <div class="col-sm-7 col-sm-pad">
                        {{ $errors->first('match_type', '<span class="help-block">:message</span>') }}
                        {{ Form::select('match_type', array('singles' => 'Singles', 'doubles' => 'Doubles'), 'singles', array('id' => 'match_type', 'class' => 'form-control input')) }}
                    </div>
                </div>
                <div class="form-group">
                    <label for="gender" class="col-sm-5 control-label col-sm-pad">League Associate</label>
                    <div class="col-sm-7 col-sm-pad">
                        {{ $errors->first('associate', '<span class="help-block">:message</span>') }}
                        {{ Form::text('associate', Input::old('associate'), array('class' => 'form-control input')) }}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Team Mate #1<br><small></small></label>
                    <div class="col-sm-7 col-sm-pad">
                        <p class="form-control-static">{{ Auth::user()->first_name." ".Auth::user()->last_name }}</p>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Opponent #1</label>
                    <div class="col-sm-7 col-sm-pad">
                        {{ $errors->first('opponent', '<span class="help-block">:message</span>') }}
                        {{ Form::select('opponent', $users, 'key', array('class' => 'form-control input')) }}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Team Mate #2<br><small>(Doubles Only)</small></label>
                    <div class="col-sm-7 col-sm-pad">
                        {{ $errors->first('team_mate2', '<span class="help-block">:message</span>') }}
                        {{ Form::select('team_mate2', $users, 'key', array('class' => 'form-control input')); }}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Opponent #2<br><small>(Doubles Only)</small></label>
                    <div class="col-sm-7 col-sm-pad">
                        {{ $errors->first('opponent2', '<span class="help-block">:message</span>') }}
                        {{ Form::select('opponent2', $users, 'key', array('class' => 'form-control input')) }}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Date</label>
                        {{ $errors->first('day', '<span class="help-block">:message</span>') }}
                        <div class="col-lg-2 col-sm-pad">
                            {{ Form::selectRange('day', 1, 31, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
                        </div>
                        <div class="col-lg-2 col-sm-pad">
                            {{ Form::selectRange('month', 1, 12, 'a', array('class' => 'form-control input input-sm dob-form-control')) }}
                        </div>
                        <div class="col-lg-3 col-sm-pad">
                            {{ Form::selectRange('year', 2014, 2015, 'a', array('class' => 'form-control input-sm input dob-form-control')) }}
                        </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Time</label>
                    <div class="col-lg-3 col-sm-pad">
                        {{ $errors->first('time', '<span class="help-block">:message</span>') }}
                        {{ Form::select('time', $times, 'key', array('class' => 'form-control input input-sm dob-form-control')) }}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-5 control-label col-sm-pad">Location</label>
                    <div class="col-sm-7 col-sm-pad">
                        {{ Form::text('location', Input::old('location'), array('class' => 'form-control input')) }}
                    </div>
                </div>
                {{ Form::submit('Create', array('class'=>'btn btn-success btn-block')) }}
                {{ Form::token() . Form::close() }}
            </div>
            <div class="col-sm-6">
            </div>
        </div>
        @include('includes.sidemenu')
    </div>

routes.php

Route::post('members/matches/create', 'MatchController@postCreate');
3
  • Did you check the permissions of app/storage? Commented Jan 30, 2014 at 23:46
  • The rest of my application works fine, that includes registration, login and a profile page. Why would it be a permission problem? Commented Jan 30, 2014 at 23:48
  • True. I have to sleep. Commented Jan 30, 2014 at 23:52

1 Answer 1

2

You're creating your Validator object incorrectly. The make() method has four parameters:

/**
 * Create a new Validator instance.
 *
 * @param  array  $data
 * @param  array  $rules
 * @param  array  $messages
 * @return \Illuminate\Validation\Validator
 */
public function make(array $data, array $rules, array $messages = array(), array $customAttributes = array()) { ... }

You're passing each of your validation rules as a separate array, rather than together as one array. Subsequently, you aren't passing any of your input data, either, so it has no actual data to validate against.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm okay I see what I have done, I have tried several other ways before this with the same issue but I shall fix the problem you said and try again.
I have fixed that problem and used the exact same method I used for the registration validation, I stll can't see why the validation is not outputting errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.