PHPackages                             codezero/laravel-form-field-prefixer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. codezero/laravel-form-field-prefixer

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

codezero/laravel-form-field-prefixer
====================================

Reuse form partials and automatically add optional prefixes and array keys to your form fields.

1.5.0(3y ago)1217MITPHPPHP ^7.3|^8.0

Since Apr 7Pushed 3y ago3 watchersCompare

[ Source](https://github.com/codezero-be/laravel-form-field-prefixer)[ Packagist](https://packagist.org/packages/codezero/laravel-form-field-prefixer)[ Fund](https://paypal.me/ivanvermeyen)[ Fund](https://ko-fi.com/ivanvermeyen)[ RSS](/packages/codezero-laravel-form-field-prefixer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (7)Used By (0)

Laravel Form Field Prefixer
===========================

[](#laravel-form-field-prefixer)

[![GitHub release](https://camo.githubusercontent.com/a05effabccb436e3d7acee72f8ec0d7d68382bfd5b010eaadec4df02ae95d6ff/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f636f64657a65726f2d62652f6c61726176656c2d666f726d2d6669656c642d70726566697865722e7376673f7374796c653d666c61742d737175617265)](CHANGELOG.md)[![Laravel](https://camo.githubusercontent.com/f1c0bc3ecc3de5400058ae5c9dc25d93ae4542c26d742ce22bb9a5ff76f9087f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c61726176656c2d31302d7265643f7374796c653d666c61742d737175617265266c6f676f3d6c61726176656c266c6f676f436f6c6f723d7768697465)](https://laravel.com)[![License](https://camo.githubusercontent.com/00da5462a08ae19c14221603ad689e283cee9287f588be5ca84d71b5073974d0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636f64657a65726f2f6c61726176656c2d666f726d2d6669656c642d70726566697865722e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Build Status](https://camo.githubusercontent.com/f0f6c1560c4817f3b27463ab791f7fa4a94d13a1a6be021e85a3e49e3066abf1/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f6275696c642f672f636f64657a65726f2d62652f6c61726176656c2d666f726d2d6669656c642d70726566697865722f6d61737465723f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/codezero-be/laravel-form-field-prefixer/build-status/master)[![Code Coverage](https://camo.githubusercontent.com/3bad59a219dea0785990a7e40d476f9063ac92df28fec050954077d7ab287a18/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f636f64657a65726f2d62652f6c61726176656c2d666f726d2d6669656c642d70726566697865722f6d61737465723f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/codezero-be/laravel-form-field-prefixer/?branch=master)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fa8c013a606f076d05b18d0b6e845981a05de614f58395c99e02c36e666f752b/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f636f64657a65726f2d62652f6c61726176656c2d666f726d2d6669656c642d70726566697865722f6d61737465723f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/codezero-be/laravel-form-field-prefixer/?branch=master)[![Total Downloads](https://camo.githubusercontent.com/24fb359297222ec947ab680503ad0c03d9e956708aa87045b2c126d3fa5aa162/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f64657a65726f2f6c61726176656c2d666f726d2d6669656c642d70726566697865722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/codezero/laravel-form-field-prefixer)

[![ko-fi](https://camo.githubusercontent.com/1fedf764fa06114b797ee53e7506df10880abed6766f854202d758df1707969d/68747470733a2f2f7777772e6b6f2d66692e636f6d2f696d672f676974687562627574746f6e5f736d2e737667)](https://ko-fi.com/R6R3UQ8V)

#### Reuse form partials with optional prefixes and array keys.

[](#reuse-form-partials-with-optional-prefixes-and-array-keys)

When you have large forms and you want to split them up into reusable partials, you might end up doing some nasty logic inside the partials. To streamline this, I tucked away the logic behind the `FormFieldPrefixer` class.

✅ Requirements
--------------

[](#-requirements)

- PHP &gt;= 7.1
- Laravel &gt;= 5.6

📦 Install
---------

[](#-install)

```
composer require codezero/laravel-form-field-prefixer
```

> Laravel will automatically register the `FormFieldPrefixer` facade.

🛠 Usage
-------

[](#-usage)

### Basic Usage - Simple Forms

[](#basic-usage---simple-forms)

Pass an instance of the `FormFieldPrefixer` to your form partial.

Optionally you may provide an optional prefix to the `make` method:

```
@include('forms.address', ['prefixer' => FormFieldPrefixer::make('client')])
```

If you don't provide a prefix, `client_` will be stripped of the example output below.

Inside the partial you can now use `$prefixer` to generate field names dynamically:

#### Labels

[](#labels)

Generate a `for` attribute:

```
for('address') }}>Address:
```

This will compile to:

```
Address:
```

#### Input Fields

[](#input-fields)

Generate a `name`, `id` and `value` attribute:

You may pass an optional default value that will be filled in when the form loads.

If you submit the form and get redirected back with the input (like when you have validation errors), then the new values will override the default ones and will be filled in behind the scenes, using `Session::getOldInput($field)`.

```
name('address') }}
    {{ $prefixer->id('address') }}
    {{ $prefixer->value('address', 'default value') }}
>
```

This will compile to:

```

```

#### Select Fields

[](#select-fields)

Generate a `name` and `id` attribute for the select and a `selected` attribute for the selected option:

The `selected` method requires the option's value and accepts an optional default value to determine if the option is the selected one. In the example below `Mr.` is set as the default value.

Like with the input field, old input has precedence over the default value.

```
name('title') }} {{ $prefixer->id('title') }}>
    @foreach(['Mr.', 'Mrs.'] as $option)
        selected('title', $option, 'Mr.') }}>
            {{ $option }}

    @endforeach

```

This will compile to:

```

    Mr.
    Mrs.

```

#### Validation Errors

[](#validation-errors)

Generate a validation key you can pass to `@error`:

```
@error($prefixer->validationKey('address'))
    {{ $message }}
@enderror
```

This will compile to:

```
@error('client_address')
    {{ $message }}
@enderror
```

### Flat Array Forms

[](#flat-array-forms)

If you require, for example, multiple addresses, you can specify an array key for each partial:

```
@foreach(['billing', 'shipping'] as $type)
    @include('forms.address', ['prefixer' => FormFieldPrefixer::make('client')->asArray($type)])
@endforeach
```

The partial will contain the same templates as the previous examples, but this time, notice the difference between the `name`, `id` and `error` key:

```

@error('client_address.billing')
    {{ $message }}
@enderror
```

Alternatively, you can also use the field name as the array key, by not specifying a key in advance:

```
@include('forms.address', ['prefixer' => FormFieldPrefixer::make('client')->asArray()])
```

Now the `address` field will look like this:

```

@error('client.address')
    {{ $message }}
@enderror
```

Again, if you did not specify a prefix, `client_` would be stripped off.

### Multi Dimensional Array Forms

[](#multi-dimensional-array-forms)

You can also use a multi dimensional array notation. For this, you always need to specify a prefix, as that will be the basename of the input. If you don't, the result will be a flat array like in the previous example.

```
@foreach(['billing', 'shipping'] as $type)
    @include('forms.address', ['prefixer' => FormFieldPrefixer::make('client')->asMultiDimensionalArray($type)])
@endforeach
```

The partial will contain the same templates as the previous examples, but this time, notice that the input's name is now an array key.

```

@error('client.billing.address')
    {{ $message }}
@enderror
```

### Hybrid Forms with a Touch of VueJS

[](#hybrid-forms-with-a-touch-of-vuejs)

***Disclaimer:** As a backend kind of guy, I didn't want to go all-in on JS, but I wanted to add just enough JS to make things work. I'm using VueJS, so this solution will only work with VueJS at this time. Making PHP and VueJS work together nicely with these dynamic forms was much more tricky than I thought (attributes needed much manipulation). But because I didn't want to duplicate all of my forms, I kept banging my head against the keyboard until this package fell out. So far I'm pretty happy with the working result.*

This code is supported, but not included in this package because it is project specific.

Feel free to copy the example and adjust it as you need.

**Again, this is a very minimal example, just enough to make things work!**

#### Example Use Case

[](#example-use-case)

Let's say we are adding and removing form fields dynamically when someone clicks a button. For each fictional "client" that we add, we require the included form fields to be filled in.

We need data binding to keep the form fields in sync when we add and remove them. When we get validation errors upon submission, we also need to restore any previously dynamically added fields with their filled in "old input".

#### Extra Setup

[](#extra-setup)

To get this to work, we need some sort of JS "form manager" that indexes the added clients and keeps a record of all of their input data. Let's create a Vue component that will do this:

```
/* components/FormManager.vue */

export default {
    props: {
        values: { required: true },
        errors: { required: true },
    },

    data() {
        return {
            formValues: [],
            formErrors: [],
        }
    },

    mounted() {
        this.formValues = this.values
        this.formErrors = this.errors
    },

    methods: {
        add() {
            this.formValues.push({})
        },

        remove(index) {
            this.formValues.splice(index, 1)
            this.formErrors = []
        },

        getError(key) {
            return (this.formErrors[key] || [])[0]
        }
    },

    render() {
        return this.$scopedSlots.default({
            formValues: this.formValues,
            addFormEntry: this.add,
            removeFormEntry: this.remove,
            getError: this.getError,
        })
    },
}

```

Don't forget to register the component in your `app.js` file:

```
/* app.js */
window.Vue = require('vue');

Vue.component('form-manager', require('./components/FormManager').default);

const app = new Vue({
    el: '#app',
});
```

#### Using the Form Manager

[](#using-the-form-manager)

Let's wrap our dynamic form parts with this new component. We also need to feed it any "old input" and validation errors, in case we get redirected back after the form submission fails.

```

	    Client #@{{ index + 1 }}

	    @include('forms.address', ['prefixer' => FormFieldPrefixer::make('clients')->asMultiDimensionalArray('${ index || 0 }')])

	        Remove Client

	    Add Client

```

**What we are doing here is:**

- we are feeding the `form-manager` any old `clients` input values in JSON format
- we are also feeding the `form-manager` the `errors` array in JSON format
- we use a scoped slot to access and bind the feeded `formValues` using the `clients` alias
- we use a scoped slot to access the `addFormEntry`, `removeFormEntry` and `getError` methods
- we are using a prefix of `clients` with the `FormFieldPrefixer`
- we are using a JS string as multi dimensional array key which will eventually print the array index
- `clients` will be the basename of the multi dimensional array
- this package will use the `clients` prefix as variable name to generate input bindings
- **the `formValues` alias should be the same as the `FormFieldPrefixer` prefix**
- we add a button to add and remove a set of client form fields
- the `client` variable from the for loop is not used in this example

#### Template Enhancements

[](#template-enhancements)

To use the form field templates with both PHP and JS, you need to add one thing to your select fields:

```
name('title') }}
    {{ $prefixer->id('title') }}
    {{ $prefixer->select('title') /* Add this line for JS compatibility */ }}
>
    @foreach(['Mr.', 'Mrs.'] as $option)
        selected('title', $option, 'Mr.') }}>
            {{ $option }}

    @endforeach

```

This will compile to:

```

    Mr.
    Mrs.

```

Notice that instead of a `selected` attribute on an option, there is now a `v-model` attribute on the select.

When you use the same template in a partial that does not have any JS key passed to it, the `v-model` will not be rendered and the `selected` attribute will. So you can use this "enhanced" template for all use cases.

**The other form field templates do not need to be changed.**

Text inputs will automatically get a `v-model` attribute instead of a `value` attribute.

#### Default Values are Ignored

[](#default-values-are-ignored)

If you want to load default values in your JS-driven form fields when the page loads, you will need to feed those values to the `form-manager`, in the same way you feed it the "old input".

Any default values passed to the `FormFieldPrefixer` methods are ignored when using JS keys.

#### Validation Errors

[](#validation-errors-1)

I ended up extracting a form error partial:

```
@php $prefixer = $prefixer ?? FormFieldPrefixer::make(); @endphp

{!!-- For JS enabled fields that render client side... --!!}
@if ($prefixer->isJavaScript())

@endif

{!!-- For normal fields that render server side... --!!}
@error($prefixer->validationKey($field))
    {{ $message }}
@enderror
```

Then just include it...

```
@include('partials.form-error', ['field' => 'field_name'])
```

#### Browser Compatibility (JS)

[](#browser-compatibility-js)

Works with all modern browsers, except Internet Explorer, because it uses template strings:

[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template\_literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

### Modify or Remove the Attribute Name

[](#modify-or-remove-the-attribute-name)

If you only want the attribute's value, without its name and the quotes, you can pass `null` as a second parameter to the `name`, `id` and `for` method, or as a third parameter to the `value` method.

```
 // will echo: some_field
```

You can also specify a different attribute name:

```
id('some_field', 'for') }}> // will echo: for="some_field"
```

### Provide a Fallback FormFieldPrefixer

[](#provide-a-fallback-formfieldprefixer)

If you want to be able to use your partials even without passing it a `FormFieldPrefixer` instance, you could add the following snippet to the top of you partials:

```
@php $prefixer = $prefixer ?? FormFieldPrefixer::make(); @endphp
```

This will make sure there is always a `$prefixer` variable available.

Of course, you are free to choose another variable name.

🚧 Testing
---------

[](#-testing)

```
composer test
```

☕️ Credits
----------

[](#️-credits)

- [Ivan Vermeyen](https://byterider.io)
- [All contributors](../../contributors)

🔓 Security
----------

[](#-security)

If you discover any security related issues, please [e-mail me](mailto:ivan@codezero.be) instead of using the issue tracker.

📑 Changelog
-----------

[](#-changelog)

See a list of important changes in the [changelog](CHANGELOG.md).

📜 License
---------

[](#-license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~211 days

Recently: every ~263 days

Total

6

Last Release

1166d ago

PHP version history (3 changes)1.0.0PHP ^7.1

1.2.0PHP ^7.3

1.3.0PHP ^7.3|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/2e17b7a892452367dfb0e5c55bf04844a16bb781f356f50019332d4b9a476ec6?d=identicon)[codezero](/maintainers/codezero)

---

Top Contributors

[![ivanvermeyen](https://avatars.githubusercontent.com/u/3598622?v=4)](https://github.com/ivanvermeyen "ivanvermeyen (36 commits)")

---

Tags

phplaravelprefixform

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/codezero-laravel-form-field-prefixer/health.svg)

```
[![Health](https://phpackages.com/badges/codezero-laravel-form-field-prefixer/health.svg)](https://phpackages.com/packages/codezero-laravel-form-field-prefixer)
```

###  Alternatives

[stevebauman/location

Retrieve a user's location by their IP Address

1.3k7.6M65](/packages/stevebauman-location)[monicahq/laravel-cloudflare

Add Cloudflare ip addresses to trusted proxies for Laravel.

3372.7M4](/packages/monicahq-laravel-cloudflare)[kra8/laravel-snowflake

Snowflake for Laravel and Lumen.

188402.3k6](/packages/kra8-laravel-snowflake)[barryvdh/laravel-form-bridge

This packages integrates Symfony Form Component in Laravel.

163354.8k1](/packages/barryvdh-laravel-form-bridge)[bezhansalleh/filament-google-analytics

Google Analytics integration for FilamentPHP

205144.8k5](/packages/bezhansalleh-filament-google-analytics)[laragear/preload

Effortlessly make a Preload script for your Laravel application.

119363.5k](/packages/laragear-preload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
