cakePHP 验证根本不起作用
cakePHP validation not working at all
蛋糕版本2.0
我正在做一个 cakePHP 项目,我无法让验证功能正常工作。
我通过订单控制器向付款table(模型)提交数据。
这是视图。
echo $this->Form->create("Payment",array("class"=>"payment"));
echo $this->Form->input("Payment.PaymentID");
echo $this->Form->input("Payment.OrderID",array("value"=>$order["Order"]["OrderID"],"type"=>"hidden"));
echo $this->Form->input("Payment.UserID",array("type"=>"hidden","value"=>$loggedInUserID));
echo $this->Form->input("Payment.TransactionTypeID",array("class"=>"transaction","options"=>$transactions,"label"=>"Transaction Type","empty"=>"Select Transaction Type"));
echo $this->Html->div("transactionType Phone");
echo $this->Form->input("Payment.NameOnCheck",array("class"=>"CheckBy Phone"));
echo $this->Form->input("Payment.CheckRoutingNumber");
echo $this->Form->input("Payment.CheckAccountNumber");
echo "</div>";
echo $this->Html->div("transactionType CreditCard clearfix");
echo $this->Form->input("Payment.CreditCardNumber",array("label"=>"Card Number"));
echo $this->Form->input("CreditCardExpirationMonth",array("label"=>"Expiration Date","options"=>$months));
echo $this->Form->input("CreditCardExpirationYear",array("label"=>" ","options"=>$years));
echo $this->Form->input("Payment.CreditCardExpirationDate",array("type"=>"hidden"));
echo $this->Form->input("Payment.CreditCardCVV",array("label"=>"CVV (not required)","class"=>"noCharge"));
echo $this->Form->input("Payment.CreditCardStreet",array("label"=>"Street Address on Card (not required)","class"=>"noCharge"));
echo $this->Form->input("Payment.CreditCardZipCode",array("Zip Code","class"=>"noCharge"));
echo "</div>";
echo $this->Html->div("transactionType AmountPaid clearfix");
echo $this->Form->input("Payment.PaymentAmount",array("label"=>"Amount Paid"));
echo $this->Form->input("Pay In Full",array("type"=>"button","label"=>" "));
echo "</div>";
echo $this->Html->div("transactionType RefundAmount");
echo $this->Form->input("RefundAmount",array("label"=>"Amount To Refund"));
echo "</div>";
echo $this->Form->end("Complete Transaction");
控制器
$this->Order->Payment->set($this->request->data);
if($this->request->is("post") || $this->request->is("put")) {
if($this->Order->Payment->save($this->request->data)){
$this->Session->setFlash("Payment Added");
//$this->redirect("add_payment/$id");
}
}
支付模式
class Payment extends AppModel{
public $useTable = "Payment";
public $primaryKey = "PaymentID";
public $belongsTo = array(
"TransactionType"=>array(
"foreignKey"=>"TransactionTypeID"
)
);
public $validate = array(
"NameOnCheck" => array(
"rule" => "notBlank"
)
);
其中一个问题是,每当我在 NameOnCheck 字段中提交包含数据的表单时,它都会给我一个错误提示,指出该字段为空白。
它也没有考虑我设置的所有验证。当我对 CheckRoutingNumber 的长度进行验证时,它只是忽略它并且只给我一个 NameOnCheck 的错误(无论是否填写)。
我也在顶部收到两次警告:
preg_match(): Delimiter must not be alphanumeric or backslash [CORE/Cake/Model/Model.php, line 3198]
有没有人对此有解决方案,我们将不胜感激! 谢谢!
除非您在其他地方定义了它,否则规则 notBlank
应该是 notEmpty
以使字段成为必需的(对于低于 2.7 的 CakePHP,如您的情况):-
public $validate = array(
"NameOnCheck" => array(
"rule" => "notEmpty"
)
);
Cake 向您显示错误消息 preg_match(): Delimiter must not be alphanumeric or backslash
,因为 notBlank
不是您应用中定义的规则,因此将其视为自定义正则表达式规则。
正如 Dave 在他对 CakePHP 2.7+ 的评论中所述,您应该使用 notBlank
.