即时进行自定义验证,但在无效时不会显示错误消息。 你知道问题在哪里吗?我认为这个问题可能在无效函数中。你知道如何设置它为这样的嵌套验证吗?在模型cakephp自定义验证不会在嵌套规则中显示错误消息
var $validate = array(
'receiver' => array(
'maxMsg' => array(
'rule' => array('maxMsgSend'),
//'message' => ''
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'field must not be left empty'
))......
自定义的验证方法:
UPDATE:如何解决它。 我将该函数的内容移至模型中的beforeValidate()。
function beforeValidate($data) {
if (isset($this->data['User']['receiver']))
{
$id = User::$auth['User']['id'];
$count_contacts = (int)$this->Contact->find('count', array('conditions' =>array('and' =>array( 'Contact.contact_status_id' => '2',
'Contact.user_id' => $id))));
$current_credit = (int)$this->field('3_credit_counter', array('id' => $id));
$max_allowed_messages = ($count_contacts >= $current_credit)? $current_credit: $count_contacts ;
if ($data>$max_allowed_messages)
{
$this->invalidate('receiver', "you can send maximum of {$max_allowed_messages} text messages.");
return false;
}
}
return true;
}
实际上它出现了一个错误消息,说maxMsg,即使在我从无效函数中删除maxMsg之后。 它似乎是从嵌套数组中获取名称。 – ondrobaco 2010-01-07 15:20:05