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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了