DEV Community

sachiko-kame
sachiko-kame

Posted on

Form Helper【CakePHP】

Introduction

Please watch over gently🙇‍♀️
I will write about cakePHP "Form Helper"!
List of things you will often see.

Overall picture

<?= $this->Form->create(null, ['url' => ['controller' => 'Advices', 'action' => 'index']]); ?>
<?= $this->Form->control('title', ['type' => 'text']); ?>
<?= $this->Form->hidden( 'user_id', ['value'=>$userID]); ?>
<?= $this->Form->button(__('tap')) ?>
<?= $this->Form->end() ?>
Enter fullscreen mode Exit fullscreen mode

Create

Cake\View\Helper\FormHelper::create(mixed $context = null, array $options = [])

Basic

$this->Form->create(null);
Enter fullscreen mode Exit fullscreen mode

change action

$this->Form->create(null, ['url' => ['action' => 'publish']]);
Enter fullscreen mode Exit fullscreen mode

change controller & action

$this->Form->create(null, [
    'url' => [
        'controller' => 'Articles',
        'action' => 'publish'
    ]
]);
Enter fullscreen mode Exit fullscreen mode

External

$this->Form->create(null, [
    'url' => 'http://www.google.com/search'
]);
Enter fullscreen mode Exit fullscreen mode

Control

Cake\View\Helper\FormHelper::control(string $fieldName, array $options = [])

Basic

$this->Form->control('title', ['type' => 'text']);
Enter fullscreen mode Exit fullscreen mode
$this->Form->control('title', ['type' => 'checkbox']);
Enter fullscreen mode Exit fullscreen mode
$this->Form->textarea('advice', ['rows' => '3']);
Enter fullscreen mode Exit fullscreen mode

Hide

$this->Form->hidden( 'article_id', ['value'=>'1' ]);
Enter fullscreen mode Exit fullscreen mode

?$this->Form->hidden('id');

Remove required

$this->Form->control('title', ['required' => false]);
Enter fullscreen mode Exit fullscreen mode

button

$this->Form->button('Add');
Enter fullscreen mode Exit fullscreen mode

end

$this->Form->end();
Enter fullscreen mode Exit fullscreen mode

reference

Top comments (0)