[Form] Implemented UrlField

This commit is contained in:
Bernhard Schussek 2010-10-19 15:20:55 +02:00 committed by Fabien Potencier
parent eaef939141
commit 733290c112
2 changed files with 98 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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());
}
}