I'm building an application in Codeigniter and tried to use valid_url
from Form validation library to check if a domain is valid (e.g. "hæi.no"). Since Norwegian characters aren't allowed, this did not work. What would be the best work around to check for valid URLs and allow Norwegian characters, such as "ÆØÅ", "æøå"?
Asked
Active
Viewed 260 times
0

kanarifugl
- 9,887
- 5
- 29
- 39
-
I may be wrong, but I'm not sure those characters are valid in a URL, look [here](http://stackoverflow.com/questions/1547899/which-characters-make-a-url-invalid) – Roshan Bhumbra Nov 26 '16 at 18:31
-
@RoshanBhumbra Many domains are IDN compatible including .no which is the important part here. Therefore, I can't use native PHP validation such as FILTER_VALIDATE_URL for example. – kanarifugl Nov 26 '16 at 18:35
-
Did my answer help out? – Roshan Bhumbra Nov 26 '16 at 20:47
1 Answers
0
You can create your own form validation library which I personally think is more elegant than using callbacks.
In your controller
$this->load->library(array('form_validation', 'My_form_validation'));
This ensures both of the libraries are loaded, you need them both as MY_Form_validation
extends CI_Form_validation
/application/libraries/My_form_validation.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation
{
/**
* Reference to the CodeIgniter instance
*
* @var object
*/
protected $CI;
function __construct($config = array())
{
$this->CI =& get_instance();
parent::__construct($config);
}
function series_exists($series_id)
{
$this->CI->db
->select('COUNT(`id`)')
->where('id', $series_id);
$rows = $this->CI->db->get('series')->result()[0];
return $rows == 1;
}
}
You can see the series_exists
function in this example, this would be called like a normal validation rule as it only takes one parameter. You would replace this with your own function which must have a unique name, such as my_valid_url
.
Add the function name to your validation rules, in my example it would look something like this
'required|integer|series_exists'

Roshan Bhumbra
- 540
- 1
- 8
- 17