Welcome Guest, Not a member yet? Register   Sign In
custom validation rule for IP in form
#1

I am very new to codeigniter(have over 10 years php procedural experiance) and seems like i have run into issues from start. I am creating a auth for my site and in the sign up for i want to check IP not only if its valid but against a custom validation rule i made. I get the IP in the controller via "request->getIPAddress()".

Is there a way i can add the IP in the post method so i can use validation rules for it like they are used for fields that are passed from the form in my view?
Reply
#2

Of course you can.

You can add it as a hidden input field in your form and do your validation.

Code:
<input type="hidden" name="user_ip" value="<?php echo $request->getIPAddress(); ?>">
Reply
#3

(12-12-2020, 11:49 PM)sammyskills Wrote: Of course you can.

You can add it as a hidden input field in your form and do your validation.

Code:
<input type="hidden" name="user_ip" value="<?php echo $request->getIPAddress(); ?>">
obviously that wont work as users will be able to edit it before posting the form
Reply
#4

There is a validation rule for valid_ip already no need for a custom one.

You just need to get it like above then run it through the validation.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(12-13-2020, 05:30 AM)InsiteFX Wrote: There is a validation rule for valid_ip already no need for a custom one.

You just need to get it like above then run it through the validation.
adding it with hidden field wont work as users can edit it via inspect on browser before sending the form, like i said i don't need this to use the validate_ip rule, i need it to use a rule which i wrote and its not for ip validation. So far i read the user manual few times googled whole day and didn't find how, this forum and users are my last chance Smile but seems slim as everyone suggest adding hidden field which is the worst practice ever
Reply
#6

Either i gave very bad explanation of what i need or this can't be done in codeigniter(hope not, that will mean i wasted a week).
What i need is to use standard codeigniter validation for items posted thru form(username, email, password..), but to use it on item that i get in controller(in my case ip). Its a very basic need for a website.
Reply
#7

Have you tried setGlobal? http://codeigniter.com/user_guide/incomi...#setGlobal
I would also try to add the value directly in the $_POST array.
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#8

(12-13-2020, 09:07 AM)includebeer Wrote: Have you tried setGlobal? http://codeigniter.com/user_guide/incomi...#setGlobal
I would also try to add the value directly in the $_POST array.
setGlobal works if all other fields are also added but i was not sure how secure is doing that for Sign Up authentication.
Adding $_POST['ip'] = request->getIPAddress(); does not work.
Reply
#9

How about the check() function? http://codeigniter.com/user_guide/librar...te-1-value

PHP Code:
$validation->check($value'required'); 

Maybe you can call your validation with something like this:
PHP Code:
if ( ! $validation->check($request->getIPAddress(), 'yourIpValidationRule')) {
    // Throw exception...

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#10

(12-13-2020, 04:18 PM)includebeer Wrote: How about the check() function? http://codeigniter.com/user_guide/librar...te-1-value

PHP Code:
$validation->check($value'required'); 

Maybe you can call your validation with something like this:
PHP Code:
if ( ! $validation->check($request->getIPAddress(), 'yourIpValidationRule')) {
    // Throw exception...

yes!! amazing!! 
went thru this guide 3-4 times last 48 hours and never give much thought on this.
Thank you, you literally saved my week.[url=https://www.spellchecker.net/amazing][/url]
Reply




Theme © iAndrew 2016 - Forum software by © MyBB