JSON-based Form builder for Laravel Enso
This package can work independently of the Enso ecosystem.
The front end implementation that utilizes this api is present in the forms package.
For live examples and demos, you may visit laravel-enso.com
click on the photo to view a short demo in compatible browsers
# InstallationComes pre-installed in Enso.
To install outside of Enso:
install the package composer require laravel-enso/forms
if needed, publish and customize the config
install the front end for the api implementation: yarn add @enso-ui/forms
template.json
file that can be used as an example when starting outForm
object has to be used in the back-end (controller) to parse the template, get additional parameters if required, and build the structureForm
class has fluent helper functions for setting/overriding most attributesVueForm
component needs to be included in the view/page/parent component, taking as parameter the URI path used to obtain the form-builder's resulting objectEnsoForm
component should be included in the view/page/parent component, taking the URI path needed to make the ajax request and fetch the form configuration. Acts like a wrapper for the VueForm
within the Enso ecosystemWhen using the form builder functionality, you will be needing several items:
VueForm
/ EnsoForm
VueJS components from the forms package inside your page/component that renders the form based on the configurationtemplate.json
as an example, and place it inside app/Forms
(recommended). Below is an example of such a template:When giving a number of columns, the fields will be evenly divided into columns, and will have equal width. If a custom value is given, then you may specify on each field the desired width. See below for more information.
When using the money input type, you should read the accounting.js documentation, as these details are outside of the scope of this documentation.
This class will handle the logic for creating the form configuration out of your template. If any extra logic is required to fill or process the form, in addition to the template, this is the place for it.
In the example above, you can see that for the 'roleList' form attribute we're setting the value by using a helper method on the model.
You may even use the available fluent methods to override (if necessary) default values provided in the template.
Form
builder.In the above examples, we're using injection to create an instance of our Form builder class (UserGroupForm), equivalent to instantiating it using new
inside the methods.
VueForm
component. Take a look at the forms UI package documentation for more information.The PHP Form
class provides the following fluent helper functions:
actions(array $actions)
, sets the actions available on the form. Valid actions are create
, store
, update
and delete
routePrefix(string $prefix)
, sets the route prefix that then is used with the various action default endpoints,title(string $title)
, the title for the form,icon(string $icon)
, the icon shown alongside the titleroute(string $action, string $route)
, permits setting a specific route for a given actionoptions(string $name, $value)
, sets the available meta options for a given form attribute Commonly used to override the form value.value(string $field, $value)
, sets the starting value for form element Commonly used to override the form value.hide(string $field)
, marks the field as hiddenshow(string $field)
, marks the field as visible, opposite effect of hide()
hideSection($fields)
, marks as hidden the sections that contain the given fieldsshowSection($fields)
, marks as visible the sections that contain the given fieldshideTab($tabs)
, marks as hidden the tabs with the given names; note that the names are case sensitiveshowTab($tabs)
, marks as visible the tabs with the given names; note that the names are case sensitivedisable(string $field)
, marks the field as disabledreadonly(string $field)
, marks the field as readonlymeta(string $field, string $param, $value)
, sets a specific value, for a meta param, for the given fieldappend($prop, $value)
, adds a property and its value in the template root-level params
object, in order to make it available in the front-end. Note that this params
object is different than the params
object you can pass as a property to the vue-form
/ enso-form
VueJS componentsrouteParams($params)
, set the given parameters as the route parametersauthorize(bool $authorize)
, set the authorize flag for the form. If this value is not given in the form, the global default value is taken from the config filelabels(bool $labels)
, sets the labels attribute on the template. If this value is not given in the form, the global default value is taken from the config filesectionVisibility($fields, bool $hidden)
, set the sections containing the given fields as visible/hidden as per the $hidden parametersectionVisibility($tabs, bool $hidden)
, set the sections containing the given tabs as visible/hidden as per the $hidden parameterIt also provides the 2 methods used for generating the properly formatted form configuration:
create(Model $model)
, for a create-type form, where the model is optional. If given, the model attribute values are filled for the form values (another way of setting some default values)edit(Model $model)
. for an edit-type form, where the model is required. The model's values are set as the form valuesThe Form builder can be globally configured from within its own configuration file, found at config/enso/forms.php
:
The following options are available:
validations
, string, values may be 'local'/'production'/'yourEnvironment'. If set to 'local', form configuration validations are performed only when developing locally, while 'production' will always perform the validation checks.
The flag only affects the validation of the template not the validation of form input values, which is always enabled.
buttons
, array, enables the customization of various options for the buttons used in the forms, such as labels, colors, events and more
altFormat
, string, sets the default date format for datepicker
fields. Note that for these fields an instance of Carbon
is expected
selectPlaceholder
, string, the placeholder used for select fields,
authorize
, boolean, default true
, flag that enables the integration with the Laravel-Enso authorization, meaning that certain user actions are not available if the user does not have access on the corresponding routes
dividerTitlePlacement
, string, default 'center'
, values may be 'left', 'center', 'right'. Affects the placement of sections' divider text, if used and given within the template
labels
, boolean, default true
. When set to false, no labels are used, instead placeholders will be shown with the label values. The option can be overridden for each template.
If using the form normally, by calling create($model)
, edit($model)
methods, then the action is set automatically, as "post" for creation and "patch" for edit.
All form inputs are grouped into sections. See below for the sections details.
# titleThis is the title of the form.
# iconThe Font Awesome 5 icon class, for example "book"
for the "fa-book"
CSS class.
Represents the route prefix that is used when checking permissions and building the route/path for a certain button. For example, for a user form's Save button, if the name of the store route is "administration.users.create"
, then the prefix is "administration.users"
and the action is "create"
.
An object that will hold the route for each action.
# actions"create"
, "show"
, "store"
, "update"
, "destroy"
The actions are used to determine the available buttons in the form. Note that if the authorize
flag is set to true, the builder also checks if the user has access to/for a certain action, and if he does not, the respective button won't be shown.
If the actions are not given, defaults are used, depending on the method
parameter, as follows:
['store']
["create", "show", "update", "destroy"]
If set to true, the form will perform an auto-save when any of its inputs change
# debounceShould be used in conjunction with autosave
so that the number of requests to the backend are limited
Flag that sets whether authorization checks should be made. If not given in the form, the option is read from the global form configuration, found at config/enso/forms.php
. If given, it overrides the global value.
Can be used to pass extra parameters to the VueJS component / front-end, useful when customizing the form in-page (with slots, linking the form component/data to other components in the page, etc).
Notes:
append('attribute', $value)
function on your (LaravelEnso\forms\app\Classes\Form
) form object instanceparams
object is different to the optional params
property of the vue-form
/ enso-form
VueJS component. Keep in mind that this params
object will be accessible in the vue-form
's data object, while the other params
is a property on the vue-form
"left"
, "center"
, "right"
It specifies the relative position of the divider. If not given, the option is read from the global form configuration, found at config/enso/forms.php
The flag activates the tab feature of the form. This then requires that each section has a tab property which specifies the name of the tab the section belongs to.
# labelsIf set to true, then placeholders will be used instead of labels.
# SectionThe section is the organizing block for form inputs.
# columns1
, 2
, 3
, 4
, 6
, 12
, "custom"
, "slot"
The attribute specifies how many columns will be used for the form elements in this section. If giving a number, then the size of each element is calculated automatically.
If using "custom"
, you need to specify for each filed the column size, by providing the column
parameter (see below).
If using "slot"
, then a slot will be rendered for that section. In this case, you also need to specify an additional "slot": "name"
parameter with the desired name of the slot.
The fields parameter will hold the actual form elements. For the configuration of each specific form element, see below.
# dividerFlag that specifies that a divider should be used here.
# titleTitle for the divider. Should be used in conjunction with the divider
parameters, as without setting the divider
to true
, the title will not be shown.
Note that the position of the divider title will depend on the value of the dividerTitlePlacement
parameter (see above).
Flag that specifies the column size. Only needed when the value of columns the value of "columns"
is "custom"
.
Specifies the name of the tab this section belongs to. Each section may have its own tab or multiple sections can share a tab.
When setting this option, the tabs
flag on the main template structure must be present and set to true.
Specifies the name of the slot that should be rendered for this section. Only needed when the value of "columns"
is "slot"
.
Is the individual element of the from, generally representing an input of some sort.
# labelThe label for the element.
# nameThe name of the Model's attribute, that is to be mapped to this input (for instance, the name is also used to fill the models's value when setting up an edit type of form).
The name will be the request's key for the value of the input given be the user, when an action is committed (for instance the user clicks the Save button).
# valueThe starting value for a form element. The value can be
value()
method.Holds various mostly optional parameters that can be used to configure a form element (see Meta below.)
# columnThe size of the column for that element IF using the "custom"
value for section columns
parameter. The given number is used in combination with Bulma's is-
x 12-columns-system. See here for more information.
Note that if columns
parameter is not set to "custom", the column
parameter is not required and is ignored.
Is a set of parameters used to configure the supported form elements.
# Generic # type"input"
, "select"
, "datepicker"
, "timepicker"
, "textarea"
, "password"
, "wysiwyg"
type
is "input"
"input"
Represents the type for an HTML element, and therefore can take the expected types such as "text"
, "number"
, "date"
, "checkbox"
, "password"
, etc. Can also take "money"
(for monetary values inputs).
Flag that marks the disabled state for a form element.
# readonlyFlag that marks the readonly state for a form element.
# placeholderThe placeholder text used on that form element.
# tooltipTooltip used for that form element.
# hiddenFlag that marks the element as hidden, which means it will be rendered but will not be visible.
# customFlag that marks this element as as CUSTOM. What this means is that the VueJS component does not attempt to insert an component for that element, but instead renders a named slot (the name being the element's name
).
This allows you to build and insert custom elements in the form, for complex scenarios.
# Select only # options"select"
If it is an array, it will be considered to be an array of options for that select element, each object should contain an id
and name
label by default, for the value
and label
field respectively. You can modify these keys using the trackBy
and label
options below. If it is a simple string, it will be considered to be an Enum class name, and the builder will attempt to get the select values from the Enum.
"select"
id
Is the attribute that is to be used as identifier for each of the select options i.e. the name of the attribute that is to be used when setting the value for the 'value' attribute of an HTML <option>
element.
"select"
name
Is the attribute that is to be used as label for each of the select options i.e. the name of the attribute that is to be used when setting the value for the an HTML <option>
element.
"select"
Flag that determines the select element to accept multiple values (works as a multi-select).
# source"select"
Flag that determines the select element to work in server-side mode, meaning that it will use the source URI in order to fetch the list of options. When using the source
parameter, the options
parameter is not required.
"select"
Flag that determines if the select options should be translated
# disable-clear"select"
Flag that disables the button to clear a choice which is useful when a selected value is mandatory.
# objects"select"
Flag that puts the select in object mode, which means that the form, instead of storing a primitive value for the selected value, stores the entire option object, which can be useful for complex scenarios
# pivotParams"select"
Pivot params that get used when using the Select component in server-side mode and are passed along with the request, when loading or refreshing the option list.
If using the OptionsBuilder
trait for the select's back-end endpoint, the pivot parameters are applied automatically.
"select"
Custom params that get used when using the Select component in server-side mode and are passed along with the request, when loading or refreshing the option list.
On the back-end, the custom params should be read from the request and applied as required.
# Input only # step"input"
Parameter corresponds to the step parameter for an HTML field.
# min"input"
Parameter corresponds to the min parameter for an HTML <input>
field, where the browser does a client side validation.
"input"
Parameter corresponds to the max parameter for an HTML <input>
field, where the browser does a client side validation.
"datepicker"
, "timepicker"
Represents the format of the date/time used for the component.
Since the flatpickr library is used, it requires its format. For more details, check the documentation .
# time"datepicker"
Flag that enables the time picking functionality for the datepicker, in addition to the default date functionality
# time12hr"datepicker"
Flag that enables the time picker to operate in 12h mode vs 24h mode.
# Textarea only # rows"textarea"
"textarea"
Specifies the number of rows for the textarea.
# Money Input # symbol"money"
-type "input"
Is the current symbol to be used for a money input, for example "$"
.
"money"
-type "input"
Is the precision (decimal places) for the amount.
# thousand"input"
Is the thousands separator for the amount.
# decimal"money"
-type "input"
Is the decimal separator for the amount.
# positive"money"
-type "input"
The format for positive amounts, e.g. "%s %v"
See the accounting.js library for more.
"money"
-type "input"
The format for negative amounts, e.g. "%s (%v)"
See the accounting.js library for more.
"money"
-type "input"
The format for zero amounts, e.g. "%s -- "
See the accounting.js library for more.
Following you will find several non-exhaustive examples, with most if not all of the types, and various parameter combinations.
# Text inputA disabled generic text input
# Numeric inputA numeric text input with a 1-5 range, and a 0.5 step when changing values
# Checkbox inputA checkbox input, with a default value of true.
# TextareaA textarea with a placeholder and a 5 rows height. Note that the textarea is resizable only if you add the "resize": true
property.
A basic what-you-see-is-what-you-get editor input.
# DatePickerThe most basic datepicker, with a placeholder.
# DatePicker with timeA datepicker also with time selection.
# TimepickerA timepicker with a placeholder and 24 hour format time. Note that if you use a 12 hour format time, on change, in the back end, you won't be able to differentiate between AM and PM.
# Single SelectA single select, with a default non-standard option list, a set value, and custom tracking attributes.
# Server Side SelectA server side single select, that fetches the list of options using the named route given as source.
# Multi-SelectA multi select, with no default value, no options and no server-side fetching option.
In this case, you would set the options list from within your controller/service/etc by calling the options method on the form builder object:
Note: For more examples, you may look into the Enso packages for various use cases.
# Publishesphp artisan vendor:publish --tag=form-config
- the configuration file,php artisan vendor:publish --tag=forms
- the example JSON template file & form builder,are welcome. Pull requests are great, but issues are good too.
# LicenseThis package is released under the MIT license.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4