CodeIgniter Forums
custom validation rule for IP in form - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: custom validation rule for IP in form (/showthread.php?tid=78197)

Pages: 1 2


custom validation rule for IP in form - xPooky - 12-12-2020

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?


RE: custom validation rule for IP in form - sammyskills - 12-12-2020

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(); ?>">



RE: custom validation rule for IP in form - xPooky - 12-13-2020

(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


RE: custom validation rule for IP in form - InsiteFX - 12-13-2020

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.


RE: custom validation rule for IP in form - xPooky - 12-13-2020

(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


RE: custom validation rule for IP in form - xPooky - 12-13-2020

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.


RE: custom validation rule for IP in form - includebeer - 12-13-2020

Have you tried setGlobal? http://codeigniter.com/user_guide/incoming/request.html?highlight=validation#setGlobal
I would also try to add the value directly in the $_POST array.


RE: custom validation rule for IP in form - xPooky - 12-13-2020

(12-13-2020, 09:07 AM)includebeer Wrote: Have you tried setGlobal? http://codeigniter.com/user_guide/incoming/request.html?highlight=validation#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.


RE: custom validation rule for IP in form - includebeer - 12-13-2020

How about the check() function? http://codeigniter.com/user_guide/libraries/validation.html#validate-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...




RE: custom validation rule for IP in form - xPooky - 12-13-2020

(12-13-2020, 04:18 PM)includebeer Wrote: How about the check() function? http://codeigniter.com/user_guide/libraries/validation.html#validate-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]