We're using kristijanhusak/laravel-form-builder to build form. Visit official document for more information https://kristijanhusak.github.io/laravel-form-builder
Example:
$this
->setModuleName({PLUGIN}_MODULE_SCREEN_NAME)
->setValidatorClass({Plugin}Request::class)
->withCustomFields()
->add('field_name', 'text', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label required'],
'attr' => [
'placeholder' => __('Placeholder'),
'data-counter' => 120,
],
]);
If you want to show form field as mandatory field, add required
class to label attributes.
->add('field_name', 'text', [ // you can change "text" to "password", "email", "number" or "textarea"
'label' => __('Field name'),
'label_attr' => ['class' => 'control-label required'], // Add class "required" if that is mandatory field
'attr' => [
'placeholder' => __('Placeholder'),
'data-counter' => 120, // Maximum characters
],
])
->add('field_name', 'onOff', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label'],
'default_value' => false,
])
->add('field_name', 'editor', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label'],
'attr' => [
'with-short-code' => false, // if true, it will add a button to select shortcode
'without-buttons' => false, // if true, all buttons will be hidden
],
])
->add('field_name', 'select', [ // Change "select" to "customSelect" for better UI
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label required'], // Add class "required" if that is mandatory field
'choices' => [
1 => __('Option 1'),
2 => __('Option 2'),
],
])
->add('field_name', 'customRadio', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label'],
'choices' => [
['option1', 'Option 1'],
['option2', 'Option 2'],
],
])
->add('field_name', 'mediaImage', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label'],
])
->add('field_name', 'color', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label'],
])
->add('field_name', 'time', [
'label' => __('Field label'),
'label_attr' => ['class' => 'control-label'],
])
Default layout template for form is core.base::forms.form
, you can set other layout for form.
Example:
->setFormOption('template', 'core.base::forms.form-modal')
By default, a form will have 2 columns. It's separated by a breaking point. You can set it by using:
->setBreakFieldPoint('field_name');
add_filter(BASE_FILTER_BEFORE_RENDER_FORM, 'add_addition_fields_into_form', 120, 3);
/**
* @param \Botble\Base\Forms\FormAbstract $form
* @param string $screen
*/
function add_addition_fields_into_form($form, $screen, $model)
{
if ($screen == POST_MODULE_SCREEN_NAME) {
$form
->add('test', 'text', [
'label' => __('Test Field'),
'label_attr' => ['class' => 'control-label'],
'attr' => [
'placeholder' => __('Test'),
],
]);
}
}
add_action(BASE_ACTION_AFTER_CREATE_CONTENT, 'save_addition_fields', 120, 3);
add_action(BASE_ACTION_AFTER_UPDATE_CONTENT, 'save_addition_fields', 120, 3);
/**
* @param string $screen
* @param Request $request
* @param Model $object
*/
function save_addition_fields($screen, $request, $object)
{
if ($screen == POST_MODULE_SCREEN_NAME) {
$object->test = $request->input('test');
$object->save();
}
}