我正在使用Codeigniter 3,我提到了各种类似问题的stackoverflow链接,如地雷。但我无法解决这个问题。回调函数customAlpha是从file-> MY_Form_validation.php调用的,它当前位于我项目的libraries文件夹中。我多次更改正则表达式,但没有用。请帮忙。
控制器 - > Login_c.php
<?php
class Login_c extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('encrypt');
$this->load->model('User/login_m');
$this->load->library('session');
$this->load->library('form_validation');
}
public function register_user()
{
$this->form_validation->set_rules(
'uname', 'Full Name',
'trim|xss_clean|required|min_length[4]|callback_customAlpha',
array(
'required' => 'Please provide %s.',
'customAlpha' => 'Only characters are allowed in Full Name field'
)
);
$this->form_validation->set_rules('upass', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('cpass', 'Password Confirmation', 'required|matches[upass]');
$this->form_validation->set_rules('uemail', 'Email', 'required|valid_email|is_unique[user.uemail]');
$this->form_validation->set_rules('umobile', 'Mobile Number', 'required|min_length[10]|max_length[10]');
if($this->input->post('oemail'))
{
$this->form_validation->set_rules('oemail', 'Email', 'required|valid_email|is_unique[user.oemail]');
$oemail = $this->input->post('oemail');
}
if ($this->form_validation->run() == FALSE)
{
$errors['err'] = validation_errors();
$this->load->view('User/signup.html',$errors);
}
else
{
//Code to store user entered data in database
}
}
}
?>
MY_Form_validation.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
public function __construct($rules = array())
{
parent::__construct($rules);
}
public function customAlpha($str)
{
//Validation for alphabetical characters and spaces
if ( !preg_match('/^(?:[A-Za-z]+(?:\s[A-Za-z]+|$)){1}$/', $str) )
{
return false;
}
else
{
return true;
}
}
}
?>
答案 0 :(得分:0)
我终于能够自己解决这个问题了。以下是有效的解决方案。
MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array())
{
parent::__construct($config);
}
public function customAlpha($str)
{
if ( !preg_match('/^([a-z]+(-| )?)+$/', $str) )
{
return false;
}
else
{
return true;
}
}
}
Login_c.php
class Login_c extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('encrypt');
$this->load->model('User/login_m');
$this->load->library('session');
$this->load->library('form_validation');
}
public function register_user()
{
$this->form_validation->set_rules(
'uname', 'Full Name',
'required|min_length[4]',
array(
'required' => 'Please provide %s.'
)
);
$this->form_validation->set_rules('upass', 'Password', 'required|min_length[5]');
$this->form_validation->set_rules('cpass', 'Password Confirmation', 'required|matches[upass]');
$this->form_validation->set_rules('uemail', 'Email', 'required|valid_email|is_unique[user.uemail]');
$this->form_validation->set_rules('umobile', 'Mobile Number', 'required|min_length[10]|max_length[10]');
$validation = $this->form_validation->customAlpha($this->input->post('uname'));
if($this->input->post('oemail'))
{
$this->form_validation->set_rules('oemail', 'Email', 'required|valid_email|is_unique[user.oemail]');
$oemail = $this->input->post('oemail');
}
else if ($this->form_validation->run()== FALSE)
{
$errors['err'] = validation_errors();
$this->load->view('User/signup.html',$errors);
}
else if($validation == false)
{
$errors['err'] = "Error in name field";
$this->load->view('User/signup.html',$errors);
}
else
{
//Code to store user entered data in database
}
}
}
?>