Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Garet's avatar
Level 3

Defining form input fields from controller

One thing that bugged me a little with Laravel is how we specify form input fields directly in the view.

I come from a CodeIgniter background where generally you define your form input fields from the controller.

What I have done in Laravel is written a little helper function called formInput() which takes an array, defined in the controller, to generate form input fields in the view.

Here's an example:

class CustomerController extends Controller
{
    // Show the create a new customer form
    public function create()
    {
        // Define the fields for our view
         $fields = [
            'company' => [
                'type'        => 'text',
                'name'        => 'company',
                'placeholder' => 'Enter company name',
                'required'    => 'required',
                'maxlength'   => 35,
                'value'       => old('company'),
                'class'       => 'form-input',
            ],
        ];
        // Return the view
        return view('customer/edit', [
            'fields'   => $fields,
        ]);
    }
}

And then in my view I can do this:

<!-- Display the company form input -->
{!! formInput($fields['company']) !!}

I have omited my formInput() helper function for brevity, but essentially this looks at all the attributes passed to it and generates an input tag with those attributes.

Is there any issue with this? A good reason not to do it? It just feels like defining the form input, along with its max length, whether it's required or not, is really a job for the controller and not for the view.

What do you think?

0 likes
9 replies
STEREOH's avatar

I can't see how it would be bad practice. You do you :)

That being said, I would, personally, not do it as I really like to make my markup myself. Plus that would make implementing a frontend Framework more difficult.

Snapey's avatar

i would not. It really messes up your controller with stuff it should have no knowledge of.

Garet's avatar
Level 3

@SNAPEY - Isn't there an argument that the view shouldn't have knowledge of whether a form input is required or not, or what its maximum length is?

If the controller is responsible for form validation, doesn't it also stand that the controller should be responsible for specifying related validation attributes on the form elements?

STEREOH's avatar

You could counter-argument that the controller shouldn't have knowledge of the styling of your inputs ( classes, placeholders ... )

Garet's avatar
Level 3

@STEREOH - Yes good point.

What's the ideal soluton? I find that adding/removing the "required" attribute and changing the min/max length attribute on form inputs in the view is tedious when modifying validation rules in the controller. I admit with my method you still have to change them in the controller, but it feels more intuitive to change a validation rule in the controller and then change the related input attribute in the same controller, rather than having to hunt for it in a view.

STEREOH's avatar

I like to treat those not as validation, as it is client-side validation that may or may not be supported by every browser or worse bypassed. The only real validation is your Controller validation.

I prefer to treat it as QoL for the user, therefore, styling.

I guess it's all matter of personal preference but as Snapey said, I can't imagine what your controller would look like if you had 20+ inputs in one of your forms.

Snapey's avatar

My controller isn't responsible for form validation. Thats in a formRequest class.

Perhaps the formRequest Class should prepare the form requirements, however this assumes that the client is html client, wheras it might be an application.

Garet's avatar
Garet
OP
Best Answer
Level 3

Here's the solution I've come up with:

Controller

use App\Http\Requests\CustomerUpdateRequest;
use App\Http\Fields\CustomerEditFields;

class CustomerController extends Controller
{
    // Type hint the fields we want
    public function edit(Customer $customer, CustomerEditFields $fields)
    {
        return view('customer/edit', [
            'customer' => $customer,
            'fields'   => $fields->fields(),
        ]);
    }

    // Type hint form validation request
    public function update(Customer $customer, CustomerUpdateRequest $request)
    {

        // Update the customer
        $customer->update($request->validated());

        // Redirect back
        return redirect()->back()->with([
            'success' => 'Customer saved',
        ]);
    }
}

I then create a FormRequest class under \App\Http\Requests which is pretty standard.

However I've created my own CustomerEditFields class under \App\Http\Fields which defines and returns the fields that I want to the edit controller, which in turn passes them to the view.

My CustomerEditFields class looks like this:

namespace App\Http\Fields;

class CustomerEditFields
{
    public function fields() {
        return [
            'company' => [
                'type'        => 'text',
                'name'        => 'company',
                'placeholder' => 'Enter company name',
                'required'    => 'required',
                'maxlength'   => 35,
                'value'       => old('company'),
                'class'       => 'form-input' ],
        ];
    }
}

Any thoughts on whether this is an acceptable, or terrible, solution?

Please or to participate in this conversation.