Skip to content

Commit

Permalink
[Form] Implemented UrlField
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Oct 22, 2010
1 parent eaef939 commit 733290c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Symfony/Component/Form/UrlField.php
@@ -0,0 +1,42 @@
<?php

namespace Symfony\Component\Form;

/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Field for entering URLs
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class UrlField extends InputField
{
/**
* {@inheritDoc}
*/
protected function configure()
{
$this->addOption('default_protocol', 'http');
}

/**
* {@inheritDoc}
*/
protected function processData($data)
{
$protocol = $this->getOption('default_protocol');

if ($protocol && $data && !preg_match('~^\w+://~', $data)) {
$data = $protocol . '://' . $data;
}

return $data;
}
}
56 changes: 56 additions & 0 deletions tests/Symfony/Tests/Component/Form/UrlFieldTest.php
@@ -0,0 +1,56 @@
<?php

namespace Symfony\Tests\Component\Form;

require_once __DIR__ . '/LocalizedTestCase.php';

use Symfony\Component\Form\UrlField;

class UrlFieldTest extends LocalizedTestCase
{
public function testBindAddsDefaultProtocolIfNoneIsIncluded()
{
$field = new UrlField('name');

$field->bind('www.domain.com');

$this->assertSame('http://www.domain.com', $field->getData());
$this->assertSame('http://www.domain.com', $field->getDisplayedData());
}

public function testBindAddsNoDefaultProtocolIfAlreadyIncluded()
{
$field = new UrlField('name', array(
'default_protocol' => 'http',
));

$field->bind('ftp://www.domain.com');

$this->assertSame('ftp://www.domain.com', $field->getData());
$this->assertSame('ftp://www.domain.com', $field->getDisplayedData());
}

public function testBindAddsNoDefaultProtocolIfEmpty()
{
$field = new UrlField('name', array(
'default_protocol' => 'http',
));

$field->bind('');

$this->assertSame(null, $field->getData());
$this->assertSame('', $field->getDisplayedData());
}

public function testBindAddsNoDefaultProtocolIfSetToNull()
{
$field = new UrlField('name', array(
'default_protocol' => null,
));

$field->bind('www.domain.com');

$this->assertSame('www.domain.com', $field->getData());
$this->assertSame('www.domain.com', $field->getDisplayedData());
}
}

0 comments on commit 733290c

Please sign in to comment.