简体   繁体   中英

CakePHP validation alpha problem w/ validation is checked - what am I doing wrong?

Excuse my ignorance on this question, but what seems to be an obvious fix is not coming together for me..

My validation is working perfectly fine with the exception of when I enter any alpha characters in my form field, I get a sql error:

SQL Error: 1054: Unknown column 'abcde' in 'where clause'...

As you can see I entered 'abcd' as a test..

But if I enter a numeric character and per my validation its all fine.. It appears the alpha value is being read as a column name??

Here is my validation rule:

        ...'Age' => array(
        array(
            'rule' => array('maxLength', 3),
        array(
            'rule' => 'numeric',
            'allowEmpty' => true,
            'message' => 'Age must be numeric.'
        ),
    ),

Here is my controller validation code:

        if ($this->RequestHandler->isAjax()) {
        if ($this->Plan->validates()) {
            $this->Plan->set($this->data);
            $errors = $this->Plan->invalidFields();
            $this->set('errors', $errors);
        } else {
            $this->Plan->set($this->data);
        }
    }

As you can see I am returning my errors to my view, and the correct error "Age must be numeric." does in fact display as expected, but just with the SQL error stuff.

Thanks for any insight as to why this is happening.

Do you even read the manual? It's clearly stated in the CookBook that you need to give your rules names if you want to use multiple rules.

Also, your array is nested completely wrong. I don't know how this could be working, but anyway, this is how your validate should look like:

var $validate = array(
    'Age' => array(
        'length' => array(
            'rule' => array('maxLength', 3)
        ),
        'numbers' => array(
            'rule' => 'numeric',
            'allowEmpty' => true,
            'message' => 'Age must be numeric.'
        )
    )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM