uzeeOnCodes uzeeOnCodes - 1 month ago 8
PHP Question

Laravel record not submitting

I have a form in my Laravel 4 web app that is refusing to submit to the database. Each time i try to submit, the page just reloads and i see no error messages even in the laravel log. I have spent two days trying to figure out what the problem is as I can't seem to see anything wrong with the code.

Any help will be appreciated.

/**** THE FORM VIEW ***/

<div class="container">

<div class="row">
@if(Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('success')}}
</div>
@elseif(Session::has('fail'))
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('fail')}}
</div>
@endif
</div>

@include('partials.admin-navbar')
<div class="admin_profile_content">
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
<div class="" style="width:80%; margin:auto;">

<div class="panel panel-default">
<div class="panel-heading">Organization </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Select the Organization hosting this competition </label>
<div class="col-sm-10">
{{ Form::select('organization', $organizations, null, ['class' => 'form-control']) }}
<p class="text-danger">
@if($errors->has('organization'))
{{ $errors->first('organization') }}
@endif
</p>
</div>
</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">Competition name </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Competition name </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="competition_name">
<p class="text-danger">
@if($errors->has('competition_name'))
{{ $errors->first('competition_name') }}
@endif
</p>
</div>
</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">Prizes </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Total prize pool</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="total_prize">
</div>
</div>

<div class="form-group" style="padding-top:41px;">
<label for="" class="col-sm-2 control-label">Number of prize-winning places</label>
<div class="col-sm-1">
Top
</div>
<div class="col-sm-2">
<input type="text" class="form-control" id="" name="number_of_winning_places">
<p class="text-danger">
@if($errors->has('number_of_winning_places'))
{{ $errors->first('number_of_winning_places') }}
@endif
</p>
</div>
<div class="col-sm-3">
competitors will win a prize
</div>
</div>

</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">Timeline </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Start and end date </label>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_start_date" name="competition_start_date">
<p class="text-danger">
@if($errors->has('competition_start_date'))
{{ $errors->first('competition_start_date') }}
@endif
</p>
<input type="hidden" name="hidden_start_date" id="hidden_start_date">
</div>
<div class="col-sm-2">
to
</div>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_end_date" name="competition_end_date">
<p class="text-danger">
@if($errors->has('competition_end_date'))
{{ $errors->first('competition_end_date') }}
@endif
</p>
<input type="hidden" name="hidden_end_date" id="hidden_end_date">
</div>
</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">Competition details </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Competition details </label>

<div class="col-sm-10">
<textarea class="form-control" name="competition_details" id="competition_details" rows="10"> </textarea>
<p class="text-danger">
@if($errors->has('competition_details'))
{{ $errors->first('competition_details') }}
@endif
</p>
</div>
</div>
</div>
</div>


<div class="panel panel-default">
<div class="panel-heading">Competition Status </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Set competition status </label>

<div class="col-sm-10">
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
</div>
</div>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">Competition data </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Upload the data for the competition </label>

<div class="col-sm-10">
<input type="file" id="competition_data_1" name="competition_data_1">
<p class="help-block"> Data file/folder 1</p>
</div>


</div>
</div>
</div>
{{ Form::token() }}
<div class="panel panel-default">
<div class="panel-heading">Upload Competition </div>
<div class="panel-body">
<div class="form-group">
<div class="col-sm-10">
<input type="submit" class="btn btn-primary btn-lg btn-block" id="submit_competition" value="Submit">
</div>
</div>
</div>
</div>

</div>

</form>




<hr>




/* the controller method */

public function postSubmitCompetition()
{
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));

if($validator->fails())
{
return Redirect::route('getSubmitCompetition')->withErrors($validator)->withInput();
}
else
{
$competition = new Competition();
$competition->hosting_organization_id = Input::get('organization');
$competition->competition_name = Input::get('competition_name');
$competition->total_prize_pool = Input::get('total_prize');
$competition->prize_winning_places = Input::get('number_of_winning_places');
$competition->start_date = Input::get('competition_start_date');
$competition->end_date = Input::get('competition_end_date');
$competition->competition_details = Input::get('competition_details');
$competition->status = Input::get('competition_status');


if($competition->save())
{

return Redirect::route('getSubmitCompetition')->with('success', 'You have successfully created this competition');

}
else
{
return Redirect::route('getSubmitCompetition')->with('fail', 'An error occurred while creating that competition. Please contact sys admin');
}
}
}


/* the route */

Route::post('/admin/submit-a-competition', array('uses' => 'CompetitionController@postSubmitCompetition', 'as' => 'postSubmitCompetition'));

Answer

Problem

Laravel never saves your data because the validator fails.

$validator = Validator::make(Input::all(), array(
       'organization' => 'required',
       'competition_name' => 'required',
       'prize_winning_places' => 'required',
       'competition_start_date' => 'required',
       'competition_end_date' => 'required',
       'competition_details' => 'required',
       'status' => 'required'
   ));

You require prize_winning_places and status but there are no input fields for these two in the view.

The missing input fields:

Field name: status - Problem: You made a select with the name: competition_status instead of: status

<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>

Field name: prize_winning_places - Problem: You made an input field with the name: ** number_of_winning_places **instead of prize_winning_places

<input type="text" class="form-control" id="" name="number_of_winning_places">

Solution

Change the $validator variable to:

$validator = Validator::make(Input::all(), array(
       'organization' => 'required',
       'competition_name' => 'required',
       'number_of_winning_places' => 'required',
       'competition_start_date' => 'required',
       'competition_end_date' => 'required',
       'competition_details' => 'required',
       'competition_status' => 'required'
   ));
Comments