Codeigniter $ this-> load->查看页面的特定部分(Data-Toggle - Tab)

时间:2022-10-10 09:32:56

I have this admin panel where I use different data-toggle tabs.

我有这个管理面板,我使用不同的数据切换选项卡。

In CI(3), if I go with redirect('user/dashboard#new'); , it redirects me to correct section of view but not with form_validation errors.

在CI(3)中,如果我使用重定向('user / dashboard #new'); ,它将我重定向到正确的视图部分,但没有使用form_validation错误。

And if I try $this->dashboard('user/dashboard#new'); it renders the errors but leads me to the wrong section of page (not at #new).

如果我尝试$ this-> dashboard('user / dashboard #new');它渲染错误但导致我错误的页面部分(而不是#new)。

I have just started developing with CI and looking for some help from seniors.

我刚开始用CI开发并寻求老年人的帮助。

Thanks in advance.

提前致谢。

Controller (user)

控制器(用户)

public function dashboard() {
if($this->session->userdata('is_logged_in')){
$data['homepage'] = '../../templates/vacations/users/dashboard';
$this->load->view('template_users',$data);
}else{
$data['session_error']='Either the session has expired or you have tried to access this page directly';
$this->load->view('../../templates/vacations/headfoot/header-login');
$this->load->view('../../templates/vacations/users/session-error', $data);
$this->load->view('../../templates/vacations/headfoot/footer-login');
}}

Form Validation

表格验证

if($this->form_validation->run() == FALSE)
{
$this->dashboard('user/dashboard#new');
} else {
$this->load->model('model_users');
if($query = $this->model_users->insert_property_details())
{
redirect('user/dashboard#new'); 
} else {
redirect('user/dashboard#new');
}}}

1 个解决方案

#1


1  

$this->dashboard('user/dashboard#new'); just runs/calls the method within the current page. 'user/dashboard#new' does nothing because the method is not written to accept arguments anyway:

$这 - >仪表板( '用户/仪表板#新');只需在当前页面中运行/调用方法。 'user / dashboard #new'不执行任何操作,因为该方法不会被编写为接受参数:

public function dashboard(/* arguments would be here normally */) { ... }

Redirecting right after running validation won't work because you will lose the validation errors when you load a new page.

在运行验证后立即重定向将无法正常工作,因为加载新页面时将丢失验证错误。

You need to save the errors somewhere, such as to session data, then redirect to dashboard, and then load the errors from saved location and display them on the dashboard view.

您需要将错误保存在某处,例如会话数据,然后重定向到仪表板,然后从保存的位置加载错误并将其显示在仪表板视图上。

Here's an example using session data.

这是使用会话数据的示例。

Form method:

表格方法:

if($this->form_validation->run() == FALSE)
{
    $this->session->set_userdata('validation_errors', validation_errors());
    $this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
    redirect('user/dashboard#new');
}
else { ... }

Dashboard method:

仪表板方法:

public function dashboard() 
{
    if($this->session->userdata('is_logged_in')){
        $data['validation_errors'] = $this->session->userdata('validation_errors');
        $data['homepage'] = '../../templates/vacations/users/dashboard';
        $this->load->view('template_users',$data);
    } else { ... }
}

Getting error array from form validation class (for comments below):

从表单验证类获取错误数组(以下评论):

class MY_Form_validation extends CI_Form_validation {
    public function error_array()
    {
        return $this->_error_array;
    }
}

#1


1  

$this->dashboard('user/dashboard#new'); just runs/calls the method within the current page. 'user/dashboard#new' does nothing because the method is not written to accept arguments anyway:

$这 - >仪表板( '用户/仪表板#新');只需在当前页面中运行/调用方法。 'user / dashboard #new'不执行任何操作,因为该方法不会被编写为接受参数:

public function dashboard(/* arguments would be here normally */) { ... }

Redirecting right after running validation won't work because you will lose the validation errors when you load a new page.

在运行验证后立即重定向将无法正常工作,因为加载新页面时将丢失验证错误。

You need to save the errors somewhere, such as to session data, then redirect to dashboard, and then load the errors from saved location and display them on the dashboard view.

您需要将错误保存在某处,例如会话数据,然后重定向到仪表板,然后从保存的位置加载错误并将其显示在仪表板视图上。

Here's an example using session data.

这是使用会话数据的示例。

Form method:

表格方法:

if($this->form_validation->run() == FALSE)
{
    $this->session->set_userdata('validation_errors', validation_errors());
    $this->session->mark_as_flash('validation_errors'); // data will automatically delete themselves after redirect
    redirect('user/dashboard#new');
}
else { ... }

Dashboard method:

仪表板方法:

public function dashboard() 
{
    if($this->session->userdata('is_logged_in')){
        $data['validation_errors'] = $this->session->userdata('validation_errors');
        $data['homepage'] = '../../templates/vacations/users/dashboard';
        $this->load->view('template_users',$data);
    } else { ... }
}

Getting error array from form validation class (for comments below):

从表单验证类获取错误数组(以下评论):

class MY_Form_validation extends CI_Form_validation {
    public function error_array()
    {
        return $this->_error_array;
    }
}