PHP code example of hynek / hynek-form

1. Go to this page and download the library: Download hynek/hynek-form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hynek / hynek-form example snippets


// config/form.php


return [
    /**
     *--------------------------------------------------------------------------
     * Form Class
     *--------------------------------------------------------------------------
     *
     * This setting specifies the fully qualified class name of the main Form
     * object used by the package. You can extend the default Form class
     * to add custom functionality and then update this configuration to use
     * your custom class.
     */
    'form_class' => \Hynek\Form\Form::class,

    /**
     *--------------------------------------------------------------------------
     * Default Form Container
     *--------------------------------------------------------------------------
     *
     * This defines the default class used to a form element.
     * Customize this if you want to use a different container class for your forms.
     */
    'default_form_container' => \Hynek\Form\FormContainer::class,

    /**
     *--------------------------------------------------------------------------
     * Default Control Container
     *--------------------------------------------------------------------------
     *
     * This setting specifies the default class used to wrap individual form controls
     * (e.g., input fields, textareas, selects). It often ----------------------------------------------------------------------
     *
     * This setting specifies the default class used to render help text (small,
     * descriptive text) associated with form controls.
     * Update this if you have a custom help text implementation.
     */
    'default_help_text' => \Hynek\Form\HelpText::class,

    /**
     *--------------------------------------------------------------------------
     * Default Error Class
     *--------------------------------------------------------------------------
     *
     * This defines the default class used to render validation error messages
     * for form controls.
     * Change this if you want to use a different class for displaying errors.
     */
    'default_error' => \Hynek\Form\Error::class,

    /**
     *--------------------------------------------------------------------------
     * Elements Collection Class
     *--------------------------------------------------------------------------
     *
     * This configuration specifies the class that represents a collection
     * of form elements (controls, containers, etc.). It's used internally
     * to manage and iterate over groups of form components.
     */
    'elements_collection' => \Hynek\Form\ElementsCollection::class,

    /**
     *--------------------------------------------------------------------------
     * Controls Mapping
     *--------------------------------------------------------------------------
     *
     * This array maps string keys (e.g., 'input', 'textarea') to their
     * corresponding fully qualified class names for different form controls.
     * This allows the package to dynamically create instances of these control
     * classes based on the type requested.
     * You can extend this array to add custom control types or override
     * existing ones with your own implementations.
     */
    'controls' => [
        'input' => \Hynek\Form\Controls\Input::class,
        'textarea' => \Hynek\Form\Controls\TextArea::class,
        'select' => \Hynek\Form\Controls\Select::class,
        'checkbox' => \Hynek\Form\Controls\Checkbox::class,
        'checkbox-group' => \Hynek\Form\Controls\CheckboxGroup::class,
        'radio' => \Hynek\Form\Controls\Radio::class,
        'radio-group' => \Hynek\Form\Controls\RadioGroup::class,
        'button' => \Hynek\Form\Controls\Button::class,
    ],

    /**
     *--------------------------------------------------------------------------
     * View Paths
     *--------------------------------------------------------------------------
     *
     * This array defines the Blade view paths used by the package for rendering
     * various form components. These views are typically located within the
     * `resources/views` directory of the package (or your application if
     * you've published them).
     * You can override these paths to point to your own custom Blade templates.
     * The `form::` prefix indicates views from the `hynek-form` package namespace.
     */
    'views' => [
        'form' => 'form::form',
        'form_container' => 'form::form-container',
        'control_container' => 'form::control-container',
        'input' => 'form::controls.input',
        'textarea' => 'form::controls.textarea',
        'select' => 'form::controls.select',
        'checkbox' => 'form::controls.checkbox',
        'checkbox-group' => 'form::controls.checkbox.group',
        'radio' => 'form::controls.radio',
        'radio-group' => 'form::controls.radio.group',
        'button' => 'form::controls.button',
        'label' => 'form::label',
        'help_text' => 'form::help-text',
        'error' => 'form::error',
    ],
];


use Hynek\Form\Contracts\FormBuilder;
use Hynek\Form\Controls\Input;
use Hynek\Form\Controls\Textarea;
use Hynek\Form\Controls\Button;

// Assuming you've resolved the FormBuilder from the service container
// e.g., via dependency injection or app()->make()
$form = app(FormBuilder::class)
    ->action(route('posts.store'))
    ->method('POST')
    ->element(
        (new Input('title'))
            ->id('post-title')
            ->label('Post Title')
            ->placeholder('Enter post title')
            ->

$form->addAttribute('class', 'my-custom-form');
$form->addAttribute(['data-foo' => 'bar', 'aria-label' => 'Contact Form']);

$form->removeAttribute('method');

$form->name('myForm');

$form->id('my-form-id');

$form->view('my-theme.forms.standard');

$form->action(route('users.store'));

$form->method('POST');

use Hynek\Form\Controls\Input;
use Hynek\Form\Controls\Select;

$form->element(new Input('username')->label('Username'))
     ->element(
         (new Select('role_id'))
             ->label('User Role')
             ->options(['1' => 'Admin', '2' => 'Editor'])
     );

$form->button('Save Changes', 'submit')
     ->button('Cancel'); // Defaults to type="button"

$form->enableAjax();

$form->disableAjax();

$form->livewireSubmit('savePost');

// After fetching existing data
$form->updateValue('title', $existingPost->title);

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hynek\Form\Contracts\FormBuilder;
use Hynek\Form\Controls\Input;
use Hynek\Form\Controls\Textarea;
use Hynek\Form\Controls\Button;

class PostController extends Controller
{
    public function create(FormBuilder $formBuilder)
    {
        $postForm = $formBuilder
            ->action(route('posts.store'))
            ->method('POST')
            ->element(
                (new Input('title'))
                    ->label('Post Title')
                    ->placeholder('Enter post title')
                    ->

// app/Forms/Controls/MyCustomControl.php
namespace App\Forms\Controls;

use Hynek\Form\Controls\FormControl;
use Hynek\Form\Traits\Renderable;

class MyCustomControl extends FormControl
{
    use Renderable;
    
    public function toArray()
    {
        return [];
    }
}

// ...
'controls' => [
    // ... existing controls
    'my-custom-control' => \App\Forms\Controls\MyCustomControl::class,
],
'views' => [
    // ... existing views
    'my-custom-control' => 'my-theme.controls.custom',
],
// ...
bash
php artisan vendor:publish --provider="Hynek\Form\FormServiceProvider"