cakePHP验证根本不起作用

时间:2015-08-24 15:57:56

标签: php sql-server cakephp cakephp-2.0 cakephp-2.1

cake version 2.0

我正在开发一个cakePHP项目,但我无法使验证功能正常工作。

我通过订单控制器将数据提交到付款表(模型)。

以下是视图

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():分隔符不能是字母数字或反斜杠[CORE / Cake / Model / Model.php,第3198行]

有没有人有这方面的解决方案,任何帮助将不胜感激! 谢谢!

1 个答案:

答案 0 :(得分:2)

除非你在其他地方定义了它,否则规则notBlank应为notEmpty以使字段成为必需字段(对于CakePHP,2.7以下情况): -

public $validate = array(
    "NameOnCheck" => array(
        "rule" => "notEmpty"
    )
);

Cake正在向您提供错误消息preg_match(): Delimiter must not be alphanumeric or backslash,因为notBlank不是您应用中的已定义规则,因此将其视为自定义正则表达式规则。

正如戴夫在对CakePHP 2.7+的评论中所述,你应该使用notBlank