magent表单验证

magent验证

这里主要说一下服务端对表单提交的数据进行验证

在控制器中我们经常看到

//...
if($count->validate()==true){
    try{
        $count->setData($data)->save();
        //...
    }catch(){
        //....
    }
}

其中的validate()函数所对应的验证方法在Model中

public function validate()
    {
        $errors = array();
        $helper = Mage::helper('count');

        if (!Zend_Validate::is($this->getData('Model_Name'), 'NotEmpty')) {
            $errors[] = $helper->__('Please enter the Model Name.');
        }

        if (!Zend_Validate::is($this->getData('Name'), 'NotEmpty')) {
            $errors[] = $helper->__('Please enter the name.');
        }

        if (!Zend_Validate::is($this->getData('Email'), 'EmailAddress')) {
            $errors[] = $helper->__('Invalid email address "%s".');
        }

        if (!Zend_Validate::is($this->getData('Serial_Number'), 'NotEmpty')) {
            $errors[] = $helper->__('Please define date.');
        }

        if (!Zend_Validate::is($this->getData('Data_Purchased'), 'Date')) {
            $errors[] = $helper->__('Please define Data_Purchased.');
        }

        if (!Zend_Validate::is($this->getData('Message_Source'), 'NotEmpty')) {
            $errors[] = $helper->__('Please define Message_Source.');
        }

        if (!Zend_Validate::is($this->getData('Content'), 'NotEmpty')) {
            $errors[] = $helper->__('Please define Content.');
        }

        if (empty($errors) || $this->getShouldIgnoreValidation()) {
            return true;
        }
        return $errors;
    }

magento的默认验证规则在lib/Zend/Validate文件夹下,’NotEmpty’,’EmailAddress’即对应NotEmpty.php,EmailAddress.php文件

发表评论

发表评论

*

沙发空缺中,还不快抢~