草庐IT

php - 想要在 Magento 系统配置中的 system.xml 中进行服务器端验证

coder 2024-07-02 原文

我是 Magento 的新手..我在系统配置下的管理面板中创建了一个自定义配置页面。我已经使用 system.xml 文件给出了一些字段。现在我想对电话号码列进行服务器端验证,但我很难做到..

我在以下路径中给出了以下代码: 应用程序/代码/本地/Envato/CustomConfig/etc/config.xml

   <?xml version="1.0"?>
  <config>
<modules>
    <Envato_CustomConfig>
        <version>0.0.1</version>
    </Envato_CustomConfig>
</modules>
<global>
    <helpers>
        <customconfig>
            <class>Envato_CustomConfig_Helper</class>
        </customconfig>
    </helpers>
    <models>
         <customconfig>
            <class>Envato_CustomConfig_Model</class>
         </customconfig>
    </models>
</global>
<adminhtml>
    <acl>
        <resources>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <customconfig_options>
                                        <title>Custom Configuration Section</title>
                                    </customconfig_options>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>
</adminhtml>
 </config>

我的 system.xml 文件: 代码/本地/Envato/CustomConfig/etc/system.xml:

      <?xml version="1.0"?>
   <config>
  <tabs>
    <customconfig translate="label" module="customconfig">
        <label>Custom Configuration Tab</label>
        <sort_order>1</sort_order>
      </customconfig>
  </tabs>

  <sections>
    <customconfig_options translate="label" module="customconfig">
        <label>Custom Configuration Settings</label>
        <tab>customconfig</tab>
        <frontend_type>text</frontend_type>
        <sort_order>1</sort_order>
        <show_in_default>1</show_in_default>
        <show_in_website>1</show_in_website>
        <show_in_store>1</show_in_store>
        <groups>
            <section_one translate="label">
                <label>Section One</label>
                <frontend_type>text</frontend_type>
                <sort_order>1</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>               
                <fields>
                    <custom_field_one>
                        <label>Custom Text Field</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of text field.      </comment>                  
                    </custom_field_one>
                    <custom_field_two>
                        <label>Image</label>
                        <frontend_type>image</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_two>
                    <custom_field_three>
                        <label>Email Id</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_three>
                    <custom_field_four>
                        <label>Mobile Number</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <validate>validate-number</validate>
           <backend_model>customconfig/number</backend_model> 
                        <sort_order>4</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_four>
                    <custom_field_five>
                        <label>Password</label>
                        <frontend_type>password</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>5</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_five>
                    <custom_field_six>
                        <label>Booking Date</label>
                        <frontend_type>text</frontend_type>
                        <validate>required-entry</validate>
                        <sort_order>6</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                    </custom_field_six>
                </fields>
            </section_one>
            <section_two translate="label">
                <label>Section Two</label>
                <frontend_type>text</frontend_type>
                <sort_order>2</sort_order>
                <show_in_default>1</show_in_default>
                <show_in_website>1</show_in_website>
                <show_in_store>1</show_in_store>               
                <fields>
                    <custom_field_two>
                        <label>Custom Select Field</label>
                        <frontend_type>select</frontend_type>
                        <source_model>customconfig/options</source_model>
                        <sort_order>1</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of select field.</comment>
                    </custom_field_two>
                    <custom_field_three>
                        <label>Custom Radio Field</label>
                        <frontend_type>radios</frontend_type>
                        <source_model>customconfig/options</source_model>
                        <sort_order>2</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of radios field.</comment>
                    </custom_field_three>
                    <custom_field_four>
                        <label>Custom Multiselect Field</label>
                        <frontend_type>multiselect</frontend_type>
                        <source_model>customconfig/options</source_model>
                        <sort_order>3</sort_order>
                        <show_in_default>1</show_in_default>
                        <show_in_website>1</show_in_website>
                        <show_in_store>1</show_in_store>
                        <comment>Example of multiselect field.</comment>
                    </custom_field_four>
                </fields>
            </section_two>
        </groups>               
    </customconfig_options>
</sections>
 </config>

我的助手文件:/app/code/local/Envato/CustomConfig/Helper/Data.php

  <?php

  class Envato_CustomConfig_Helper_Data extends Mage_Core_Helper_Abstract
   {
    }

我的模型文件:app/code/local/Envato/CustomConfig/Model/Options.php

      <?php
        class Envato_CustomConfig_Model_Options
       {
      public function toOptionArray()
         {
 return array(
  array('value'=>1, 'label'=>'One'),
  array('value'=>2, 'label'=>'Two'),
  array('value'=>3, 'label'=>'Three'),           
  array('value'=>4, 'label'=>'Four')                    
 );
   }
   }

我的模型文件:app/code/local/Envato/CustomConfig/Model/Options.php

      <?php
     class Envato_CustomConfig_Model_Number extends Mage_Core_Model_Abstract
       {
public function save()
{   
    $number = $this->getValue(); //get the value from our config
    $number = preg_replace('#[^0-9]#','',$number); //strip non numeric
    if(strlen($number) < 10)   //exit if we're less than 10 digits long
    {
         Mage::getSingleton('core/session')->addNotice('Phone Numbers need 10 digits.');
    }

    return parent::save();  

    }
     }

我的标签在 system.xml 文件中工作。但是我收到数据存储的错误消息。我想在数据存储到数据库之前进行服务器端验证...... 任何人都可以帮助对这些字段进行服务器端验证..

提前致谢......

最佳答案

我认为您的模型类存在问题。请查看此详细教程:

http://alanstorm.com/magento_system_config_validation

更新:

class Envato_CustomConfig_Model_Number extends Mage_Core_Model_Config_Data{


public function save() {   
        $number = $this->getValue(); //get the value from our config
        $number = preg_replace('#[^0-9]#','',$number); //strip non numeric
        if(strlen($number) < 10)   //exit if we're less than 10 digits long
        {
             Mage::getSingleton('core/session')->addError('Phone Numbers need 10 digits.');
             return true;
        }

        return parent::save();  
    }
}

使用这个模型..它不会保存数据直到它有效。

关于php - 想要在 Magento 系统配置中的 system.xml 中进行服务器端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873116/

有关php - 想要在 Magento 系统配置中的 system.xml 中进行服务器端验证的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - 如何从 format.xml 中删除 <hash></hash> - 2

    我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为

  4. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  5. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  6. ruby - 用 Ruby 编写一个简单的网络服务器 - 2

    我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b

  7. ruby-on-rails - 在 Rails 中调试生产服务器 - 2

    您如何在Rails中的实时服务器上进行有效调试,无论是在测试版/生产服务器上?我试过直接在服务器上修改文件,然后重启应用,但是修改好像没有生效,或者需要很长时间(缓存?)我也试过在本地做“脚本/服务器生产”,但是那很慢另一种选择是编码和部署,但效率很低。有人对他们如何有效地做到这一点有任何见解吗? 最佳答案 我会回答你的问题,即使我不同意这种热修补服务器代码的方式:)首先,你真的确定你已经重启了服务器吗?您可以通过跟踪日志文件来检查它。您更改的代码显示的View可能会被缓存。缓存页面位于tmp/cache文件夹下。您可以尝试手动删除

  8. 电脑0x0000001A蓝屏错误怎么U盘重装系统教学 - 2

      电脑0x0000001A蓝屏错误怎么U盘重装系统教学分享。有用户电脑开机之后遇到了系统蓝屏的情况。系统蓝屏问题很多时候都是系统bug,只有通过重装系统来进行解决。那么蓝屏问题如何通过U盘重装新系统来解决呢?来看看以下的详细操作方法教学吧。  准备工作:  1、U盘一个(尽量使用8G以上的U盘)。  2、一台正常联网可使用的电脑。  3、ghost或ISO系统镜像文件(Win10系统下载_Win10专业版_windows10正式版下载-系统之家)。  4、在本页面下载U盘启动盘制作工具:系统之家U盘启动工具。  U盘启动盘制作步骤:  注意:制作期间,U盘会被格式化,因此U盘中的重要文件请注

  9. 【鸿蒙应用开发系列】- 获取系统设备信息以及版本API兼容调用方式 - 2

    在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList​()Obt

  10. kvm虚拟机安装centos7基于ubuntu20.04系统 - 2

    需求:要创建虚拟机,就需要给他提供一个虚拟的磁盘,我们就在/opt目录下创建一个10G大小的raw格式的虚拟磁盘CentOS-7-x86_64.raw命令格式:qemu-imgcreate-f磁盘格式磁盘名称磁盘大小qemu-imgcreate-f磁盘格式-o?1.创建磁盘qemu-imgcreate-fraw/opt/CentOS-7-x86_64.raw10G执行效果#ls/opt/CentOS-7-x86_64.raw2.安装虚拟机使用virt-install命令,基于我们提供的系统镜像和虚拟磁盘来创建一个虚拟机,另外在创建虚拟机之前,提前打开vnc客户端,在创建虚拟机的时候,通过vnc

随机推荐