CodeIgniter Forums

Full Version: Need help using valid_url rule reference
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey Guys,

I am trying to use the valid_url rule in CodeIgniter and it doesn't seem to be working.

When I submit the form by just typing it "test" it doesn't display any kind of validation error. I'm not sure what is wrong here.

If anyone can help me out I would really appreciate it. Thanks!




PHP Code:
private  function validateForm(){


 
   $this->form_validation->set_rules("proj_url","project URL""required|valid_url",[
 
       "required" => "Please enter a project url",
 
       "valid_url" => "Please enter the %s in the correct url format (www.myproject.com)"
 
   ]);




Is the required rule working? I ask because that code just sets up the rules. To send the submitted data to validation, you have to use.
PHP Code:
if ($this->form_validation->run())
{
 
   // form data validated ok
}
else
{
 
   // form data failed validation

Yes the required rule is working.I had to change the core function of valid_url... for some reason it wasn't working properly
It is working properly, 'test' is a valid hostname and therefore a valid URL too, no matter how strange that may seem at first.
@eldan88 what do you mean it wasn't working properly? "test" IS a valid url. If you want, after validation you can at any time do a prep_url() on the value.
I had the same issue.. I would post "224regqawergwgw" without any domain identifier and still return true for valid url...



My solution was to create a callback function and check the string with a filter.


PHP Code:
$this->form_validation->set_rules('add_website''website url''trim|required|callback_check_valid_url'); 



PHP Code:
public function check_valid_url$param )
{
 
 if( ! filter_var($paramFILTER_VALIDATE_URL) ){
 
   $this->form_validation->set_message('check_valid_url''The {field} must be a valid url');
 
   return FALSE;
 
 }else{
 
   return TRUE;
 
 }