首页  >  文章  >  后端开发  >  CakePHP 验证

CakePHP 验证

PHPz
PHPz原创
2024-08-29 12:58:04693浏览

CakePHP 是一个用于实现动态编程应用程序的开源工具,并为开发人员提供了不同类型的功能。验证是 CakePHP 提供的功能之一,通过使用验证,我们可以根据我们的要求为任意数据数组提供验证。在 CakePHP 中,我们需要在数据验证之前根据形状和大小构建实体。这里我们还需要考虑默认实体,这些实体将在实体对话之前进行验证。我们还可以根据我们的要求应用验证规则。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

什么是 CakePHP 验证?

信息批准是任何应用程序的重要组成部分,因为它有助于确保模型中的信息适应应用程序的业务规则。例如,您应该确保密码长度约为八个字符,或者保证用户名是特殊的。表征批准规则使结构处理变得更加简单。

对于审批周期有不同的看法。我们将在本节中介绍的是模型方面的内容。基本上:当您为模型调用 save() 技术时会发生什么。有关如何处理显示审批错误的更多数据。

CakePHP 验证方法

现在让我们看看 CakePHP 中不同的验证方法,如下所示。

1.添加

将另一个标准添加到字段的标准集中。如果后续争用是一个簇,则该字段的规则列表将被第二个争用取代,而第三个争用将被忽略。

语法

Add(string $specified field, array|string $specified name,
array|Cake\Validation\ValidationRule $required rule [])

说明

在上面的语法中,我们使用带有不同参数的 add 方法。在上面的语法中,指定名称用于定义我们需要添加的规则的名称。该数组用于根据要求定义此规则或多个规则,并返回 $this.

2.允许空

通过使用这个方法,我们可以允许空字段

语法

allowEmpty(string $specified field, boolean|string|callable $whentrue, string|null msgull)

说明

在上面的语法中,我们使用带有不同参数的 add 方法。在上面的语法中,指定名称用于定义我们需要添加的规则的名称。布尔参数用于指示何时需要允许清空,这里我们还可以在执行创建或更新操作时根据 true 或 false 进行验证。消息用于显示消息字段,这将返回 $this.

3.字母数字

通过使用此方法,我们可以根据我们的要求向字段添加字母数字规则。

语法

alphanumeric (string $specified field, string|null $Msgnull, string|callable|null $whennull)

说明

在上面的语法中,我们使用带有不同参数的字母数字方法。在上面的语法中,指定名称用于定义我们需要添加的规则的名称。将另一个标准添加到字段的标准集中。如果后续争用是一个簇,则该字段的规则列表将被第二次争用取代,第三次争用将被忽略,并返回 $this。

4.信用卡

通过该方法,我们可以根据需要在指定字段中添加信用卡规则。

语法

creditCard(string $specified field , string $type'all', string|null $msgnull, string|callable|null $whennull)

说明

在上面的语法中,我们使用信用卡的方法来添加具有不同参数的规则。您需要应用标准的领域。

您需要允许的卡片类型。默认为“全部”。您还可以提供多种认可的卡类型,例如“mastercard”、“visa”、“amex”。

标准达不到标准时的错误消息。当应该应用批准规则并返回 $this 时,“make”或“update”或一个可调用的函数有效。

5.电子邮件

通过使用此方法,我们可以根据我们的要求向该字段添加电子邮件验证规则。

语法

Email(string $specified field , boolean $checkMXfalse, string|null $msgnull, string|callable|null, $whennull)

说明

通过使用上面的语法,我们可以实现电子邮件验证规则。您也需要应用标准的领域。

无论是否检查MX记录。

标准失败时的错误消息。

当需要应用批准规则时,“制作”或“更新”或可调用的函数有效。

6.最大长度

通过使用此方法,我们可以对字段应用字符串验证。

语法

maxLength(string $specified field, integer $max, string|null $msgnull, string|callable|null $whennull)

说明

In the above syntax, we use the maxLength method with different parameters. Here the specified field is used to define the field to which we want to apply the rule, max is used to define the maximum length of string, msgnull is used to show an error message when the rule fails.

7. minLength

By using this method, we can apply string validation to the field.

Syntax

minLength(string $specified field, integer $min, string|null $msgnull, string|callable|null $whennull)

Explanation

In the above syntax, we use the minLength method with different parameters. Here the specified field is used to define the field which we want to apply the rule, min is used to define the minimum length of string, msgnull is used to show an error message when the rule fails.

How to Create CakePHP Validation?

Now let’s see how we can create CakePHP validation with examples as follows. First, we need to make the changes in routes.php file as follows.

<?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('validation',['controller'=>'Valid','action'=>'index']);
$builder->fallbacks();
});
?>

Now create an index.php file and write the following code as follows.

<?php
if($errors) {
foreach($errors as $error)
foreach($error as $mssg)
echo '<font color="red">'.$mssg.'</font><br>';
} else {
echo "There is no errors.";
}
echo $this->Form->create(NULL,array('url'=>'/validation'));
echo $this->Form->control('username of person');
echo $this->Form->control('password');
echo $this->Form->button('Submit');
echo $this->Form->end();
?>

Now execute the above code we will get the following screen as shown below screenshot.

CakePHP 验证

Suppose let’s consider, if we enter only password then it shows username is required as shown in the following screenshot.

CakePHP 验证

Similarly, we can apply validation for username of person filed as shown in the following screenshot as follows.

CakePHP 验证

In this way, we can implement different methods such as to get, post as per our requirement.

Conclusion

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

以上是CakePHP 验证的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:CakePHP pagination下一篇:CakePHP save