Welcome Guest, Not a member yet? Register   Sign In
validation problems
#1

[eluser]feri_soft[/eluser]
I have two validation issues. First one i have a login system and i wanted to create my callback for the validation to see if the following data is correct and the users logs in

So here is something:
Code:
function login()
    {
        $this->validation->set_message('check_login', 'sdfdfsdf');
        $rules['username'] = "trim|required|callback_check_login|xss_clean";
        $rules['password'] = "trim|required|xss_clean";
        $rules['remember'] = "numeric";
        $this->validation->set_rules($rules);
        
        $fields['username'] = 'asda';
        $fields['password'] = 'sdasd';
        $fields['remember'] = 'asdasd';
        $this->validation->set_fields($fields);
        
        if ($this->validation->run() == false)
        {
            $this->load->view('login');
        }else{
            $this->load->view('profile');
        }
    }

And i have created a simple callback to check the login:
Code:
function _check_login($username, $password, $remember){
        if($this->user_model->login($username, $password, $remember)){
            return true;
        }else{
            return false;
        }
    }
Obviously this doesnt work as the validation requires only one parameter to work. I could create something else like checking the login in the else clause and if error occurs display the login form with an array data containing something like: '1' => 'No such user', '2' => 'Wrong pass' etc...but i thing this is kind of a double work first create erros array in validation and then create 2nd errors array for login check. Makes no sense. Isnt there a way working with the Ci itself with no other modifications to make my idea working and use callback with multiple attributes. Like the matches and min_lenght / max_lenght funcs.

And i have another very simillar problem tried to create telephone check to see if a user has entered a valid phone or valid GSM and if none of the two is entered display error. So i wrote something like:

Code:
function _phoneorgsm(){
        if(!empty($_POST['phone']) && is_numeric($_POST['phone'])){
            return true;
        }elseif(!empty($_POST['gsm']) && is_numeric($_POST['gsm'])){
            return true;
        }else{
            return false;
        }
    }


but for some reasons it doesnt work just displays me the page with all other errors and not this one. I suppose the same will happen if i try this on the login so i didnt. Here is the function:
Code:
$this->validation->set_message('phoneorgsm', 'asdasdasdasd');
    $rules['phone'] = "trim|callback_phoneorgsm";
        $rules['gsm'] = "trim|callback_phoneorgsm";
etc....

Any ideas i need something very very urgently because i am on a hurry with this one. Thanks a lot !
#2

[eluser]Michael Wales[/eluser]
First the first - the callback only accepts one parameter (the field it's used on, but all submitted values are available via $this->validation, for example:

Code:
function _check_login($username) {
  $password = $this->validation->password;
  $email = $this->validation->email;
}

I'm not quite understanding your second issue, but I think it is because you have the set_message() method in the wrong location - try this:

Code:
function _phoneorgsm(){
  if(!empty($_POST['phone']) && is_numeric($_POST['phone'])) {
    return true;
  } elseif(!empty($_POST['gsm']) && is_numeric($_POST['gsm'])) {
    return true;
  }else{
    $this->validation->set_message('phoneorgsm', 'asdasdasdasd');
    return false;
  }
}
#3

[eluser]feri_soft[/eluser]
Well thanks answearing my first one. I have not tested it yet but i am sure it will work or at least hope so Big Grin
And to the second answearing the first fixes the second but however the problem is not with the message as i have another callbacks and it works great on them the same way. It is something with the posts i think but cant figure out what exactly but i will replace them with the validation values of the fields. I suppose those values are already with applied filters from the rules such as xss, trim and so on right?
#4

[eluser]Michael Wales[/eluser]
You can use the input or validation class or the $_POST variable - all will return the same thing (with CSS, trim, etc all applied).

Code:
$_POST['var'];
$this->input->post('var');
$this->validation->var;
#5

[eluser]feri_soft[/eluser]
Well you see my code i used ordinary post and nothing happens... just doesnt applies field names and so on are fine...
#6

[eluser]Michael Wales[/eluser]
All you are doing is xss_cleaning and trimming those variables - you probably wouldn't notice much of a difference. Run the following code, proof that the three are identical, after a form has been submitted. Just enter a string like ' test ' into the field and watch the spaces magically go away.

Controller: test.php
Code:
class Test extends Controller {

  function index() {
    $this->load->library('validation');
    $rules['string'] = 'required|trim|xss_clean';
    $this->validation->set_rules($rules);
    $fields['string'] = 'string';
    $this->validation->set_fields($fields);

    if ($this->validation->run()) {
      $data['p_string'] = $_POST['string'];
      $data['i_string'] = $this->input->post('string');
      $data['v_string'] = $this->validation->string;
    }
    $this->load->view('test');
  }
}

View: test.php
Code:
<p>There should be no spaces after this colon:&lt;?= $p_string; ?&gt;<br />
There should be no spaces after this colon:&lt;?= $i_string; ?&gt;<br />
There should be no spaces after this colon:&lt;?= $v_string; ?&gt;</p>

&lt;?= form_open('test'); ?&gt;
String: &lt;input type="text" name="string" id="string" /&gt;<br />
&lt;input type="submit" value="Submit" /&gt;
&lt;?= form_close(); ?&gt;
#7

[eluser]feri_soft[/eluser]
Ok so they will always be cleared in the 3 ways no matter of the validation right...that raises a bit of a problem if i dont want them trimmed but however thats not the issue the problem is that the function which looks ideal just doesnt work. I putted instead of the callback a simple required check and its fine so i suppose its with the callback but what exactly...i tried all 3 ways and nothing happens... Thanks!
#8

[eluser]feri_soft[/eluser]
I am getting mad at this. Changed it a little bit to have a single parameter like so:

Code:
function _phone_gsm($str){
        if(!empty($str) && is_numeric($str)){
            return true;
        }elseif(!empty($_POST['gsm']) && is_numeric($_POST['gsm'])){
            return true;
        }else{
            return false;
        }
    }
and applied to phone rules and nothing happens. How can something as easy as that be a problem !?!?!?!?!? Cant you use posts in your callbacks, what unusefull validation would that be!? Please help with this one.

and btw i tried with $this->validation->gsm as well as $this->input->post('asdas')
and guess whats happening .... NOTHING
#9

[eluser]feri_soft[/eluser]
I found the problem you cant use private functions with _asdasd it should be a normal function but i dont want anyone to have access to it...how to fix that, thanks!
#10

[eluser]Michael Wales[/eluser]
See my reply to your other post.




Theme © iAndrew 2016 - Forum software by © MyBB