MilkCore

ObjectToForm
in package

Form Generator from Object Definitions

This class provides methods to automatically generate forms from object definitions. It simplifies form creation by converting object properties into appropriate form fields based on their types and validation rules.

Tags
example
// Define object properties
$fields = [
    'name' => [
        'type' => 'string',
        'label' => 'Full Name',
        'form-params' => ['required' => true]
    ],
    'email' => [
        'type' => 'email',
        'label' => 'Email Address',
        'form-params' => ['required' => true]
    ],
    'age' => [
        'type' => 'int',
        'label' => 'Age',
        'form-params' => ['min' => 18]
    ]
];

// Generate the form
echo ObjectToForm::start('user_form');
foreach ($fields as $field_name => $rule) {
    echo ObjectToForm::row($rule, $_POST[$field_name] ?? '');
}
echo ObjectToForm::end('Save User');

Table of Contents

Methods

end()  : string
get_input()  : string
Generates the appropriate input field based on field type
get_token_name()  : string
Generates a token name for a form
row()  : string
Generates a form row based on a field rule
start()  : string
Generates the opening HTML for a form
submit()  : string

Methods

get_input()

Generates the appropriate input field based on field type

public static get_input(array<string|int, mixed> $rule, mixed $value) : string

This method creates the actual form input element based on the field type specified in the rule. It handles various input types including text, number, email, select, textarea, checkbox, etc.

Parameters
$rule : array<string|int, mixed>

The field definition rule

$value : mixed

The current value of the field

Tags
example
// Generate a text input
$textRule = ['name' => 'title', 'type' => 'string', 'label' => 'Title'];
$textInput = ObjectToForm::get_input($textRule, 'Current Title');

// Generate a select dropdown
$selectRule = [
    'name' => 'category',
    'type' => 'select',
    'label' => 'Category',
    'options' => ['1' => 'Books', '2' => 'Electronics']
];
$selectInput = ObjectToForm::get_input($selectRule, '2');
Return values
string

The HTML for the input field

get_token_name()

Generates a token name for a form

public static get_token_name(string $page) : string

This method creates a standardized token name for CSRF protection based on the page name.

Parameters
$page : string

The page identifier

Return values
string

The generated token name

row()

Generates a form row based on a field rule

public static row(array<string|int, mixed> $rule, mixed $value) : string

This method creates a complete form field with proper wrapping elements based on the field type and rules.

Parameters
$rule : array<string|int, mixed>

The field definition rule

$value : mixed

The current value of the field

Tags
example
// Generate a text input field
$rule = [
    'name' => 'username',
    'type' => 'string',
    'label' => 'Username',
    'form-params' => ['required' => true]
];
echo ObjectToForm::row($rule, $currentValue);
Return values
string

The HTML for the complete form field

start()

Generates the opening HTML for a form

public static start(string $page[, string $url_success = '' ][, string $url_error = '' ][, string $action_save = 'save' ][, array<string|int, mixed> $attributes = [] ]) : string

This method creates the opening form tag with proper attributes, including CSRF token, action, and redirect URLs.

Parameters
$page : string

The page identifier for the form

$url_success : string = ''

The URL to redirect to on successful submission

$url_error : string = ''

The URL to redirect to on error (defaults to current URL)

$action_save : string = 'save'

The action name for form processing (default: 'save')

$attributes : array<string|int, mixed> = []

Additional form attributes

Tags
example
// Basic form start
echo ObjectToForm::start('user_form');

// Form with success and error URLs
echo ObjectToForm::start(
    'product_form',
    Route::url(['page' => 'products']),
    Route::url(['page' => 'product_form', 'error' => 1])
);

// Form with custom attributes
echo ObjectToForm::start('contact_form', '', '', 'send', [
    'class' => 'contact-form',
    'enctype' => 'multipart/form-data'
]);
Return values
string

The HTML for the form opening tag and hidden fields

submit()

public static submit([string $button_text = 'Save' ][, array<string|int, mixed> $button_attr = [] ]) : string
Parameters
$button_text : string = 'Save'
$button_attr : array<string|int, mixed> = []
Return values
string

        
On this page

Search results