Mike_91's avatar

Form input values with Laravel

I want to build a form that is going to be used to enter data for new records as well as for editing existing records.

How do i define the form input's value attribute knowing that it can be either blank or the existing record's value

I want to use plain html and not the laravel form.

Here is my code for the edit

<form method="POST" action="/users/{{ $user->id }}" enctype="multipart/form-data"> 
                
                    {{ csrf_field() }}
                    {{ method_field('PATCH') }}

                        <div class="form-group">
                          <label for="name">Name:</label>
                          <input type="text" class="form-control" id="name" placeholder="Enter name" name="name" >
                        </div>

                        <div class="form-group">
                          <label for="username">Username:</label>
                          <input type="text" class="form-control" id="username" placeholder="Enter username" name="username" >
                        </div>

                        <div class="form-group">
                          <label for="email">Email:</label>
                          <input type="text" class="form-control" id="email" placeholder="Enter email" name="email" >
                        </div>

                        <div class="form-group">
                          <label for="password">Password:</label>
                          <input type="password" class="form-control" id="password" placeholder="Enter password" name="password" >
                        </div>

                        <div class="form-group">
                          <label for="password_confirmation">Password Confirmation:</label>
                          <input type="password" class="form-control" id="password_confirmation" placeholder="Enter password_confirmation" name="password_confirmation" >
                        </div>

                     

                        <button type="submit" class="btn btn-success">Update</button>

                    </form>

0 likes
4 replies
tykus's avatar

If you are reusing the same form, then you could send an empty User object to the create view from your controller:

$user = new User;
return view('users.create', compact('user'));

You can then use the value attribute on the input to set the property value (null in the case of create).

<input type="text" class="form-control" id="name" value="{{$user->name}}" placeholder="Enter name" name="name" >

If you are validating and redirecting back with input, then you would need to also be able to handle the old input:

<input type="text" class="form-control" id="name" value="{{old('name') ??
 $user->name}}" placeholder="Enter name" name="name" >
Mike_91's avatar

thanks it works but i have a problem with textareas, checkboxes, select dont recognise the value given.

<div class="form-group">
                          <label for="description">Description:</label>
                          <textarea  value="{{$category->description}}" class="form-control" id="description" placeholder="Enter description" name="description" ></textarea>
                        </div>
tykus's avatar
tykus
Best Answer
Level 104

The value goes between the tags for a textarea input:

<textarea class="form-control" id="description" placeholder="Enter description" name="description" >
    {{$category->description}}
</textarea>

For the checkboxes, you need to set the checked attribute:

<input type="checkbox" name="active" value="1" {{$user->active ? "checked" : ""}}

Please or to participate in this conversation.