flashgata上的codeigniter form_validation错误未显示

时间:2014-11-06 06:39:23

标签: php codeigniter validation

我的用户/添加表单

<?php echo $this->session->flashdata('message'); ?>
<?php echo form_open('user/save', array('role' => 'form')); ?>
  <div class="form-group">
    <label for="username" class="control-label">Username</label>
    <input type="text" class="form-control" name="username" value="">
  </div>
  <div class="form-group">
    <label for="email" class="control-label">Email</label>
    <input type="email" class="form-control" name="email" value="">
  </div>
  <div class="form-group">
    <label for="password" class="control-label">Password</label>
    <input type="password" class="form-control" name="password" value="">
  </div>
  <div class="form-group">
    <label for="password_confirm" class="control-label">Confirm Password</label>
    <input type="password" class="form-control" name="password_confirm" value="">
  </div>
  <br>
  <div class="form-group">
    <p>
      <?php echo btn_submit('Save'); ?>
      <?php echo link_to_previous_page('user', ' Back'); ?>
    </p>
  </div>
<?php echo form_close(); ?>

我在用户控制器上的保存功能

public function save()
{
  $rules = $this->user->rules;
  $this->form_validation->set_rules($rules);

  if ($this->form_validation->run() == TRUE) {
    $data = array(
      'username'    => $this->input->post('username'),
      'email'     => $this->input->post('email'),
      'password'    => md5($this->input->post('password')),
      'group_id'    => 1,
      'status'    => 1,
      'created'     => date('Y-m-d H:i:s'),
      'created_by'    => date('Y-m-d H:i:s'),
      'updated'     => date('Y-m-d H:i:s'),
      'updated_by'  => $this->session->userdata['username']
    );

    $this->user->save($data);

    $this->session->set_flashdata('message', msg_success('Insert data success'));
    redirect('user');
  }

  $this->session->set_flashdata('message', validation_errors());
  redirect('user/add');
}

我已经在我的user_model上设置了验证规则,我在其中设置了别名&#39; user&#39;,下面是规则:

public $rules = array(
  'username' => array(
    'field' => 'username',
    'label' => 'Username',
    'rules' => 'trim|callback__unique_username|required'
  ),
  'email' => array(
    'field' => 'email',
    'label' => 'Email',
    'rules' => 'trim|valid_email|callback__unique_email|xss_clean|required'
  ),
  'password' => array(
    'field' => 'password',
    'label' => 'Password',
    'rules' => 'trim|matches[password_confirm]|required'
  ),
  'password_confirm' => array(
    'field' => 'password_confirm',
    'label' => 'Confirm Password',
    'rules' => 'trim|matches[password]|required'
  )
);

我在其他表格上做了同样的事情,但它确实有效,我错过了什么?谢谢

2 个答案:

答案 0 :(得分:0)

试试这个

我的用户/添加表单

<?php echo $this->session->flashdata('message'); ?>
<?php echo form_open('user/save', array('role' => 'form')); ?>
  <div class="form-group">
    <label for="username" class="control-label">Username</label>
    <input type="text" class="form-control" name="username" value="">
  </div>
  <?php echo form_error('username'); ?>
  <div class="form-group">
    <label for="email" class="control-label">Email</label>
    <input type="email" class="form-control" name="email" value="">
  </div>
  <?php echo form_error('email'); ?>
  <div class="form-group">
    <label for="password" class="control-label">Password</label>
    <input type="password" class="form-control" name="password" value="">
  </div>
  <?php echo form_error('password'); ?>
  <div class="form-group">
    <label for="password_confirm" class="control-label">Confirm Password</label>
    <input type="password" class="form-control" name="password_confirm" value="">
  </div>
  <?php echo form_error('password_confirm'); ?>
  <br>
  <div class="form-group">
    <p>
      <?php echo btn_submit('Save'); ?>
      <?php echo link_to_previous_page('user', ' Back'); ?>
    </p>
  </div>
<?php echo form_close(); ?>

我在用户控制器上的保存功能

<?php 
public function save()
{
  $rules = $this->user->rules;
  $this->form_validation->set_rules($rules);

  if ($this->form_validation->run() == TRUE) {
    $data = array(
      'username'    => $this->input->post('username'),
      'email'     => $this->input->post('email'),
      'password'    => md5($this->input->post('password')),
      'group_id'    => 1,
      'status'    => 1,
      'created'     => date('Y-m-d H:i:s'),
      'created_by'    => date('Y-m-d H:i:s'),
      'updated'     => date('Y-m-d H:i:s'),
      'updated_by'  => $this->session->userdata['username']
    );

    $this->user->save($data);

    $this->session->set_flashdata('message', msg_success('Insert data success'));
    redirect('user');
  }
else
{
  $this->load->view('load_your_add_from_view_here');
}
}

答案 1 :(得分:0)

您确定要加载会话类吗?

$this->load->library('session');