如何发送验证错误按摩?笨

时间:2017-02-27 15:03:36

标签: php forms codeigniter validation

我想在注册表单上显示验证错误消息。登录已存在于数据库中时,应显示消息。

我的错误:无法访问与您的字段名称Login_r对应的错误消息。(规则) 请尽量帮助我,这非常重要。谢谢

配置/ form_validation.php

$config = array(
    'login' => array(
        array(
            'field' => 'login',
            'label' => 'Login',
            'rules' => 'required'
        ),
        array(
            'field' => 'password',
            'label' => 'Password',
            'rules' => 'required'
        )
    ),
    'register' => array(
        array(
            'field' => 'name_r',
            'label' => 'Name',
            'rules' => 'required|alpha'
        ),
        array(
            'field' => 'lastname_r',
            'label' => 'Lastname',
            'rules' => 'required|alpha'
        ),
        array(
            'field' => 'login_r',
            'label' => 'Login_r',
            'rules' => 'required|callback_rule'
        ),
        array(
            'field' => 'password_r',
            'label' => 'Password_r',
            'rules' => 'required|min_length[4]|max_length[12]'
        ),
        array(
            'field' => 'confirm_password_r',
            'label' => 'Confirm_password',
            'rules' => 'required|matches[password_r]'
        ),
        array(
            'field' => 'email_r',
            'label' => 'Email',
            'rules' => 'required|valid_email'
        ),
        array(
            'field' => 'adres_r',
            'label' => 'Adres',
            'rules' => 'required'
        )
    ),                          
);

控制器:

if ($this->form_validation->run('register') == false)
{
    $this->form_validation->set_message('rule', 'Dzialaj !');
    $this->load->view('content/register');
}
else
{
    $this->load->view('content/index');
    $name_r  = $this->input->post('name_r');
    $lastname_r = $this->input->post('lastname_r');
    $login_r = $this->input->post('login_r');
    $password_r  = $this->input->post('password_r');            
    $email_r  = $this->input->post('email_r'); 
    $adres_r = $this->input->post('adres_r');

    $data_db = array(
        'name' => $name_r,
        'lastname' => $lastname_r,
        'login' => $login_r,
        'password' => $password_r,
        'email' => $email_r,
        'adres' => $adres_r
        );
    $this->Main_model->register($data_db);
}

型号:

public function register($data_db) {

    $this->db->where('login',$data_db['login']);
    $query = $this->db->get('users');
    $row = $query->row();

    if($row->login){
        $this->form_validation->set_message('rule', 'Error Message');
    } else {
        $this->db->insert('users', $data_db);
    }

查看(形式)

    <?php
echo validation_errors();
echo form_open();
echo 'Imie: ' . form_input('name_r');
echo br(2);
echo 'Nazwisko: ' . form_input('lastname_r');
echo br(2);
echo 'Login: ' . form_input('login_r');
echo br(2);
echo 'Haslo: ' . form_password('password_r');
echo br(2);
echo 'Potwierdz Haslo: ' . form_password('confirm_password_r');
echo br(2);
echo 'E-mail: ' . form_input('email_r');
echo br(2);
echo 'Adres: ' . form_input('adres_r');
echo br(1);
echo form_submit('zarejestrowany','Stworz konto');

echo form_close();
?>
            </div>
        </div>
    </div>
      <!--Import jQuery before materialize.js-->
      <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
      <script type="text/javascript" src="js/materialize.min.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我不确定您发布的验证码在哪里,也应该在控制器中。我建议重构,这样你就不会在模型中进行任何验证:

型号:

public function get_user($login) {
    $this->db->where('login',$login);
    $query = $this->db->get('users');
    $row = $query->row();
    return $row;
}

public function register($data_db) {
    $this->db->insert('users', $data_db);
}

控制器:

if ($this->form_validation->run('register') == false)
{
    $this->form_validation->set_message('rule', 'Dzialaj !');
    $this->load->view('content/register');
}
else
{
$this->load->view('content/index');
$name_r  = $this->input->post('name_r');
$lastname_r = $this->input->post('lastname_r');
$login_r = $this->input->post('login_r');
$password_r  = $this->input->post('password_r');            
$email_r  = $this->input->post('email_r'); 
$adres_r = $this->input->post('adres_r');

$user = $this->Main_model->get_user();
if($user->login) {
    $this->form_validation->set_message('rule', 'Error Message');
    redirect('content/register')
}
 $data_db = array(
    'name' => $name_r,
    'lastname' => $lastname_r,
    'login' => $login_r,
    'password' => $password_r,
    'email' => $email_r,
    'adres' => $adres_r
              );
$this->Main_model->register($data_db);

这仍然不是很好,因为如果$ user-&gt; login为true我必须使用重定向,那么创建回调会更好,就像我在聊天中提到的那样,但我真的不明白你的代码是如何构建的。

答案 1 :(得分:0)

您的控制器需要一个名为&#34; rule&#34; (因为你的form_validation规则中有一个名为&#34; callback_rule&#34;的规则)。

function rule($form_value)
{
  if(<condition for form value is good>)
  {
    return TRUE;
  }
  else
  {
    $this->form_validation->set_message('login_r','your error message');
    return FALSE;
  }
}