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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化