如何在opencart中的表单验证错误后重新填充表单数据?

时间:2015-04-24 12:27:53

标签: drupal yii opencart

我有以下代码

public function insert()
{
   $template="information/news_form.tpl"; //Loading the template file for the insert page
  $this->template = ''.$template.'';

 $this->language->load('information/news'); // Calling language file
 $this->load->model('information/news'); // Calling model file
 $this->document->setTitle($this->language->get('heading_title'));

  $this->data['breadcrumbs'] = array(); // Home link
  $this->data['breadcrumbs'][] = array(
     'text' => $this->language->get('text_home'),
     'href' => $this->url->link('common/home', '&token=' . $this->session->data['token'], 'SSL'),
     'separator' => false

  ); 
  $this->data['breadcrumbs'][] = array(
     'text' => $this->language->get('heading_title'),
     'href' => $this->url->link('information/news', '&token=' . $this->session->data['token'], 'SSL'),//to the table
     'separator' => $this->language->get('text_separator')
  );
   $this->data['cancel'] = $this->url->link('information/news', '&token=' . $this->session->data['token'], 'SSL');


   if ($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validateForm($this->request->post['news']))//Data entered by the user  
       {
        $this->model_information_news->insert($this->request->post['news'],$this->session->data['user_id']);//Information is now sent to model. 
        $this->session->data['success'] = $this->language->get('text_success_insert');
        $this->redirect($this->url->link('information/news', 'token=' . $this->session->data['token'], 'SSL'));
        }
         $this->form();//form function call
    } 

Validation function

public function validateForm($news)
{

if (!$this->user->hasPermission('modify', 'catalog/category')) {
        $this->error['warning'] = $this->language->get('error_permission');
    }

 if((utf8_strlen(trim($news[1]['title']))==0)||(utf8_strlen(trim($news[1]['title']))>250))
  $this->error['title']=$this->language->get('error_title');

  if($this->error)
   return false;
  else 
   return true;
 }

形成人口功能

private function form() 
{
    $this->load->language('information/news');
    $this->load->model('information/news');
    $this->load->model('localisation/language');
    $cityId = array();
    //Text from language file
    $this->data['text_title'] = $this->language->get('text_title');
    $this->data['text_description'] = $this->language->get('text_description');
    $this->data['text_image_manager'] = $this->language->get('text_image_manager');
    $this->data['text_image'] = $this->language->get('text_image');
    $this->data['text_browse'] = $this->language->get('text_browse');
    $this->data['text_clear'] = $this->language->get('text_clear');
    $this->data['text_city'] = $this->language->get('text_city');

    $this->data['button_submit'] = $this->language->get('button_submit');
    $this->data['button_cancel'] = $this->language->get('button_cancel');

    $this->data['tab_general'] = $this->language->get('tab_general');
    $this->data['languages'] = $this->model_localisation_language->getLanguages();
    $this->data['token'] = $this->session->data['token'];
    $this->document->setTitle($this->language->get('heading_title'));

     if (isset($this->error['title'])) 
        $this->data['error_title'] = $this->error['title'];
    else 
        $this->data['error_title'] = '';

    if (isset($this->error['warning'])) {
        $this->data['error_warning'] = $this->error['warning'];
    } else {
        $this->data['error_warning'] = '';
    }

    if (isset($this->request->get['news_id'])) //obtaing all the news details.
    { 
       $this->data['news'] = $this->model_information_news->getNews($this->request->get['news_id']);

       $cityList = $this->model_information_news->getAdsCities($this->request->get['news_id']);

       foreach($cityList as $city)
       {
            $cityId[]=$city['city_id'];
       }
       $cityId = implode(",",$cityId);
       $this->data['news']['hidcity']=$cityId;


    }
    else
    { 
        $this->data['news']= '';
    }   
    $this->data['cityOptions'] = $this->generateCities();
    if (isset($this->request->post['title'])) {
        $this->data['title'] = $this->request->post['title'];
    } else {    
        $this->data['title'] = '';
    }


    if (isset($this->request->post['image'])) {
        $this->data['image'] = $this->request->post['image'];
    } elseif (!empty($this->data['news'])) {
        $this->data['image'] = $this->data['news']['image'];
    } else {
        $this->data['image'] = '';
    }

    $this->load->model('tool/image');

    if (isset($this->request->post['image']) && file_exists(DIR_IMAGE . $this->request->post['image'])) {
        $this->data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
    } elseif (!empty($this->data['news']) && $this->data['news']['image'] && file_exists(DIR_IMAGE . $this->data['news']['image'])) {
        $this->data['thumb'] = $this->model_tool_image->resize( $this->data['news']['image'], 100, 100);
    } else {
        $this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
    }

    $this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);


$this->children = array(
        'common/header',
        'common/footer'
    );
    $this->response->setOutput($this->render());
}

当我提交表单并且获得任何验证错误时,所有字段都将被清除。

1 个答案:

答案 0 :(得分:3)

//Error to show in the tpl

    if (isset($this->error['city'])) {
      $this->data['error_city'] = $this->error['city'];
    } else {
      $this->data['error_city'] = '';
    }

//To show the other text value in the form
if (isset($this->request->post['city'])) {
    $this->data['city'] = $this->request->post['city'];
}elseif (isset($this->request->get['id'])) {
  $this->data['city'] = $order_details['city'];
}else {
  $this->data['city'] = '';
}

您已在表单函数中添加此代码。我只举一个例子来说明如何使用代码,您已根据表单更改了data

相关问题