Download the PHP package codezero/laravel-form-field-prefixer without Composer

On this page you can find all versions of the php package codezero/laravel-form-field-prefixer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-form-field-prefixer

Laravel Form Field Prefixer

Laravel Build Status Code Coverage Scrutinizer Code Quality Total Downloads

ko-fi

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

📦 Install

Laravel will automatically register the FormFieldPrefixer facade.

🛠 Usage

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:

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

Generate a for attribute:

This will compile to:

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).

This will compile to:

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.

This will compile to:

Validation Errors

Generate a validation key you can pass to @error:

This will compile to:

Flat Array Forms

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

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

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

Now the address field will look like this:

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

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.

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.

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

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

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:

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

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.

What we are doing here is:

Template Enhancements

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

This will compile to:

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

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

I ended up extracting a form error partial:

Then just include it...

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

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.

You can also specify a different attribute name:

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:

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

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

🚧 Testing

☕️ Credits

🔓 Security

If you discover any security related issues, please e-mail me instead of using the issue tracker.

📑 Changelog

See a list of important changes in the changelog.

📜 License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-form-field-prefixer with dependencies

PHP Build Version
Package Version
Requires php Version ^7.3|^8.0
illuminate/support Version ^7.0|^8.0|^9.0|^10.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package codezero/laravel-form-field-prefixer contains the following files

Loading the files please wait ....