0

I'm trying to build a back end admin area to add users to my application (I don't want users to register I want them to be set up by an admin).

I've created the necessary controller and request.

If the data is acceptable it submits via a POST request to domain.tld/admin/users and is stored via UserController@store.

When the validation criteria isn't met, the user is sent back to the form (domain.tld/admin/users/create) and error messages are shown, but the fields aren't populated with the previously filled out data.

I'm using Route::resource('admin/users') for RESTful route creation and a clean routes file, I've found that when the HTTP POST request is made to the parent route (domain.tld/admin/users) as generated by Route::resource(), the FormRequest doesn't populate the old data.

But when the HTTP POST request is made to the create route where the form is situated (domain.tld/admin/users/create), the validation fail populates the old data correctly.

Is there any way to make this work as it should whilst still using Route::resource('admin/users') without having to use the {{ old('') }} helper in my views?

UsersController (very basic)

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use App\Http\Requests\Admin\UserCreateRequest;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $users = User::all();
        return view('admin.users.index', compact('users'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        $users = User::all();
        return view('admin.users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(UserCreateRequest $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

UserCreateRequest

<?php

namespace App\Http\Requests\Admin;

use App\Http\Requests\Request;

class UserCreateRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'email' => 'required|confirmed|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ];
    }
}

create.blade.php

@extends('admin._layouts.master')

@section('title', 'Create a user')

@section('content')

    <form method="post" action="{{ action('Admin\UsersController@store') }}">

        @foreach ($errors->all() as $error)
            <p class="alert alert-danger">{{ $error }}</p>
        @endforeach

        @if (session('status'))
            <div class="alert alert-success">
                {{ session('status') }}
            </div>
        @endif

        {!! csrf_field() !!}

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

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

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

        <div class="form-group">
            <label for="email_confirmation">Confirm email address</label>
            <input class="form-control" id="email_confirmation" placeholder="Email" type="email" name="email_confirmation">
        </div>

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

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

        <button class="btn btn-default" type="submit">Submit</button>
    </form>

@endsection

Any help would be greatly appreciated.

  • What are you using to create the form? Could we see that code, please? – Stuart Wagner Aug 19 '15 at 23:21
  • What is code in the view? Post one of input please. – Carter Aug 19 '15 at 23:27
  • Please call old() in your input's value attribute, you have to do this. – Carter Aug 19 '15 at 23:34
  • It's not required anymore, from the docs.... "Laravel allows you to keep input from one request during the next request. This feature is particularly useful for re-populating forms after detecting validation errors. However, if you are using Laravel's included validation services, it is unlikely you will need to manually use these methods, as some of Laravel's built-in validation facilities will call them automatically." I have seen this work, but not when using RESTful routes, only when submitting POST request to itself and not the parent route. –  Aug 19 '15 at 23:49
  • I am using form request for nearly every form submission, and honestly I never get the old input without calling old() manually. Plus I just tested a moment ago. So I am really doubtful. Anyway let me dig into the laravel's source and have a look. – Carter Aug 19 '15 at 23:56
  • 1
    Look at line 135 in FormRequest, what it does is exactly what we have to do manually in Laravel 4, redirecting with old input and errors. And what's next we need to do in Laravel 4? Flash out old input by ourselves. So It seems to me that the 5.1 docs is not clear: what can be done automatically by using Form Request are actually all about validation and storing old input into session. Nothing more. – Carter Aug 20 '15 at 00:21
  • any new thoughts on this? and how can it be done? – aahhaa Feb 29 '16 at 05:35

1 Answers1

-1

you want to use the class Input to get old post data in Laravel.

<input type="text" class="form-control" value="{{Input::old('name')}}" name="name" placeholder="Enter name" required="required" />
mdamia
  • 4,447
  • 1
  • 24
  • 23
  • OP mentioned they already know about the `old()` helper, and this is really just the same thing. – Stuart Wagner Aug 19 '15 at 23:17
  • I want the validation request to populate the old data automatically as it should (and does when the form submits to itself rather than its parent). I don't want to add helpers to my views if I shouldn't have to. –  Aug 19 '15 at 23:24
  • This is only correct when not using Laravels built in methods. –  Aug 19 '15 at 23:50