codeigniter中的表单验证无效

时间:2013-11-28 01:26:26

标签: php codeigniter

在下面的codeigniter代码中没有在文本框中输入数据并提交它。它应该抛出表单验证。在我的情况下,只有表单验证不会抛出它是必需的,除了email.Pls帮助我解决问题。 控制器:

function create_member()
    {
        $this->load->library('form_validation');

        // field name, error message, validation rules
        $this->form_validation->set_rules('name', 'Name', 'trim|required');

        $this->form_validation->set_rules('college_name', 'college_name', 'trim|required');
        $this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
    $this->form_validation->set_rules('phone_number', 'Phone Number', 'trim|required');
            $this->form_validation->set_rules('address', 'address', 'trim|required');
        $this->form_validation->set_rules('message', 'message', 'trim|required');
         $this->load->helper('date');

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('contact_view');
        }

        else
        {           
            $this->load->model('contact_model');

            if($query = $this->contact_model->create_member())
            {

                $this->load->view('contact_view');  
                    redirect('contact', 'refresh'); 
            }


        }

    }


}

查看:

<?php

echo form_open('contact/create_member');

echo form_input('name', set_value('name', ' Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
echo form_input('college_name', set_value('college_name', 'college_name'));
echo form_input('address', set_value('address', 'address'));
echo form_input('phone_number', set_value('phone_number', 'phone_number'));
echo form_textarea('message', set_value('message', 'message'));
echo form_submit('submit', 'Submit');

?>

4 个答案:

答案 0 :(得分:1)

在您的视图中,您已在这些输入中设置了值。如果规则仅为form validation,则required将返回true。您不应该为这些输入设置默认值,但使用placeholder属性显示消息,而输入字段没有值。

您的视图中还有另一个问题。你的表格没有结束。

您可以在echo form_close();

之后添加一行form_submit

答案 1 :(得分:1)

在视图中你应该添加这一行

    <?php echo validation_errors(); ?>

你的代码

echo validation_errors(); // line to display error

echo form_open('contact/create_member');

echo form_input('name', set_value('name', ' Name'));
echo form_input('email_address', set_value('email_address', 'Email Address'));
echo form_input('college_name', set_value('college_name', 'college_name'));
echo form_input('address', set_value('address', 'address'));
echo form_input('phone_number', set_value('phone_number', 'phone_number'));
echo form_textarea('message', set_value('message', 'message'));
echo form_submit('submit', 'Submit');

答案 2 :(得分:1)

    **admin.php (controller)**

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class Admin extends CI_Controller {



        public function index()
        {

          $this->login_validation();
        }

        public function login_validation()
        {
            $validations = array(

                array(
                        'field' => 'email',
                        'label' => 'Email',
                        'rules' => 'required|trim|valid_email|xss_clean'
                ),

                array(
                        'field' => 'password',
                        'label' => 'Password',
                        'rules' => 'required|trim|min_length[6]|max_length[20]|xss_clean',
                        'errors' => array(
                            'required' => 'You must provide a %s.'
                        )
                    )


            );


            $this->form_validation->set_rules($validations);


            if ($this->form_validation->run() == TRUE)
            {

                   $form_fields = array(
                        'email' => $this->input->post('email'),
                        'password' => $this->input->post('password'),
                        'remember' => $this->input->post('remember_me')
                   );


                    $this->load->model('admin_login_model');

                    $result = $this->admin_login_model->get_admin_login_data($form_fields);

                    if($result === TRUE){

                       echo 'Login suceess';
                    }
                    else{

                         echo 'Wrong Login Credential';
                         $this->load->view('admin/admin_login_form');
                    }
            }
            else
            {
                    $this->load->view('admin/admin_login_form');
            }



        }



    }


**admin_login_form.php (view)**    

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>Admin Login</title>
      <link rel="stylesheet" media="screen" href="<?=site_url('assets/css/bootstrap.min.css');?>" />
   </head>

   <body>
      <div class="container">
         <div id="loginbox" style="margin-top:50px;" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
            <div class="panel panel-info" >
               <div class="panel-heading">
                  <div class="panel-title">Admin Login</div>
                  <div style="float:right; font-size: 80%; position: relative; top:-10px">
                  <a href="#" onClick="$('#loginbox').hide(); $('#forgotbox').show()">Forgot password?</a></div>
               </div>
               <div style="padding-top:30px" class="panel-body" >
                  <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                  <?php echo validation_errors(); ?>

                  <?php
                     $form_attributes = array(
                        'class' => 'form-horizontal', 
                        'id' => 'loginform',
                        'role' => 'form'
                        );
                     echo form_open('admin/', $form_attributes);

                  ?>
                     <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                       <?php 
                        $email_attributes = array(
                        'id'=>'login-username',
                        'class' => 'form-control',
                        'name'=>'email',
                        'placeholder' => 'Email',
                        'value'=>set_value('email'),
                        'placeholder'=>'UserName',
                        );
                        echo form_input($email_attributes);
                       ?>                                     
                     </div>
                     <div style="margin-bottom: 25px" class="input-group">
                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                     <?php 
                        $password_attributes = array(
                        'id'=>'login-password',
                        'class' => 'form-control',
                        'name'=>'password',
                        'placeholder' => 'password',
                        'value'=>set_value('password'),
                        'placeholder'=>'Password',
                        );
                        echo form_password($password_attributes);  
                     ?> 
                     </div>
                     <div class="input-group">
                        <div class="checkbox">
                           <label>
                           <?php
                              $checkbox_attributes = array(
                               'name'        => 'remember_me',
                               'id'          => 'login-remember',
                               'value'       => '1',
                               'checked'     => TRUE,
                              );
                              echo form_checkbox($checkbox_attributes);
                           ?>
                             Remember me
                           </label>
                        </div>
                     </div>
                     <div style="margin-top:10px" class="form-group">
                        <!-- Button -->
                        <div class="col-sm-12 controls">
                      <?php
                              $submit_attributes = array(
                               'name'        => 'submit',
                               'id'          => 'btn-login',
                               'class'       => 'btn btn-success',
                               'value'       => 'Login',
                              );
                             echo form_submit($submit_attributes);
                     ?>
                        </div>
                     </div>
                   <?php
                  echo form_close();
                  ?>
               </div>
            </div>
         </div>
      <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
   </body>
</html>

答案 3 :(得分:0)

**Form validation, image upload and captcha using codeigniter libraries**

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Loginmodel');

    }
    public function index()
    {
        $config = array(
            'img_path'      => 'uploadss/',
            'img_url'       => base_url().'uploadss/',
            'font_path'     => base_url().'system/fonts/texb.ttf',
            'img_width'     => '200',
            'img_height'    => 90,
            'word_length'   => 3,
            'font_size'     => 25
        );
        $captcha = create_captcha($config);

        // Unset previous captcha and set new captcha word
        $this->session->unset_userdata('captchaCode');
        $this->session->set_userdata('captchaCode', $captcha['word']);

        // Pass captcha image to view
        $fetch['captchaImg'] = $captcha['image'];

        $fetch['data'] = $this->Loginmodel->alldata();
        $this->load->view('login',$fetch);
    }

    public function loginerror()
    {
        $this->form_validation->set_rules('fname','first name','required|alpha');
        $this->form_validation->set_rules('lname','last name', 'required');
        $this->form_validation->set_rules('mobile', 'Mobile', 'required|numeric');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('companyname', 'Companyname', 'required');
        $this->form_validation->set_rules('designation', 'Designation', 'required');
        $this->form_validation->set_rules('companysize', 'Companysize', 'required|numeric');

        if($this->form_validation->run())
        {
            $inputCaptcha = $this->input->post('captcha');
            $sessCaptcha = $this->session->userdata('captchaCode');
            if($inputCaptcha === $sessCaptcha)
            {
                echo 'Captcha code matched.';

                    $fname = $this->input->post('fname');
                    $lname = $this->input->post('lname');
                    $mobile = $this->input->post('mobile');
                    $email = $this->input->post('email');
                    $password = $this->input->post('password');
                    $companyname = $this->input->post('companyname');
                    $designation = $this->input->post('designation');
                    $companysize = $this->input->post('companysize');

                    $checkmobile = $this->Loginmodel->checkmobile($mobile,$email);

                    if($checkmobile)
                    {
                        $this->session->set_flashdata("danger","Mobile Number or Email exist.....");
                        return redirect('Login/index'); 
                    }
                    else
                    {
                        $insertdata = $this->Loginmodel->insert($fname,$lname,$mobile,$email,$password,$companyname,$designation,$companysize);
                        $this->session->set_flashdata("success","Record Inserted");
                        return redirect('Home/indexhome');
                    }   
                // }
                // else
                // {
                //  $this->session->set_flashdata("danger","Please fill all the values properly");
                //  $this->index();
                // }
            }
            else
            {
                echo 'Captcha code does not match, please try again.';
                $this->index();
            }
        }
        else
        {
            $this->session->set_flashdata("danger","Please fill all the values properly");
            $this->index();
        }
    }

    public function refresh(){
        // Captcha configuration
        $config = array(
            'img_path'      => 'uploadss/',
            'img_url'       => base_url().'uploadss/',
            'font_path'     => base_url().'system/fonts/texb.ttf',
            'img_width'     => '200',
            'img_height'    => 90,
            'word_length'   => 3,
            'font_size'     => 25
        );
        $captcha = create_captcha($config);

        $this->session->unset_userdata('captchaCode');
        $this->session->set_userdata('captchaCode',$captcha['word']);

        echo $captcha['image'];
    }

    public function upload($id)
    {
        if(!empty($_FILES['imagename']['name']))
        {
                $config['upload_path'] = 'uploadss/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['file_name'] = $_FILES['imagename']['name'];

                $this->load->library('upload',$config);
                $this->upload->initialize($config);

                if($this->upload->do_upload('imagename'))
                {
                    $uploadData = $this->upload->data();
                    $imagename = $uploadData['file_name'];
                }
                else
                {
                    echo "not upload";
                }
        }
        else
        {
            echo "error";
        }
        $this->load->view('uploadimage');
    }

    public function uploadimageerror()
    {
        if(!empty($_FILES['imagename']['name']))
        {
                $config['upload_path'] = 'uploadss/';
                $config['allowed_types'] = 'jpg|jpeg|png|gif';
                $config['file_name'] = $_FILES['imagename']['name'];

                $this->load->library('upload',$config);
                $this->upload->initialize($config);

                if($this->upload->do_upload('imagename'))
                {
                    $uploadData = $this->upload->data();
                    $imagename = $uploadData['file_name'];
                }
                else
                {
                    echo "not upload";
                }
        }
        else
        {
            echo "error";
        }
    }

    public function deletedata($id)
    {
        $dele = $this->Loginmodel->delete($id);
        return redirect('Login/index');
    }
}
?>