问题
For the Update Profile Page
I use the route as
Route::get('editdriver/{data}', 'DriverController@EditDriver');
And in the controller after validation i use,
return Redirect::to('editdriver/'.$data)->withInput()->withErrors($validation->messages());
So, the url will be
http://localhost/project/editdriver/1
If i empty the value which is required in the rule
and press submit the form,
It shows the old data with the validation message.
What i need is, It should not show the old value, which is from the db.
I even tried.,
return Redirect::back()->withErrors($validation->messages());
How can i do this ??
Update :
In the controller i have
public function EditDriver($data=NULL)
{
$editvehicle=$data;
$DriverDetails = DriverModel::where('DriverId', $editvehicle)->get()->toArray();
return View::make('home/editdriver')->with('DriverDetails', $DriverDetails);
}
In the view i have
$("#Firstname").val("<?php echo $DriverDetails[0]['Firstname']?>");
And displaying from jquery to html as
{{ Form::text('Firstname',null, array('id'=> 'Firstname')) }}
回答1:
I have no idea why you are using jQuery to fill in your form - that makes no sense and you should remove that code.
All you need is this one line of code:
{{ Form::text('Firstname', Input::old('Firstname', $DriverDetails[0]['Firstname']), array('id'=> 'Firstname')) }}
Using Input::old($DriverDetails[0]['Firstname'])
will prefill the inital form with your database value - but will then fill in the form with any 'inputted' values if there is a validation error
And to confirm - your original return statement looks correct:
return Redirect::to('editdriver/'.$data)->withInput()->withErrors($validation->messages());
回答2:
I think you wanna use the Form::model method of Laravel. This allows you to populate the form with data from your database in an easy way and give you also the possibility to input old form data.
{{ Form::model($User, array('url' => 'management/edit/' . $User->id, 'role' => 'form')) }}
<div class="form-group">
<label>Username</label>
{{ Form::text('username', Input::old('username'), array('class' => 'form-control', 'placeholder' => 'Username', 'name' => 'username')) }}
</div>
<div class="form-group">
<label>Password</label>
{{ Form::password('password', array('class' => 'form-control', 'placeholder' => 'Password', 'name' => 'password')) }}
</div>
{{ Form::submit('Edit', array('class' => 'btn btn-default')) }}
{{ Form::close() }}
In your public function you can do this:
public function showEdit($ID)
{
//Return View With User Information
return View::make('management.edit')
->with('User', Management::find($ID));
}
This will pass back the information of the user you wanna edit. And the Form::model will match this automatically with the names of your Form::text. I think that your database fields should match the names of the Form::text.
And then when you post your edit to the server you can do this:
public function postEdit($ID)
{
// Get Input
$PostData = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
// Declare Validation Rules.
$ValRules = array(
'username' => 'required|unique:management,username,' . $ID,
'password' => 'required'
);
// Declare Validation Messages
$ValMessages = array(
'username.required' => 'The field Username is required.</br>',
'username.unique' => 'The field Username must be unique.</br>',
'password.required' => 'The field Password is required.</br>'
);
// Validate Input
$ValResult = Validator::make($PostData, $ValRules, $ValMessages);
// Check Validate
if ($ValResult->passes())
{
// Update User
$UserUpdate = Management::find($ID);
$UserUpdate->username = Input::get('username');
$UserUpdate->password = Input::get('password');
$UserUpdate->save();
// Succes, Redirect To User
return Redirect::to('management')
->with('Success', 'The management users is modified.');
}
else
{
// Grab Messages From Validator
$ValErrors = $ValResult->messages();
// Error, Redirect To User Edit
return Redirect::to('management/edit/' . $ID)
->withErrors($ValErrors)
->withInput(Input::except('password'));
}
}
See the ->withInput to return the information the user enterd. The Form::model will then look in the Session Flash Data and put the old input on the right places. Notice that Session Flash Data will take precendence over the models value ($User).
Laravel Form Model Binding: http://laravel.com/docs/4.2/html#form-model-binding
Hope this helps!
来源:https://stackoverflow.com/questions/28296562/laravel-back-to-page-with-old-input-for-validation