avtonom/form-helper-error-bundle
Installation:
composer require avtonom/form-helper-error-bundle
Replace ~1.1 with the latest stable version (e.g., ^1.1.0).
Enable Bundle:
Add to config/bundles.php (Laravel 5.4+) or app/AppKernel.php (Symfony):
Avtonom\FormHelper\ErrorBundle\FormHelperErrorBundle::class,
Configure Translator (if not using Symfony’s default):
# config/packages/framework.yaml (Symfony) or .env (Laravel)
FRAMEWORK_TRANSLATOR_FALLBACKS=['%locale%']
First Use Case: Inject the helper into a controller/service:
use Avtonom\FormHelper\ErrorBundle\Service\FormErrorHelper;
public function __construct(private FormErrorHelper $formErrorHelper) {}
public function showFormErrors(Request $request) {
$form = $this->createForm(...);
$form->handleRequest($request);
// Get errors as an array (nested by field)
$errors = $this->formErrorHelper->getErrorsAsArray($form);
return response()->json($errors);
}
Form Validation Feedback:
getErrorsAsArray() to flatten Symfony form errors into a structured array for:
$errors = $this->formErrorHelper->getErrorsAsArray($form);
// Output: ['field.name' => ['Error 1', 'Error 2'], ...]
Conditional Error Handling:
if ($this->formErrorHelper->hasErrors($form)) {
return redirect()->back()->withErrors($errors);
}
Custom Error Messages:
messages.en.yaml):validation:
unique: "This value is already taken."
Integration with Laravel Validation:
Validator for hybrid validation:$validator = Validator::make($data, $rules);
$form = $this->createForm(...)->submit($data);
$errors = array_merge(
$validator->errors()->toArray(),
$this->formErrorHelper->getErrorsAsArray($form)
);
Dynamic Forms:
$errors = $this->formErrorHelper->getErrorsAsArray($form, true); // Deep flatten
Laravel-Specific:
config/services.php:
'form_error_helper' => Avtonom\FormHelper\ErrorBundle\Service\FormErrorHelper::class,
AppServiceProvider:
$this->app->bind('form.helper.error', function ($app) {
return new FormErrorHelper($app['form.factory']);
});
Symfony Forms:
FormBuilder and FormInterface.FormType extensions for custom validation.Testing:
$helper = $this->createMock(FormErrorHelper::class);
$helper->method('getErrorsAsArray')->willReturn(['test' => ['Error']]);
Symfony vs. Laravel Compatibility:
symfony/form via laravel/symfony-form).FormFactory is properly bound (Laravel 5.5+ may need manual binding).Error Structure:
getErrorsAsArray() returns nested arrays by default. Use the second parameter to flatten:
$flatErrors = $this->formErrorHelper->getErrorsAsArray($form, true);
Translation Fallbacks:
messages.{locale}.yaml includes:
validation:
required: "This field is required."
Circular References:
$errors = $this->formErrorHelper->getErrorsAsArray($form, true, 3); // Max depth 3
Bundle Autoloading:
composer.json:
"autoload": {
"psr-4": {
"Avtonom\\FormHelper\\": "vendor/avtonom/form-helper-error-bundle/src"
}
}
dump($form->getErrors(true)); // Symfony's raw error structure
# config/packages/dev/framework.yaml
framework:
translator:
debug: true
\Log::debug('Form errors:', [
'raw' => $form->getErrors(true),
'processed' => $this->formErrorHelper->getErrorsAsArray($form)
]);
Custom Error Formatters:
class CustomFormErrorHelper extends FormErrorHelper {
public function getErrorsAsJson($form) {
return json_encode($this->getErrorsAsArray($form));
}
}
services.php:
'form_error_helper' => CustomFormErrorHelper::class,
Add Custom Validation Rules:
Constraint system and integrate with the bundle’s error handling.Laravel Notifications:
$notification = new FormValidationFailed($errors);
$this->notify($user, $notification);
Frontend Integration:
return response()->json([
'success' => false,
'errors' => $this->formErrorHelper->getErrorsAsArray($form)
]);
How can I help you explore Laravel packages today?