<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发
》</a></h5>
<div class="image-container">
<p>
<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">
<img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发">
</a>
</p>
</div>
<div class="text-container" style="font-size:14px; color:#888">
<p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p>
</div>
<hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在Magento 2中,您可以使用客户端JavaScript和服务器端PHP验证来实现自定义表单验证。以下是实现自定义表单验证的示例代码:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">客户端JavaScript验证:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在您的模块中创建一个JavaScript文件,用于包含您的表单验证逻辑,例如 Vendor_Module/view/frontend/web/js/custom-validation.js</p><pre class="brush:as3;toolbar:false">require([
'jquery',
'jquery/ui',
'jquery/validate'
], function($){
$.validator.addMethod(
'custom-validation-rule',
function (value) {
// Add your custom validation logic here
return true; // Return true or false based on validation result
},
$.mage.__('Your validation error message')
);
$('#your-form-id').validate({
rules: {
'form-field-name': {
required: true, // Required validation
custom-validation-rule: true // Your custom validation rule
}
}
});
});</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">将此文件添加到您的布局文件中,例如 Vendor_Module/view/frontend/layout/customer_account_create.xml</p><pre class="brush:as3;toolbar:false"><head>
<script src="Vendor_Module::js/custom-validation.js"/>
</head></pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">服务器端PHP验证:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在您的模块中创建一个 PHP 验证类,例如 Vendor\Module\Model\Validation.php</p><pre class="brush:as3;toolbar:false"><?php
namespace Vendor\Module\Model;
use Magento\Framework\Exception\LocalizedException;
class Validation
{
public function validate($formData)
{
// Add your custom validation logic here
if (empty($formData['field-name'])) {
throw new LocalizedException(__('Your validation error message'));
}
return true; // Return true or false based on validation result
}
}</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">在您的控制器中使用此类来验证表单数据,例如 Vendor\Module\Controller\Index\Save.php</p><pre class="brush:as3;toolbar:false"><?php
namespace Vendor\Module\Controller\Index;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Vendor\Module\Model\Validation;
class Save extends Action
{
protected $validation;
public function __construct(
Context $context,
Validation $validation
) {
parent::__construct($context);
$this->validation = $validation;
}
public function execute()
{
$formData = $this->getRequest()->getPostValue();
if ($formData) {
try {
$isValid = $this->validation->validate($formData);
if ($isValid) {
// Do your save action
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
} catch (LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
}
}
// Return to the form page with errors
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setUrl($this->_redirect->getRefererUrl());
return $resultRedirect;
}
}</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; white-space: normal;">请注意,这些只是示例代码,您需要根据您的具体需求进行修改。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: "Helvetica Neue"; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>