Search code examples
phpsql-servercakephpcakephp-2.0cakephp-2.1

cakePHP validation not working at all


cake version 2.0

I am working on a cakePHP project and I can't get the validation function to work correctly.

I submit data to the Payment table (Model) through the Order Controller.

Here is the view.

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");

controller

 $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");
            }

        }

Payment Model

class Payment extends AppModel{
public $useTable = "Payment";
public $primaryKey = "PaymentID";
public $belongsTo = array(
    "TransactionType"=>array(
        "foreignKey"=>"TransactionTypeID"
    )
);
    public $validate = array(
        "NameOnCheck" => array(
            "rule" => "notBlank"
        )
    );

One of the problems is, whenever I submit the form with data in the NameOnCheck field, it gives me an error saying that is blank.

It also doesn't account for all the validations I am setting. When I make a validation for the length of CheckRoutingNumber, it just ignores it and only gives me an error for NameOnCheck (whether it is filled in or not).

I also get this warning at the top, twice:

preg_match(): Delimiter must not be alphanumeric or backslash [CORE/Cake/Model/Model.php, line 3198]

Does anyone have a solution for this, any help would be appreciated! Thanks!


Solution

  • Unless you have defined it elsewhere the rule notBlank should be notEmpty to make a field required (for CakePHP below 2.7 as is your case):-

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

    Cake is giving you the error message preg_match(): Delimiter must not be alphanumeric or backslash because notBlank is not a defined rule in your app so is treating it as a custom regular expression rule.

    As Dave states in his comment for CakePHP 2.7+ you should use notBlank.