目錄
什麼是 CakePHP 形式?
如何建立 CakePHP 表單?
CakePHP 表單函數
Values
Conclusion
首頁 後端開發 php教程 CakePHP 表單

CakePHP 表單

Aug 29, 2024 pm 12:58 PM
php

CakePHP是一個用於處理動態程式設計的開源工具;它為開發人員提供了不同類型的內建標籤來處理 HTML 表單。表單是 CakePHP 提供的用於可靠處理 HTML 表單的標籤之一,或者我們可以說它與其他 PHP 框架一樣簡單且安全。優點之一是我們可以使用 CakePHP 來產生不同的 HTML 元素。在 CakePHP 中,我們可以透過使用內建標籤和方法輕鬆地根據我們的要求建立表單,並進行所有必要的驗證、不同的佈局。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

什麼是 CakePHP 形式?

此結構包含強大的結構庫記錄,用於在 CakePHP 中執行各種活動。此表單的重點是透過這種方式有效地建立結構,以便簡化審批、重新填入和配置。在表單中,有不同的結構標籤,我們將使用它們在格式組織器中建立結構。此外,該表格非常靈活,這意味著它可以透過使用正確的標點符號和策略來實現特定結果,從​​而完成您需要顯示的幾乎所有結構。一行程式碼比 HTML 程式碼綽綽有餘,因為我們需要編寫龐大的程式碼來建立結構,但在 Form 中,我們需要為結構編寫一個簡單的語法。

如何建立 CakePHP 表單?

現在讓我們看看如何在 CakePHP 中建立表單,範例如下。 首先,我們需要建立一個表單:通常我們知道,當我們使用表單類別時,我們還需要定義子類別。

例如:

namespace App\Form;
use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;
class sampleForm extends Form
{
protected function buildSchema(Schema $schema): Schema
{
return $schema->addField('Emp Name', 'string')
->addField('Emp Email', ['type' => 'string'])
->addField('Emp Address', ['type' => 'text']);
}
public function validationDefault(Validator $valid): Validator
{
$valid->minLength('Emp Name', 20)
->Emp Email('email');
return $valid;
}
protected function _run(array $data): bool
{
// Send an email.
return true;
}
}
登入後複製

說明

在上面的範例中,我們使用了三種不同的方法,分別是 buildSchema、defaultvalidation 和 run,如圖所示。

接下來,我們需要在控制器內部編寫處理請求資料的程式碼,如下所示。

namespace App\Controller;
use App\Controller\AppController;
use App\Form\ sampleForm;
class SampleController extends AppController
{
public function index()
{
$sample= new sampleForm ();
if ($this->request->is('post')) {
if ($sample->execute($this->request->getData())) {
$this->Flash->success( ‘Welcome Done’);
} else {
$this->Flash->error('There is Problem');
}
}
$this->set('sample', $sample);
}
}
登入後複製

之後,我們需要設定表單值,最後,我們需要根據我們的要求建立帶有表單的 HTML。

現在讓我們來看一個例子,以便更好地理解,如下。

首先,我們需要如下配置routes.php。

<?php
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([
'httpOnly' => true,
]));
$builder->applyMiddleware('csrf');
//$builder->connect('/pages',['controller'=>'Pages','action'=>'display', 'home']);
$builder->connect('register',['controller'=>'ContactForm','action'=>'index']);
$builder->fallbacks();
});
登入後複製

現在我們需要建立一個與上面程式碼類似的控制器文件,因此建立一個控制器文件並編寫以下程式碼。

<?php
namespace App\Controller;
use App\Controller\AppController;
class ContactFormController extends AppController{
public function index(){
$country = array('India',England',Canada');
$this->set('country',$country);
$gender = array('Male','Female');
$this->set('gender',$gender);
}
}
?>
登入後複製

說明

在上面的程式碼中,我們編寫了國家和性別等聯絡資訊的程式碼。 現在建立一個索引檔案並編寫以下程式碼。

<?php
echo $this->Form->create(NULL,array('url'=>'/sampleForm'));
echo '<label for="name">Name</label>';
echo '<label for="country">Country</label>';
echo $this->Form->select('country',$country);
echo '<label for="gender">Gender</label>';
echo $this->Form->radio('gender ',$gender);
echo '<label for="address">Address</label>';
echo $this->Form->text ('address');
echo $this->Form->button('Submit');
echo $this->Form->end();
?>
登入後複製

執行上述程式碼後,我們將得到如下螢幕,如下圖所示。

CakePHP 表單

假設我們選擇了像印度這樣的國家,如下圖所示。

CakePHP 表單

現在輸入一些詳細信息,如以下螢幕截圖所示的姓名和地址。

CakePHP 表單

現在點擊提交按鈕,我們會收到一封歡迎訊息。

CakePHP 表單函數

現在讓我們看看CakePHP中的表單函數如下。

選擇選項

用於傳回數組元素。

文法

selectOptions( array $specifiedarrayelement(), array $parentsarrayelement(),
boolean $showParentsnull, array $attributesarray() )
登入後複製

說明

在上面的語法中,我們使用具有不同參數的 selectOption 函數,例如元素的格式、父元素組和不同的 HTML 屬性。基本上,它會傳回數組。

用於選擇格式化元素。

文法

select( string $Specified Name field, array $required options of array(), array $specified attributes array() )
登入後複製

說明

在上面的語法中,我們使用具有不同參數的 select 函數,指定的 name 欄位用於選擇 name 屬性,陣列也用於選擇元素並傳回所選元素。

按鈕

它用於建立帶有類型的按鈕。

文法

Button(string $specified name, array $optionsarray() )
登入後複製

說明

按鈕的銘文。不是自然的 HTML 編碼。一組選擇和 HTML 歸屬,並傳回按鈕標籤。

複選框

透過使用此函數,我們根據我們的要求在表單內建立一個複選框。

文法

Checkbox(string $Specifed name field, array $optionsarray() )
登入後複製

說明

In the above syntax, we use a checkbox function with different parameters such as name and array attributes. It returns the text input element.

Create

It is used to return the returned form element.

Syntax

create( mixed $nullmodel value , array $array() )
登入後複製

Explanation

Here we need to specify the name of the model and the array of specified HTML attributes.

File

By using this function, we can create the file and return the generated file.

Hidden

It is used to create the hidden file and returns the generated hidden input.

Input

It is used to create input elements and return the form widget.

Radio

It is used to create a set of radio buttons and returns the radio widget.

Submit

It is used to create a submit button element and it returns the HTML submit.

Values

Here we can set the default value for form by using setData() method as per our requirement as shown in the following code.

namespace App\Controller;
use App\Controller\AppController;
use App\Form\ContactForm;
class SampleController extends AppController
public function index()
{
$sample = new SampleForm();
if ($this->request->is('post')) {
if ($contact->execute($this->request->getData())) {
$this->Flash->success(' Welcome Done ');
} else {
$this->Flash->error('There is Problem');
}
}
if ($this->request->is('get')) {
$contact->setData([
'Emp of name' => 'sam',
Emp'email' => [email protected]'
]);
}
$this->set('sample', $sample);
}
}
登入後複製

Conclusion

We hope from this article you learn more about the CakePHP form. From the above article, we have taken in the essential idea of the CakePHP form and we also see the representation and example of the CakePHP form. From this article, we learned how and when we use the CakePHP form.

以上是CakePHP 表單的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1669
14
CakePHP 教程
1428
52
Laravel 教程
1329
25
PHP教程
1273
29
C# 教程
1256
24
PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP與Python:了解差異 PHP與Python:了解差異 Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP行動:現實世界中的示例和應用程序 PHP行動:現實世界中的示例和應用程序 Apr 14, 2025 am 12:19 AM

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP的持久相關性:它還活著嗎? PHP的持久相關性:它還活著嗎? Apr 14, 2025 am 12:12 AM

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP和Python:解釋了不同的範例 PHP和Python:解釋了不同的範例 Apr 18, 2025 am 12:26 AM

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python:代碼示例和比較 PHP和Python:代碼示例和比較 Apr 15, 2025 am 12:07 AM

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

See all articles