Usage examples
Esta página aún no está disponible en tu idioma.
This document provides practical examples for metadata resolution, nested fields, and error handling.
Basic form model
Section titled “Basic form model”<?php
declare(strict_types=1);
namespace App\FormModel;
use UIAwesome\FormModel\Attribute\{Hint, Label, Placeholder};use UIAwesome\FormModel\BaseFormModel;
final class ContactForm extends BaseFormModel{ #[Label('Full name')] #[Placeholder('Ada Lovelace')] public string $name = '';
#[Hint('Use a valid email address.')] #[Label('Email address')] #[Placeholder('ada@example.com')] public string $email = '';
public function getHints(): array { return [ 'name' => 'Enter your full name.', ]; }
public function getLabels(): array { return [ 'email' => 'Email (map fallback)', ]; }
public function getPlaceholders(): array { return [ 'email' => 'fallback@example.com', ]; }}
$form = new ContactForm();
echo $form->getLabel('name');// "Full name"
echo $form->getHint('email');// "Use a valid email address."Attribute priority over map metadata
Section titled “Attribute priority over map metadata”<?php
echo $form->getLabel('email');// "Email address" from #[Label(...)]
echo $form->getHint('name');// "Enter your full name." from getHints() fallbackGenerated fallback labels
Section titled “Generated fallback labels”<?php
declare(strict_types=1);
namespace App\FormModel;
use UIAwesome\FormModel\BaseFormModel;
final class PostForm extends BaseFormModel{ public string $seoTitle = '';}
$form = new PostForm();
echo $form->getLabel('seoTitle');// "Seo Title"Error collection per field
Section titled “Error collection per field”<?php
$form = new ContactForm();
$form->addError('email', 'Email is required.');$form->addError('email', 'Email format is invalid.');
print_r($form->getError('email'));/*[ 'Email is required.', 'Email format is invalid.',]*/First-error mode and summary output
Section titled “First-error mode and summary output”<?php
$form->setErrors( [ 'email' => ['Email is required.', 'Email format is invalid.'], 'password' => ['Password is required.'], ],);
print_r($form->getFirstErrors());/*[ 'email' => 'Email is required.', 'password' => 'Password is required.',]*/
print_r($form->getErrorSummary());/*[ 'Email is required.', 'Email format is invalid.', 'Password is required.',]*/Nested metadata access
Section titled “Nested metadata access”<?php
declare(strict_types=1);
namespace App\FormModel;
use UIAwesome\FormModel\BaseFormModel;
final class AddressForm extends BaseFormModel{ public string $city = '';
public function getLabels(): array { return ['city' => 'City']; }}
final class ProfileForm extends BaseFormModel{ public AddressForm $address;
public function __construct() { $this->address = new AddressForm(); }}
$form = new ProfileForm();
echo $form->getLabel('address.city');// "City"Field configuration metadata
Section titled “Field configuration metadata”<?php
declare(strict_types=1);
namespace App\FormModel;
use UIAwesome\FormModel\BaseFormModel;
final class LoginForm extends BaseFormModel{ public string $email = '';
public function getFieldConfigs(): array { return [ 'email' => [ 'class' => ['w-full rounded-md border border-slate-300 px-3 py-2'], ], ]; }}
$form = new LoginForm();
print_r($form->getFieldConfig('email'));