Welcome Guest, Not a member yet? Register   Sign In
Form Validation And URL Validation
#1

[eluser]ralfo[/eluser]
Hi all,

I'm really liking codeigniter, love the simplicity and rapid development aspects.

Now, I want to use the form validation class to validate ALL data coming into my application.

This is fine when the data coming in is from a form (of course lol).

However, I want to use it to validate the URL segments too.

Can this be done?

Would be really neat and tidy if it could, otherwise I will be using the form validation class to validate form data, but my own custom classes/functions to validate URL segments (eg domain.com/order/book/345 - test that 345 is within the specified bounds using the form validation class that already exists).

Please advise Smile
#2

[eluser]skunkbad[/eluser]
[quote author="ralfo" date="1251043912"]...Would be really neat and tidy if it could, otherwise I will be using the form validation class to validate form data, but my own custom classes/functions to validate URL segments (eg domain.com/order/book/345 - test that 345 is within the specified bounds using the form validation class that already exists)...[/quote]

If it were me, I'd be checking/validating '345' inside the 'book' method of the 'order' controller. This would be quite easy:

Code:
public function book($book_num)
{
   if(isset($book_num) && is_int($book_num) && $book_num > 0 && $book_num < 346)
   {
      // do something with '1' through '345'
   }
   else
   }
      // show error message indicating that no book was selected
   }
}

You can see that in this case, there really is no need to validate the URL with the form validation class. If you for some reason really do need to validate the URL, you could probably extend the form validation class, having it accept the URI segments as variables. Right now it only accepts post vars, but I don't think it could be that hard to extend.
#3

[eluser]ralfo[/eluser]
Yeah thanks, this is probably what I'll end up doing, cheers Smile
#4

[eluser]ralfo[/eluser]
Just thought about starting this, and a custom class seemed like the way to go to keep it neat and tidy.

Then looked through the form_validation code, and I see that most, if not all, of the things I want to check for are already methods in there.

So I will be extending the form_validation class to accept URL segments methinks Big Grin
#5

[eluser]devbro[/eluser]
I think that is not a good idea and kind of misleading. form_validation is meant for validating things that are coming from form tags in form of $_POST only.

There is a general validation class inside the system/libraries folder which you can take a look at. Also if that is helping then you can use other custom php validation classes from internet.
#6

[eluser]ralfo[/eluser]
[quote author="devbro" date="1251144877"]I think that is not a good idea and kind of misleading. form_validation is meant for validating things that are coming from form tags in form of $_POST only.

There is a general validation class inside the system/libraries folder which you can take a look at. Also if that is helping then you can use other custom php validation classes from internet.[/quote]

Thanks, I'll take a look Smile

Yeah, I went off the idea of extending the form_validation class shortly after I posted my last message.

Was about to create a new class (combining validation and error messaging) but will check out the validation class and see what it's got.

So much under the hood as standard with codeigniter, it's hard to see the wood for the trees sometimes.




Theme © iAndrew 2016 - Forum software by © MyBB