DEV Community

sachiko-kame
sachiko-kame

Posted on

1

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

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay