後,我有幾個問題在這裏:CakePHP的:學習數瓊斯蛋糕論壇插件模型驗證規則,表單字段,===
1)是不是必須爲每個字段(出現在模型的驗證規則)成爲數據庫表中的字段?我在Cupcake論壇插件的用戶模型中找到了以下驗證規則。用戶表中的oldPassword和newPassword不是字段。我很困惑因爲我認爲我應該只對表格的字段進行驗證規則。
public $validate = array(
'username' => array(
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'That username has already been taken',
'on' => 'create'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a username'
)
),
'password' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password'
),
'oldPassword' => array(
'rule' => array('isPassword'),
'message' => 'The old password did not match'
),
'newPassword' => array(
'isMatch' => array(
'rule' => array('isMatch', 'confirmPassword'),
'message' => 'The passwords did not match'
),
'custom' => array(
'rule' => array('custom', '/^[-_a-zA-Z0-9]+$/'),
'message' => 'Your password may only be alphanumeric'
),
'between' => array(
'rule' => array('between', 6, 20),
'message' => 'Your password must be 6-20 characters in length'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password'
)
),
'email' => array(
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'That email has already been taken',
'on' => 'create'
),
'email' => array(
//'rule' => array('email', true),//boolean true as second parameter verifies that the host for the address is valid -- to be uncommented once website is uploaded
'rule' => array('email'),
'message' => 'Your email is invalid'
),
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => 'Your email is required'
)
)
);
2)每個表單字段是否需要成爲數據庫表中的字段?
例如,當我要求用戶註冊時,會有:用戶名,電子郵件地址,密碼和確認密碼。但確認密碼字段不需要是表格中的字段吧? 這是一個很好的做法嗎?
我發現form_app_model.php以下isMatch功能:
/**
* Validates two inputs against each other
* @access public
* @param array $data
* @param string $confirmField
* @return boolean
*/
public function isMatch($data, $confirmField) {
$data = array_values($data);
$var1 = $data[0];
$var2 = (isset($this->data[$this->name][$confirmField])) ? $this->data[$this->name][$confirmField] : '';
return ($var1 === $var2);
}
有人能告訴我什麼是在上面的代碼的最後一行===?
謝謝。
''===這裏http://stackoverflow.com/questions/8768958/logical-operators-php-true-讀吧or-false/8768986#8768986 – 2012-03-01 06:46:19
並且不是 - 驗證中的每個字段都不是真實的數據庫字段。 – mark 2012-03-01 10:06:11
謝謝馬克。我知道了。 :) – vaanipala 2012-03-02 05:13:49