Welcome Guest, Not a member yet? Register   Sign In
How to get fieldname on a form_validation callback function
#11

[eluser]unsub[/eluser]
Thank you so much Smile
And I totally get why now; I didn't make that connection for some reason, ie: the url/method thing.

I appreciate your help on this. Hopefully I can try it out this afternoon.

cheers
-Gabriel
#12

[eluser]unsub[/eluser]
Hi
Alas, it doesn't work... I have no idea what I'm doing wrong at this point.

Here's what I have so far.
setting the rules for the captcha field:
Code:
$this->form_validation->set_rules('captcha','captcha','required|callback__captcha_check[captcha]');
and the callback method:
Code:
function _captcha_check($value,$field){
    $exp=time()-600;
    $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
    $binds = array($this->input->post('captcha'), $this->input->ip_address(), $exp);
    $query = $this->db->query($sql, $binds);
    $row = $query->row();
        if ($row->count == 0){
            $this->form_validation->set_value('_captcha_check[captcha]',"You entered the wrong characters. Try again.");
        return FALSE;
        }else{
        return TRUE;
        }
    }

hmmm... well, at this point I can say, with complete sincerity, "AAAARRRGH!" Sad

hehe
Now, it does catch bad entries, and won't allow a submit unless it's correct. But for some reason which I can't seem to wrap my head around, the 'set_value()' in the callback simply refuses to execute. Flat out refuses. Not even polite about it, either. Just gives me the silent treatment.

Anyone got any pointers? Something that I have misseed?

Cheers, and thanks again for the help.

-gabriel
#13

[eluser]opel[/eluser]
I tried a number of things to and couldnt get it to work. May be worth posting in the bug tracker?
#14

[eluser]lifo101[/eluser]
Maybe something else is happening. First, why are you trying to use the trick I explained above by adding the [captcha] param to the callback? You don't even use the param in the function.

And I think your set_value is wrong, the first parameter should be the variable name: 'captcha' not '_captcha_check[captcha]'. (I made that mistake in my above post too).

Also, are you sure your SQL is running correctly? $row->count must not ever by zero (maybe its null?). You might want to change your logic a little. This is how I normally do it.

Code:
$q = $ci->db->query($sql, $binds);
if ($q->num_rows() == 0) {
    // no rows found
    return false;
}
$r = $q->row();
#15

[eluser]unsub[/eluser]
[quote author="lifo101" date="1249054217"]Maybe something else is happening. First, why are you trying to use the trick I explained above by adding the [captcha] param to the callback? You don't even use the param in the function.

And I think your set_value is wrong, the first parameter should be the variable name: 'captcha' not '_captcha_check[captcha]'. (I made that mistake in my above post too).

Also, are you sure your SQL is running correctly? $row->count must not ever by zero (maybe its null?). You might want to change your logic a little. This is how I normally do it.

Code:
$q = $ci->db->query($sql, $binds);
if ($q->num_rows() == 0) {
    // no rows found
    return false;
}
$r = $q->row();
[/quote]

Hi

Smile see? and that's why refer to myself as an artist and not a developer :lol:

Thanks, I'll give that a try. You know, it was because I couldn't make it work the way I thought it worked that I tried your stuff from above. A silly act of desperation.

My sql is working fine, but I'll switch it anyway.

There is some stuff in here that I basically learned from looking at other code, but I'm getting the feeling that maybe I misunderstood the original stuff, and it's pure fluke that I ever got it to work in the past...

Thanks again Smile
#16

[eluser]unsub[/eluser]
well... same as before, I'm afraid.
I have echo'd out varifications at every stage, and it's all working except for setting the value.
Oh well. I altered the default message about not being able to set the value for that field, and made it the same as the captcha erorr. Stupid, but for this project it will hold me over 'till I figure out what I did wrong.
Here's what I have now:
Code:
function _captcha_check($value=NULL)
    {
        $exp=time()-600;
        $sql = "SELECT * FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($value, $this->input->ip_address(), $exp);
        $query = $this->db->query($sql, $binds);
        
        if ($query->num_rows() == 0)
        {
            $this->form_validation->set_value('captcha',"You entered the wrong characters. Try again.");
            return FALSE;
        }else{
            return TRUE;
        }

    }
#17

[eluser]Unknown[/eluser]
You need to change
$this->form_validation->set_value('captcha',"You entered the wrong characters. Try again.");

to

$this->form_validation->set_message('captcha',"You entered the wrong characters. Try again.");

in order to show your message instead of "Unable to access an error message corresponding to your field name."

Smile

[quote author="unsub" date="1249083250"]well... same as before, I'm afraid.
I have echo'd out varifications at every stage, and it's all working except for setting the value.
Oh well. I altered the default message about not being able to set the value for that field, and made it the same as the captcha erorr. Stupid, but for this project it will hold me over 'till I figure out what I did wrong.
Here's what I have now:
Code:
function _captcha_check($value=NULL)
    {
        $exp=time()-600;
        $sql = "SELECT * FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
        $binds = array($value, $this->input->ip_address(), $exp);
        $query = $this->db->query($sql, $binds);
        
        if ($query->num_rows() == 0)
        {
            $this->form_validation->set_value('captcha',"You entered the wrong characters. Try again.");
            return FALSE;
        }else{
            return TRUE;
        }

    }
[/quote]
#18

[eluser]unsub[/eluser]
Smile
thank you so very much! I'll give that a try. Funnily enough, just last night my ugly hack finally got in the way and I was going to have to revisit this.

cheers
#19

[eluser]unsub[/eluser]
damn. Still doesn't work. I'm really frustrated with this, I can't even begin to figure out the problem. I've tried everything. Sad Can anyone see some blindingly obvious error that I am missing? I feel like there must be something staring me in the face but I just can't see it! It doesn't get more frustrating .




Theme © iAndrew 2016 - Forum software by © MyBB