Welcome Guest, Not a member yet? Register   Sign In
form_validation and regex_match?
#1

[eluser]JackU[/eluser]
I´m using the form_validation library to validate my $_GET

I could do this:
$this->form_validation->set_rules("dir","directory","min_length[2]|regex_match[/^(\\\[a-zA-Z0-9_]+)+$/]");

But i can´t do this:
$this->form_validation->set_rules("answer","answer","min_length[2]|regex_match[/^(yes|no)$/i]");

Why?

It seems that it won´t even get sent the regex_match function in form_validation. Why is that?

#2

[eluser]thebigpj[/eluser]
Some Advice:

Code:
$this->form_validation->set_rules(“dir”,“directory”,“min_length[2]|regex_match[/^(\\\[a-zA-Z0-9_]+)+$/]”);

could just be:

Code:
$this->form_validation->set_rules(“dir”,“directory”,“min_length[2]|alpha_numeric”);

http://ellislab.com/codeigniter/user-gui...ereference

Quote:alpha_numberic - "Returns FALSE if the form element contains anything other than alpha-numeric characters."


Alternative to the above / Your Problem:

Code:
$this->form_validation->set_rules(“dir”,“directory”,“min_length[2]|callback_directory_check”);

Then in your controll have a function called 'checkDir' e.g.

Code:
public function directory_check($str) { if('yes'==$str) etc...}

http://ellislab.com/codeigniter/user-gui...#callbacks

The 'callback_directory_check' tells the controller to use the 'directory_check' method to see if this matches up. Just remember to return either TRUE or FALSE.


*Sorry I am new to these forums. I hope this reply helps.
#3

[eluser]JackU[/eluser]
Thank you for your quick answer!

Quote:could just be:
$this->form_validation->set_rules(“dir”,“directory”,“min_length[2]|alpha_numeric”);

No. Because my regex contains that it have to start with a backslash (\test).

Quote:Alternative to the above / Your Problem:
$this->form_validation->set_rules(“dir”,“directory”,“min_length[2]|callback_directory_check”)

Yes, i tried that earlier. And it works. But it really seems "bad practice" (correct me if i´m wrong) to put stuff like that in a controller. Also that function could be accessed via a URL like this: http://someserver.com/index.php/controll...tory_check.

I tried to change my callback to private....but then i got a huge error.
#4

[eluser]thebigpj[/eluser]
It needs to be public so that the form_validation can access it.

Try using an _ as the first character of your function (e.g. '_directory_check')and call it via 'callback__directory_check'.

This will make it unaccessible via the url.

I don't see where else this function would go. Possible the model but that is for in/out database functions. The next place is the library itself but when upgrading your library to the latest version you will need to remember this function is required to be re-added.




Theme © iAndrew 2016 - Forum software by © MyBB