FormComposeComposable CakePHP forms.
Bind CakePHP data, render controls with PHP elements, and keep your application in charge of the HTML.
Bind CakePHP data, render controls with PHP elements, and keep your application in charge of the HTML.
Start with bound controls and ordinary PHP views. Put custom field markup in a PHP element, and extract a blueprint when the same fields repeat. Both approaches use the same fluent configuration and rendering parts.
<?php
$form = $this->Form->bind($article);
?>
<?= $form->open() ?>
<?= $form->control('title')->template('forms/article-title') ?>
<?= $form->control('body')->type('textarea') ?>
<?= $form->control('status')->choices([
'draft' => 'Draft',
'published' => 'Published',
]) ?>
<?= $form->submit('Save article') ?>
<?= $form->close() ?><?php
// templates/element/forms/article-title.php
/**
* @var \FormCompose\Form\Field\BoundField $field
* @var \FormCompose\Rendering\Attributes $attributes
*/
?>
<div<?= (string)$attributes ?>>
<?= $field->label() ?>
<div class="title-input-shell"><?= $field->input() ?></div>
<?= $field->help() ?>
<?= $field->errors() ?>
</div><?php
declare(strict_types=1);
namespace App\Form;
use FormCompose\Attribute\Form;
use FormCompose\Blueprint\BlueprintBuilder;
use FormCompose\Blueprint\FormBlueprint;
use FormCompose\Blueprint\FormContext;
use function Cake\I18n\__;
/**
* Shares article fields across forms while keeping values and saving in CakePHP.
*/
#[Form('article')]
class ArticleForm extends FormBlueprint
{
public function configure(BlueprintBuilder $form, FormContext $context): void
{
$form->add('title');
$form->add('body')->type('textarea');
$form->add('status')->choices([
'draft' => __('Draft'),
'published' => __('Published'),
]);
}
}<?php
echo $this->Form
->form($article)
->blueprint('article')
->errorSummary()
->submit('Save article');Start with your first form, choose your own templates, or share markup through themes. For other tasks, see search forms, file uploads, and custom controls.
CakePHP supplies values, validation metadata, routing, and submission protection. Ordinary forms need no CSS framework or JavaScript runtime.