Codeigniter:返回上一个控制器,包括url变量并运行CI验证

时间:2017-08-07 03:58:33

标签: php codeigniter

美好的一天!

场合

我们说我的网址如下:

.....的index.php /家/ edit_something / STR123

现在,如果验证失败,我想重新定向到上一个控制器以及网址代码(STR123)和验证错误,以便系统可以再次检索特定数据。

问题

如何将url变量传递给上一个控制器并同时运行CI验证错误?

这是我的控制器:

public function edit_something()
{   
    // some code here...
}

public function save_edit()
{   
    $this->form_validation->set_rules('my_string', 'String', 'trim|required');

    if ($this->form_validation->run() == FALSE)
    {
        $this->edit_something(); 
        // back to previous controller (with code STR123) when validation fails
    }
    else
    {
        $this->db->trans_begin();

        // some code/query here...

        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
        }
        else
        {
            $this->db->trans_commit();

            // some code/query here...

        }
    }
}

先谢谢你们!

2 个答案:

答案 0 :(得分:1)

在您看来

<?php
    if($products){
        $attrib = array('data-toggle' => 'validator', 'role' => 'form');
        echo admin_form_open_multipart("products/edit/" . $product->id, $attrib)
    }
?>
控制器中的

ProductController.php

function edit($id = NULL){
    if($this->form_validation->run('products/add') == false){
    $this->data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));    
    if($id){
            $this->data['products'] = $this->products->getProductById($id);
        }
        $this->load->view('products/edit', $this->data);
    }
}

如果您不想重定向到控制器而不是视图,那么:

redirect('products/edit'.($id ? '/'.id:''));

答案 1 :(得分:0)

$this->session->set_flashdata('item', array('message' => 'your validation message.'));

redirect(base_url('index.php/home/edit_something/STR123'));