CodeIgniter-是否可以在回调函数中挂载form_validation-> set_rules?

时间:2018-09-04 08:58:20

标签: php codeigniter

嗨,我在通过回调函数挂钩验证时遇到问题。 $ this-> form_validation-> set_message有效,但$ this-> form-> validation-> set_rules不起作用。 TIA

<?php
if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Users extends CI_Controller {
  public function validate_form() {

    $data = array('success' => false, 'messages' => array());

    $this->load->library('form_validation');
    $this->form_validation->set_rules('Employed', '', 'required|callback_is_employed');
    $this->form_validation->set_error_delimiters('<p class="text-danger">','<br />','</p>');

    if ($this->form_validation->run()) {
        $data['success'] = true;
    } else {
        foreach ($_POST as $key => $value) {
            $data['messages'][$key] = form_error($key);
        }
    }
    echo json_encode($data);
  }

  public function is_employed() {
    if($this->input->post('Employed')) {
        $this->form_validation->set_message('is_employed', 'data is checked');
        $this->form_validation->set_rules('EmployedNoB', 'EmployedNoB', 'trim|required');
        $this->form_validation->set_rules('EmployedCA', 'EmployedCA', 'trim|required');
        $this->form_validation->set_rules('EmployedCN', 'EmployedCN', 'trim|required');
        return false;
    }
  }
}
?>

0 个答案:

没有答案