有一个电子商务商店运行ExpressionEngine和Expresso商店。我正试图获得适当的表单验证,但我一直遇到障碍。
Expresso Store文档是这样说的:
规则:field_name ="规则"
以及使用require="field_name"参数,越长safeccracker风格的语法也是可用的。对于每个字段,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。但在实践中,我无法让它起作用。也许我的Regex是错误的,但我还没有真正能够得到任何"规则"(如"数字"或"alpha")可靠地工作。实际上,我只是将billing_name设置为数字(所以只有数字),并且输入了带空格的全名—绝对没有错误。
之前,我得到了一些错误-但只有当我使用字母数字,它会标记我正在使用的空格。但只适用于某些领域,而不适用于其他领域。
本质上,我真正想做的只是清除除空格、"."one_answers"-"以外的所有特殊字符。空格,句号和破折号。
也许我只是忽略了代码中的一些小问题。这是我第一次尝试在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}
明白了。从本质上讲,CodeIgniter验证规则的运行方式与我预期的不同。某些角色会被筛选,而其他角色则不会。这是完全的用户错误。