CakePHP 表单

WBOY
发布: 2024-08-29 12:58:52
原创
903 人浏览过

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中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!