Laravel Custom Field Showcase
Discover real-world examples and use cases of Laravel Custom Field in action. See how different applications leverage the fluent builder API to create dynamic forms, admin panels, and API-driven form builders.
Dynamic Form Builder
Dynamic Form Builder
Build forms programmatically from database configurations
Overview
Create a dynamic form builder where administrators can define form structures in the database, and the system automatically renders them using Laravel Custom Field. This is perfect for CMS systems, survey builders, or any application where form structures need to be configurable without code changes.
Key Features
- Database-driven form definitions
- Dynamic field type rendering
- Conditional field visibility
- Validation rule management
- Form versioning and history
Use Cases
- Survey and questionnaire builders
- Dynamic product configuration
- User registration forms
- Contact form generators
- Application form builders
Implementation Example
use JobMetric\CustomField\CustomFieldBuilder;
class FormBuilderService
{
public function buildForm(array $formConfig): array
{
$fields = [];
foreach ($formConfig['fields'] as $fieldConfig) {
$builder = CustomFieldBuilder::{$fieldConfig['type']}();
$builder->name($fieldConfig['name'])
->label($fieldConfig['label'] ?? null);
if ($fieldConfig['required'] ?? false) {
$builder->required();
}
if (isset($fieldConfig['placeholder'])) {
$builder->placeholder($fieldConfig['placeholder']);
}
if (isset($fieldConfig['options'])) {
$builder->options(function ($opt) use ($fieldConfig) {
foreach ($fieldConfig['options'] as $option) {
$opt->label($option['label'])
->value($option['value']);
if ($option['selected'] ?? false) {
$opt->selected();
}
$opt->build();
}
});
}
if (isset($fieldConfig['data'])) {
foreach ($fieldConfig['data'] as $key => $value) {
$builder->data($key, $value);
}
}
$fields[] = $builder->build();
}
return $fields;
}
public function renderForm(array $fields): string
{
$html = '';
$scripts = [];
$styles = [];
foreach ($fields as $field) {
$fieldHtml = $field->toHtml();
$html .= $fieldHtml['body'];
$scripts = array_merge($scripts, $fieldHtml['scripts']);
$styles = array_merge($styles, $fieldHtml['styles']);
}
return view('forms.dynamic', [
'fields' => $html,
'scripts' => array_unique($scripts),
'styles' => array_unique($styles)
])->render();
}
}
E-Commerce Product Configuration
Product Configuration Forms
Dynamic product attribute selection and customization
Overview
Build product configuration forms where customers can select product attributes like size, color, material, and other customizable options. The form dynamically updates based on product type and available inventory, providing a seamless shopping experience.
Key Features
- Dynamic option loading
- Real-time inventory checking
- Price calculation integration
- Image preview updates
- Validation based on availability
Product Types
- Clothing (size, color, material)
- Electronics (specifications, warranty)
- Furniture (dimensions, finish, fabric)
- Food (quantity, packaging, delivery)
- Custom products (user-defined fields)
Implementation Example
use JobMetric\CustomField\CustomFieldBuilder;
class ProductConfigurationService
{
public function buildConfigurationForm(Product $product): array
{
$fields = [];
// Size selection
if ($product->hasSizes()) {
$sizes = $product->availableSizes()
->where('stock', '>', 0)
->get();
$fields['size'] = CustomFieldBuilder::select()
->name('size')
->label('Size')
->required()
->data('product-id', $product->id)
->data('update-price', true)
->options(function ($opt) use ($sizes) {
$opt->label('Select Size')->value('')->build();
foreach ($sizes as $size) {
$opt->label($size->name)
->value($size->id)
->build();
}
})
->build();
}
// Color selection
if ($product->hasColors()) {
$colors = $product->availableColors()
->where('stock', '>', 0)
->get();
$fields['color'] = CustomFieldBuilder::radio()
->name('color')
->label('Color')
->required()
->data('product-id', $product->id)
->data('update-image', true)
->options(function ($opt) use ($colors) {
foreach ($colors as $color) {
$opt->mode('pro')
->type('radio')
->name('color')
->label($color->name)
->value($color->id)
->data('color-hex', $color->hex_code)
->build();
}
})
->build();
}
// Quantity input
$fields['quantity'] = CustomFieldBuilder::number()
->name('quantity')
->label('Quantity')
->required()
->min(1)
->max($product->maxQuantity())
->value(1)
->data('product-id', $product->id)
->data('update-price', true)
->build();
return $fields;
}
}
Admin Panel Form Builder
Admin Panel Forms
Consistent, maintainable forms across admin interfaces
Overview
Create consistent, maintainable admin panel forms using Laravel Custom Field. Whether you're building CRUD interfaces, settings pages, or complex data entry forms, the fluent API ensures all forms follow the same structure and styling conventions.
Key Features
- Consistent form structure
- Automatic validation integration
- Reusable form components
- Asset management
- Template customization
Form Types
- User management forms
- Content management forms
- Settings and configuration
- Bulk data import forms
- Report generation forms
Implementation Example
use JobMetric\CustomField\CustomFieldBuilder;
class UserFormService
{
public function buildCreateForm(): array
{
return [
'name' => CustomFieldBuilder::text()
->name('name')
->label('Full Name')
->required()
->placeholder('Enter full name')
->build(),
'email' => CustomFieldBuilder::email()
->name('email')
->label('Email Address')
->required()
->placeholder('Enter email address')
->data('check-unique', route('api.users.check-email'))
->build(),
'password' => CustomFieldBuilder::password()
->name('password')
->label('Password')
->required()
->data('min-length', 8)
->build(),
'role' => CustomFieldBuilder::select()
->name('role')
->label('Role')
->required()
->options(function ($opt) {
$opt->label('Select Role')->value('')->build();
$opt->label('Administrator')->value('admin')->build();
$opt->label('Editor')->value('editor')->build();
$opt->label('Viewer')->value('viewer')->build();
})
->build(),
'status' => CustomFieldBuilder::radio()
->name('status')
->label('Status')
->required()
->options(function ($opt) {
$opt->mode('pro')
->type('radio')
->name('status')
->label('Active')
->value('active')
->selected()
->build();
$opt->mode('pro')
->type('radio')
->name('status')
->label('Inactive')
->value('inactive')
->build();
})
->build(),
];
}
public function buildEditForm(User $user): array
{
$fields = $this->buildCreateForm();
// Pre-fill values
$fields['name']->attributes['value'] = $user->name;
$fields['email']->attributes['value'] = $user->email;
$fields['role']->attributes['value'] = $user->role;
$fields['status']->attributes['value'] = $user->status;
// Remove password requirement for edit
unset($fields['password']);
return $fields;
}
}
API Form Builder
API Form Builder
Export form definitions as JSON for frontend frameworks
Overview
Build form definitions in Laravel and export them as JSON for use in frontend frameworks like React, Vue, or Angular. The toArray() method provides a complete serialization of field configurations, making it easy to build API-driven form builders.
Key Features
- JSON serialization
- Frontend framework integration
- Validation rule export
- Option data export
- Asset path information
Use Cases
- React form libraries
- Vue form builders
- Angular reactive forms
- Mobile app forms
- Multi-platform form sync
Implementation Example
use JobMetric\CustomField\CustomFieldBuilder;
class FormApiController extends Controller
{
public function getFormDefinition(string $formType)
{
$fields = $this->buildFormFields($formType);
$formDefinition = [
'fields' => array_map(function ($field) {
return $field->toArray();
}, $fields),
'validation' => $this->getValidationRules($fields),
'assets' => $this->collectAssets($fields),
];
return response()->json($formDefinition);
}
private function buildFormFields(string $formType): array
{
// Build fields based on form type
return [
CustomFieldBuilder::text()
->name('name')
->label('Name')
->required()
->build(),
// ... more fields
];
}
private function collectAssets(array $fields): array
{
$scripts = [];
$styles = [];
foreach ($fields as $field) {
$scripts = array_merge($scripts, $field->getScripts());
$styles = array_merge($styles, $field->getStyles());
}
return [
'scripts' => array_unique($scripts),
'styles' => array_unique($styles),
];
}
}
Get Started
Ready to build your own forms? Get started with Laravel Custom Field: