【问题标题】:CodeIgniter\Expression Engine regex form validation not workingCodeIgniter\Expression Engine 正则表达式表单验证不起作用
【发布时间】:2014-08-05 19:48:32
【问题描述】:

拥有运行 ExpressionEngine 和 Expresso 商店的电子商务商店。我正在尝试进行正确的表单验证,但我一直遇到障碍。

Expresso 商店文档有这样的说法:

rules:field_name="rule"

除了使用 require="field_name" 参数,更长的 SafeCracker 风格的语法也可用。对于每个字段,任何 可以指定 CodeIgniter 表单验证规则。如果这个语法是 除了 require="field_name" 参数之外,两者都将 申请。请注意,order_email 字段已经通过 valid_email 规则,不需要手动指定。

文档给出的示例代码:

rules:billing_name="required"
rules:billing_phone="numeric"
rules:billing_postcode="required|min_length[5]|max_length[5]"

这意味着可以指定普通的 CodeIgniter 表单字段规则 - 例如 regex_match。但在实践中,我无法让它发挥作用。也许我的正则表达式有问题,但我真的无法让任何“规则”(例如“数字”或“阿尔法”)可靠地工作。事实上,我只是将 billing_name 设置为数字(所以只有数字),然后我用空格输入了我的全名——绝对没有错误。

以前,我遇到了一些错误——但只有当我使用字母数字时,它才会标记我正在使用的空格。但仅适用于某些领域,而不适用于其他领域。

基本上,我真正想做的只是清除除空格“。”之外的所有特殊字符。和 ”-”。所以空格,句点和破折号。

也许我只是忽略了代码中的一些小问题。这是我第一次尝试在 Expresso Store 中应用 CodeIgniter 表单验证规则。

您在下面看到的代码最终是我将任何可能的东西放入正则表达式的结果,只是为了得到一些一致的东西——错误或其他。

  {exp:store:checkout
    form_class="form-horizontal"
    next="/checkout/confirm"
    error_handling="inline"
    error_delimiters='<span class="checkoutError">|</span>'
    require="name|address1|city|country|postcode|order_email"
    rules:billing_address1="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_address2="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:order_custom1="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_name="regex_match[/^[0-9]{10}$/]"
    rules:billing_city="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_phone="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:billing_postcode="numeric"

    rules:shipping_address1="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_address2="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_name="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_city="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_phone="regex_match[/[^A-Za-z0-9\-\(\) ]/]"
    rules:shipping_postcode="numeric"


    }

      {if no_items}
         {redirect="/checkout/"}
      {/if}

        <div class="w-row">
          <div class="w-col w-col-6 checkoutcolumn1">
            <div class="forminfotextblock">
              <p class="forminfotext">Contact Details</p>
            </div>
            <div class="formblock">
              <div class="w-form">
                  <div class="w-clearfix">
                    {error:billing_name}<label class="checkoutlabel" for="name">Name:</label>
                    <input class="w-input checkoutfield" id="billing_name" type="text" name="billing_name" value="{billing_name}">                   
                  </div>
                  <div class="w-clearfix">
                    {error:billing_address1}<label class="checkoutlabel" for="name-3">Address:</label>
                    <input class="w-input checkoutfield" id="billing_address1" type="text" name="billing_address1" value="{billing_address1}">
                  </div>
                  <div class="w-clearfix">
                     {error:billing_address2}<label class="checkoutlabel" for="name-4">Address 2:</label>
                    <input class="w-input checkoutfield" id="billing_address2" type="text" name="billing_address2" value="{billing_address2}">
                  </div>
                  <div class="w-clearfix">
                    {error:billing_city}<label class="checkoutlabel" for="name-5">City:</label>
                    <input class="w-input checkoutfield" id="billing_city" type="text" name="billing_city" value="{billing_city}">
                  </div>
                  <div class="w-clearfix">
                   {error:billing_postcode} <label class="checkoutlabel" for="name-6">Postcode:</label>
                    <input class="w-input checkoutfield" id="billing_postcode" type="number" name="billing_postcode" value="{billing_postcode}">
                  </div>
                  <div class="w-clearfix">
                    <label class="checkoutlabel" for="billing_state">State:</label>
                    <select class="w-select checkoutfield" id="billing_state" name="billing_state" value="{billing_state}">
                      {billing_state_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                   {error:billing_country}<label class="checkoutlabel" for="billing_country">Country:</label>
                    <select class="w-select checkoutfield" id="billing_country" name="billing_country" value="{billing_country}">
                      {billing_country_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                    {error:order_email}<label class="checkoutlabel" for="name-9">Email:</label>
                    <input class="w-input checkoutfield" id="order_email" type="text" name="order_email" value="{order_email}">
                  </div>
                  <div class="w-clearfix">
                   {error:billing_phone} <label class="checkoutlabel" for="name-10">Phone:</label>
                    <input class="w-input checkoutfield" id="billing_phone" type="text" name="billing_phone" value="{billing_phone}">
                  </div>
                  <div class="w-clearfix">
                     {error:order_custom1}<label class="checkoutlabel" for="name-11">Instructions:</label>
                    <input class="w-input checkoutfield" id="order_custom1" type="text" name="order_custom1" value="{order_custom1}">
                  </div>
                  <div class="w-checkbox">
                    <input type="hidden" name="shipping_same_as_billing" value="0" />
                    <input class="w-checkbox-input" id="shipping_same_as_billing" type="checkbox" name="shipping_same_as_billing" value="1" {shipping_same_as_billing_checked}>
                    <label class="w-form-label" for="shipping_same_as_billing">Same details for shipping</label>               
                  </div>
              </div>
            </div>
          </div>
          <div class="w-col w-col-6 checkoutcolumn2">
            <div class="forminfotextblock">
              <p class="forminfotext">Shipping Address</p>
            </div>
            <div class="formblock">
              <div class="w-form">
                  <div class="w-clearfix">
                    {error:shipping_name}<label class="checkoutlabel" for="name-14">Name:</label>
                    <input class="w-input checkoutfield" id="shipping_name" type="text" name="shipping_name" value="{shipping_name}">
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_address1}<label class="checkoutlabel" for="name-15">Address:</label>
                    <input class="w-input checkoutfield" id="shipping_address1" type="text" name="shipping_address1" value="{shipping_address1}">
                  </div>
                  <div class="w-clearfix">
                   {error:shipping_address2} <label class="checkoutlabel" for="name-16">Address 2:</label>
                    <input class="w-input checkoutfield" id="shipping_addres2" type="text" name="shipping_address2" value="{shipping_address2}">
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_city}<label class="checkoutlabel" for="name-17">City:</label>
                    <input class="w-input checkoutfield" id="shipping_city" type="text" name="shipping_city" value="{shipping_city}">
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_postcode}<label class="checkoutlabel" for="name-23">Postcode:</label>
                    <input class="w-input checkoutfield" id="shipping_postcode" type="number" name="shipping_postcode" value="{shipping_postcode}">
                  </div>
                  <div class="w-clearfix">
                    <label class="checkoutlabel" for="field-3">State:</label>
                    <select class="w-select checkoutfield" id="shipping_state" name="shipping_state" value="{shipping_state}">
                      {shipping_state_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                    {error:shipping_country}<label class="checkoutlabel" for="field-4">Country:</label>
                    <select class="w-select checkoutfield" id="shipping_country" name="shipping_country">
                      {shipping_country_options}
                    </select>
                  </div>
                  <div class="w-clearfix">
                   {error:shipping_phone} <label class="checkoutlabel" for="name-22">Phone:</label>
                    <input class="w-input checkoutfield" id="shipping_phone" type="text" name="shipping_phone" value="{shipping_phone}">
                  </div>
              </div>
            </div>
            <div class="forminfotextblock">
              <p class="forminfotext">Shipping Method</p>
            </div>
            <div class="formblock">
              <div class="w-form">
              {shipping_methods}
                  <div class="w-radio">
                   <label class="w-form-label" for="shipping_method">
                       <input type="radio" class="w-radio-input" name="shipping_method" value="{method_title}" id="{method_id}" />
                       {method_title}
                   </label>
                  </div>      
              {/shipping_methods}           


                 </div>
                 <span id="overnightInfo" style="display:none">
                  <input class="w-input overnightaccountinfo" id="field" type="text" placeholder="Enter your account number" name="field">
                  <br/>
                  <input class="w-input overnightaccountinfo" id="field-2" type="text" placeholder="Enter your account carrier" name="field-2" data-name="Field 2">
                 </span>
                 <br/>
                  <input class="button" name="next" type="submit" value="Review Order">
              </div>
            </div>
          </div>
        </div>
      <script> 
         //on click displays the account and carrier fields
         $('#2').click(function() {
            $("#overnightInfo").toggle(this.checked);
         });

         //on click hides the account and carrier fields
         $('#1').click(function() {
            $("#overnightInfo").toggle(this.hidden);
         });

         //copy billing information over to shipping information fields
         $(function(){
             $("#shipping_same_as_billing").change(function() {
                 if ($("#shipping_same_as_billing:checked").length > 0) {
                     bindGroups();
                 } else {
                     unbindGroups();
                 }
             });
         });

         var bindGroups = function() {
             // First copy values
             $("input[name='shipping_name']").val($("input[name='billing_name']").val());
             $("input[name='shipping_address1']").val($("input[name='billing_address1']").val());
             $("input[name='shipping_address2']").val($("input[name='billing_address2']").val());
             $("input[name='shipping_city']").val($("input[name='billing_city']").val());
             $("input[name='shipping_postcode']").val($("input[name='billing_postcode']").val());
             $("select[name='shipping_state']").val($("select[name='billing_state']").val());
             $("select[name='shipping_country']").val($("select[name='billing_country']").val());
             $("input[name='shipping_phone']").val($("input[name='billing_phone']").val());            

             // Then bind fields
             $("input[name='billing_name']").keyup(function() {
                 $("input[name='shipping_name']").val($(this).val());
             });
             $("input[name='billing_address1']").keyup(function() {
                 $("input[name='shipping_address1']").val($(this).val());
             });
             $("input[name='billing_address2']").keyup(function() {
                 $("input[name='shipping_address2']").val($(this).val());
             });
             $("input[name='billing_city']").keyup(function() {
                 $("input[name='shipping_city']").val($(this).val());
             });
             $("input[name='billing_postcode']").keyup(function() {
                 $("input[name='shipping_postcode']").val($(this).val());
             });
             $("select[name='billing_state']").keyup(function() {
                 $("select[name='shipping_state']").val($(this).val());
             });
             $("select[name='billing_country']").keyup(function() {
                 $("select[name='shipping_country']").val($(this).val());
             });
             $("input[name='billing_phone']").keyup(function() {
                 $("input[name='shipping_phone']").val($(this).val());
             });            
         };

         var unbindGroups = function() {
             $("input[name='billing_name']").unbind("keyup");
             $("input[name='billing_address1']").unbind("keyup");
             $("input[name='billing_address2']").unbind("keyup");
             $("input[name='billing_city']").unbind("keyup");
             $("input[name='billing_postcode']").unbind("keyup");
             $("select[name='billing_state']").unbind("keyup");
             $("select[name='billing_country']").unbind("keyup");
             $("input[name='billing_phone']").unbind("keyup");
         };          
      </script>

 {/exp:store:checkout}  

【问题讨论】:

标签: php regex codeigniter validation expressionengine


【解决方案1】:

想通了。从本质上讲,CodeIgniter 验证规则的触发方式与我预期的不同。某些字符正在被筛选,而其他 (') 则没有。所以,这是完全的用户错误。

【讨论】:

    猜你喜欢
    • 2021-06-18
    • 2018-11-29
    • 2014-04-21
    • 1970-01-01
    • 2016-08-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多