Laravel Back to page with old input for validation

不想你离开。 提交于 2019-12-05 23:11:04

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!