Provides SilverStripe with a DBField and FormField for handling external URLs.

Installs: 295

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 9


3.1.0 2024-03-13 03:17 UTC

This package is auto-updated.

Last update: 2024-05-13 03:49:32 UTC


Build Status Scrutinizer Code Quality Code Coverage

Provides a DBField and FormField for handling external URLs.

Validate and tidy urls as they are captured from users. Configuration is highly flexible. Makes use of php's parse_url and http_build_url to do the actual work.


Note - this is forked from sunnysideup/silverstripe-externalurlfield and updated into new composer vendor namespace; making composer installs easier in client projects.

composer require sunnysideup/silverstripe-externalurlfield "*@stable"


Makes use of the http_build_url function from the PECL pecl_http library. However the module's composer requirements include a PHP fallback/shim/polyfill. The composer replacement does check for the presence of http_build_url.

  • SilverStripe ^4 || ^5

DataObject / Template Usage

Handled by ExternalURL class (Varchar).

use SilverStripe\ORM\DataObject;

class MyDataObject extends DataObject
    private static $db = array(
        'Website' => 'ExternalURL(768)', // set a max length so that we can index it - if you do not need to index it then you may not need to add this.
<% with $MyDataObject %>
    <p>Website: $Website</p>
    <p>Website Nice: $Website.Nice</p>
    <p>Website Domain: $Website.Domain</p>
    <p>Website Domain No WWW: $Website.Domain.NoWWW</p>
<% end_with %>

Given the url, the above produces:

Website Nice:
Website Domain:
Website Domain No WWW:

Form Usage

Handled by ExternalURLField (FormField).

Validation is handled by the html5 pattern attribute, and also server side by a more robust regular expression. The field uses the html5 type="url" attribute.

You can configure various parts of the url to be stripped out, or populated with defaults when missing.

use Sunnysideup\ExternalURLField\ExternalURLField;

$websitefield = new ExternalURLField('Website');

//set options (with defaults shown)
    //these will be added, if missing
    'defaultparts' => array(
        'scheme' => 'http'
    //these parts are removed from saved urls
    'removeparts' => array(
        'scheme' => false,
        'user' => true,
        'pass' => true,
        'host' => false,
        'port' => false,
        'path' => false,
        'query' => false,
        'fragment' => false
    'html5validation' => true

//say you want to store nice tidy facebook urls
    'query' => true,
    'fragment' => 'true',
    'port' => 'true'
//a urls like
//would become

HTML5 validation

Enabled by default, the html5 validation sets the field type atribute to url, and adds a pattern attribute which is set to https?://.+.

Disable using the html5validation config:

$field->setConfig("html5validation", false);

Disabling html5 validation is particularly useful if you want to allow users to enter urls that have no scheme/protocol e.g: instead of