I have a Laravel form that gets passed through some validation. Obviously, if the validation fails, I'd like the user to be redirected to form and alerted of their mistake. Also, because the form is fairly large, I don't want the user to have to re-input any data. Here is the form (I've cut off a large part to reduce the size of this message)
:@layout('templates.main')
@section('content')
@if(Session::has('validation_errors'))
<ul class="form_errors">
@foreach($errors as $error)
{{$error}}
@endforeach
</ul>
@endif
{{ Form::open('account/create', 'POST') }}
<table>
<tr>
<td>{{ Form::label('dealer', 'Dealer') }}</td>
<td>{{ Form::select('dealer', array('1' => 'Dealer #1', '2' => 'Dealer #2')) }}</td>
<td>{{ Form::label('amount', 'Amount') }}</td>
<td>{{ Form::text('amount', Input::old('amount')) }}</td><!--Here is where I'm testing for old input-->
</tr>
<tr>
<td>{{ Form::label('date', 'Date') }}</td>
<td>{{ Form::date('date', NULL, array('class' => 'date_picker')) }}</td>
<td>{{ Form::label('installation', 'Installation #') }}</td>
<td>{{ Form::input('text', 'installation') }}</td>
</tr>
<tr>
<td colspan="4">{{ Form::textarea('notebox', NULL, array('id' => 'notebox')) }}</td>
</tr>
<tr>
<td colspan="4">{{ Form::submit('Submit') }} {{ Form::reset('Reset') }}</td>
</tr>
</table>
{{ Form::close() }}
@endsection
When the form is submitted, here is the controller that handles it:
public function post_create() {
//Validate it in the model
$validation = Account::validate(Input::all());
if($validation->fails()){
return Redirect::to('account/create')
->with_input()
->with('validation_errors', true)
->with('errors', $validation->errors->all('<li>:message</li>'));
}
else {
return "passed";//for debugging purposes
}
}
And lastly, here is the Account model:
class Account extends Eloquent {
public static function validate($input) {
//Validation rules
$rules = array(
'firstname' => 'required',
'lastname' => 'required',
//etc...
);
//Custom validation messages
$messages = array(
'firstname_required' => 'A first name is required.',
'lastname_required' => 'A last name is required.',
//etc...
);
//Pass it through the validator and spit it out
return Validator::make($input, $rules, $messages);
}
}
So when I go to test this by intentionally making the form invalid, I am redirected to the account/create form, however, no validation errors show, and the old input is not saved (I only attached Input::old()
to the amount textbox).
object(Laravel\Messages)#31 (2) { ["messages"]=> array(0) { } ["format"]=> string(8) ":message" }
– Andrew De Forest Jan 28 at 19:25var_dump
ofInput::old()
in my controller, I see the values that should be saved. However, when I do the same exactvar_dump
in my view, it returns empty. So something is being lost between my controller and my view and I'm not sure why. – Andrew De Forest Jan 28 at 20:30