-2

I am new to codeigniter I have been trying to do form validation for registration page but my validation fails always no matter what i input,please help me solve this Here's my view:

<div class="signup-form"><!--sign up form-->
<h2>New User Signup!</h2>
<?php echo form_open('login/register');?>
    <input type="text" name="name" placeholder="Name"/>
    <input type="email" name="email" placeholder="Email Address"/>
    <input type="password" name="password" placeholder="Password"/>
    <button type="submit" class="btn btn-default">Signup</button>
</form>
</div><!--/sign up form-->

Here's my Conroller:

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

class Login extends CI_Controller{

    public function register(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('email','required|valid_email');
        $this->form_validation->set_rules('password','required');
        if($this->form_validation->run() === FALSE){
            $this->load->view('test.php');
        }
        else{
            $this->load->view('index.php');
        }
    }
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
jerrytom
  • 17
  • 2
  • because of this `===` – Linus Oct 31 '15 at 21:40
  • In rules setting, you are passing two arguments only. It is required to pass 3 arguments there (or 4 as optional one). [Docs](https://codeigniter.com/userguide3/libraries/form_validation.html#setting-validation-rules). – Tpojka Nov 01 '15 at 01:08
  • In form validation run area Instead of `===` this should be `==` –  Nov 01 '15 at 04:46
  • Not quite. Check Form_validation class run() Methos returns bool exclusivelly, so `===` is very valid. – Tpojka Nov 01 '15 at 12:58

1 Answers1

-1

Main Reason For this problem is due to fact this

Solution by @Tpojka

Small change in your code

public function register(){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','required|valid_email');
    $this->form_validation->set_rules('password','required');
    if($this->form_validation->run() == FALSE){  //see here change 
        $this->load->view('test.php');
    }
    else{
        $this->load->view('index.php');
    }
}

== compares the values of variables for equality, type casting as necessary. === checks if the two variables are of the same type AND have the same value.

$this->form_validation->run() Runs the validation routines. Returns boolean TRUE on success and FALSE on failure.

Reference

http://au.php.net/manual/en/language.operators.comparison.php

Read this for Details //important read this

Community
  • 1
  • 1
Linus
  • 899
  • 3
  • 19
  • 33
  • Not quite right. If you check `run()` method you'll see that return value is `(bool)` so comparison operator for checking if left/right side values are identical, is very appropriate in this case. – Tpojka Oct 31 '15 at 22:35
  • @Tpojka you mean to say === is right to use here – Linus Oct 31 '15 at 23:04
  • @Tpojka what you are trying to say – Linus Oct 31 '15 at 23:20
  • may be you are right! – Linus Nov 01 '15 at 00:28
  • Yes, I am saying that `$this->form_validation->run()` method returns only bool value (i.e. `TRUE OR FALSE`). – Tpojka Nov 01 '15 at 01:05
  • @Tpojka so what can be reason for above problem OP still not clarified it – Linus Nov 01 '15 at 01:08
  • @Tpojka i am talking about user post(question) why validation always fails? – Linus Nov 01 '15 at 01:11
  • That was the not main problem.I figured out in set rules method i was not giving the optional parameter for title(second parameter),I should give atleast an empty string else "required" will be considered as second parameter – jerrytom Nov 01 '15 at 06:00
  • @Jerrytom yeah that's my mistake any way to solved it :) – Linus Nov 01 '15 at 06:02
  • @jerrytom I pointed that already. Empty string is still a parameter. `===` is not an issue. – Tpojka Nov 01 '15 at 12:57