douzhuangxuan3268 2015-09-22 01:43
浏览 35
已采纳

如何验证控制器中的表单帖子,还检查产品集合过滤器是否匹配

I have created a simple product selector. In the phtml, I created a form that post data and these data are received by controller to be validated before registering to a registry.

I also have product collection that gets the data to be filter coming from the registry. I have several filters and if one fails, the system should display a message.

This is what I have done so far.

My Controller:

public function indexAction() {
            if ($data = $this->getRequest()->getPost()) {
            $data['ta'] = $this->getRequest()->getPost('torqueAction');
            $data['tr'] = $this->getRequest()->getPost('torqueRequired');
            $data['tm'] = $this->getRequest()->getPost('torqueMetric');
            $data['tmax'] = $data['tr'] / 80 * 100;
            if (validation is equal to true) {
                Mage::register('t-action', $data['ta']);
                Mage::register('t-req', $data['tr']);
                Mage::register('t-metric', $data['tm']);
                Mage::register('t-max', $data['tmax']);
                $this->_redirect('productselector/index/result');
            }else {
                    // Display message about the error
            }
        }
    }

My Collection located in the phtml:

$collection = Mage::getModel('catalog/product')
            ->getCollection()
            ->addFieldTofilter($attr_name, array(
                'eq' => Mage::getResourceModel('catalog/product')
                                ->getAttribute($attr_name)
                                ->getSource()
                                ->getOptionId($attr_val)
                ))
            ->addAttributeToSelect('*')
            ->addFieldTofilter($metric, array('gteq' => $metric_val_min))
            ->addFieldTofilter($metric, array('lteq' => $metric_val_max))
            ->load();
if ($collection == NULL) { // not sure how to validate it if one filter fails
    echo "sorry, no product available";
}else {
    //Display some errors
}

QUESTIONS:

How to validate form post in the controller and checks if empty and no malicious code?

How to check product collection if one filter was not met?

How do I display these errors from controller and product collection?

Thanks

  • 写回答

1条回答 默认 最新

  • douqianni4080 2015-09-22 02:15
    关注

    How to validate form post in the controller and checks if empty and no malicious code?

    You can check an empty post like this.

    if ($this->getRequest()->getPost('field_name', false)) {
         //yes 'fiel_name' is not empty. do something with it.
    }
    

    This is because getPost() will accept two parameters. If the parameter-1 is not set, then it will return the default value that we specified as parameter-2. In this case, default value is false and hence condition fails if field_name is empty.

    I didn't get the "malicious" part in the question. The answer for is, it depends from situation to situation.

    How to check product collection if one filter was not met?

    Honestly, question is not clear for me. However if the filters that you have applied using addFieldToFilter fails, then you will get an empty collection. You can check the count of a collection like this.

    if ($collection->getSize() > 0) {
        //do something with collection
    } else {
        //show some error
    }
    

    How do I display these errors from controller and product collection?

    Errors, warning, success messages are setting on session variable and thus you can get the session in the redirecting page and show them in frontend.

    These are the avaialble notifications in Magento.

    $session  = Mage::getSingleton('customer/session');
    
    //use to show error
    $session->addError('add some error message');
    
    //use to show warning
    $session->addWarning('add some warning message');
    
    //use to show notice
    $session->addNotice('add some notice message');
    
    //use to show success message
    $session->addSuccess('add some success message');
    

    You can set them in controller. Now this is how you can grab all these items and show in frontend. Use them in the form template wisely.

    $messages = Mage::getSingleton('customer/session')->getMessages();
    foreach ($messages as $message) {
        echo $message;
    }
    

    Hope that makes sense.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题