Support the ongoing development of Laravel.io →
posted 10 years ago
Forms
Last updated 2 years ago.
0

Open your form using Form::model and your $user values will auto populate.

{{ Form::model($user, ['action'  => ['UserController@handleEditUser', $user->id], 'role'=> 'form']) }}

{{ Form::label('user_name', 'User Name:') }}
{{ Form::text('user_name', null, array('class'=>'form-control')) }}

{{ Form::close() }}

Passing null as default value will look for the property on the $user object.

If you're using Bootstrap for CSS then I can't recommend these form macros enough: https://gist.github.com/mnshankar/7253657

Last updated 2 years ago.
0

I've just re-read your question and I missed the real question.

This is what I do

if(!$form->valid())
{
  return \Redirect::back()
    ->withInput()
    ->with('Error', 'Invalid form etc');
}

The withInput() method puts the form data in the session. If you use my method suggested above, the form would repopulate automatically.

Last updated 2 years ago.
0

hi @meigwilym , i changed the code to


 {{ Form::model($user, ['action'  => ['UserController@handleEditUser', $user->id], 'role'=> 'form']) }}
        
        <input type="hidden" name="id" value="{{ $user->id }}">

        <div class="col-xs-6 col-sm-4">
        {{ Form::label('user_name', 'User Name:') }}
        {{ Form::text('user_name',null, array('class'=>'form-control')) }}
        </div>

 {{ Form::close() }}

The form still not repopulate with the $user values after i erase the value and click save.

Last updated 2 years ago.
0

@claratan it does make sense that the name field would be empty on the redirect as ->withInput() should keep the name field empty even though you in reality did not input anything into the field you cleared it, that is still a input. So it is doing what it is supposed to.

Last updated 2 years ago.
0

@avlastenok ,in my condition is now i am edit a records. For example, i EDIT a records(with all the initial values shown). now i erase all the exist input value , then i 'mistake' click save button, those fields cant be empty , so error shows , but now all the fields should not show back all the initial input values that i been erase just now ? It should shows right ? but in my code does not show ,how to make it show ? Thanks

Last updated 2 years ago.
0

Hello claratan, If I get it all correctly you want to repopulate the form with the user's info you are editing if validation fails. You can do that using,

{{ Form::model($user, array('route' => array('user.edit', $user->id))) } //make sure the form method, and route is set to patch if you want to update/edit  values

To bind the form with the user model and then if validation fails use something like this in your function:

if($validator->passes()){
		User::where('id', $id)->update(array(
			'username' => Input::get('user_name'),
			));
		return Redirect::to('/users')->with('message','User updated successfully!');
		}
		else{
			return Redirect::to('/users/edit')
			->withInput() //this will return the wrong input in the form fields
			->withErrors($validator)
			->with('message','Something went wrong');
		}
Last updated 2 years ago.
0

In old larave3 there is Input::flash (); and Input::old () methods but not sure if same thing applies in L4 as well.

Last updated 2 years ago.
0

Thanks again for all! I tried it out but i still not able to get the repopulate values.

q1: {{ Form::model($user, array('route' => array('user.edit', $user->id))) } ,the user.edit is the the method ??

q2: User::where('id', $id)->update(array( 'username' => Input::get('user_name'), ));

i think instead of using update , i using save()

Controller:

public function editUser(User $user)
    {
        return View::make('user.edituser', compact('user')); 
    }

  public function handleEditUser()
  {  
      $id = Input::get('id');
      $rules = array(
			'user_name'    => 'required|unique:user,user_name,'. $id, 			
		);

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

		if ($validator->fails()) {
			return Redirect::to('user/editUser/' . $id)
				->withInput()
        ->withErrors($validator);
		} else {
			
      $user = User::find(Input::get('id')); 
			$user->comp_name = Input::get('user_name');   
     $user->save();
      
    return Redirect::action('UserController@listUser');
    }
  }

view:


@extends('layouts.default')

@section('content')

    <div class="page-header">
        <h2>Edit User</h2>
    </div>
    
		<p style="color:red">  
      @if ($errors->has())
		
			@foreach ($errors->all() as $error)
				{{  $error }}<br>
       
			@endforeach
		
		@endif
		</p>
           
        {{ Form::model($user, ['action'  => ['UserController@handleEditUser', $user->id], 'role'=> 'form']) }}

        <input type="hidden" name="id" value="{{ $user->id }}">  

        <div class="col-xs-6 col-sm-4">
        {{ Form::label('user_name', 'User Name:') }}
			  {{ Form::text('user_name',null, array('class'=>'form-control')) }}
        </div>
        
       {{ Form::submit('Save',array('class'=>'btn btn-primary')) }} 
       <button class="btn" type="button" onclick="location.href='{{ action('UserController@listUser') }}'">Cancel</button>  
       
      {{ Form::close() }}
@stop

route:


Route::get('user/editUser/{user}',  array('uses' => 'UserController@editUser'));
Route::post('user/editUser',  array('uses' => 'UserController@handleEditUser'));

Last updated 2 years ago.
0

Hi guys , i still got no idea on this, someone can help me check the code ? thanks in advance

Last updated 2 years ago.
0

did you try Session ?

return Redirect::to('user/editUser/' . $id)
            ->with("user_name" , Input::get("user_name"))
    ->withErrors($validator);

and in your view you should be able to get

Session::get("user_name");
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

claratan claratan Joined 30 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.