2010-06-24 09:40:05 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Form;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
|
|
|
/*
|
2010-10-02 11:38:11 +01:00
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
2010-06-24 09:40:05 +01:00
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
2010-10-02 11:38:11 +01:00
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
2010-06-24 09:40:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A field for repeated input of values
|
|
|
|
*
|
|
|
|
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class RepeatedField extends FieldGroup
|
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* The prototype for the inner fields
|
|
|
|
* @var FieldInterface
|
|
|
|
*/
|
|
|
|
protected $prototype;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Repeats the given field twice to verify the user's input
|
|
|
|
*
|
|
|
|
* @param FieldInterface $innerField
|
|
|
|
*/
|
|
|
|
public function __construct(FieldInterface $innerField, array $options = array())
|
|
|
|
{
|
|
|
|
$this->prototype = $innerField;
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
parent::__construct($innerField->getKey(), $options);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$field = clone $this->prototype;
|
|
|
|
$field->setKey('first');
|
|
|
|
$field->setPropertyPath('first');
|
|
|
|
$this->add($field);
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
$field = clone $this->prototype;
|
|
|
|
$field->setKey('second');
|
|
|
|
$field->setPropertyPath('second');
|
|
|
|
$this->add($field);
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Returns whether both entered values are equal
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isFirstEqualToSecond()
|
|
|
|
{
|
|
|
|
return $this->get('first')->getData() === $this->get('second')->getData();
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Sets the values of both fields to this value
|
|
|
|
*
|
|
|
|
* @param mixed $data
|
|
|
|
*/
|
|
|
|
public function setData($data)
|
2010-06-24 09:40:05 +01:00
|
|
|
{
|
2010-06-24 10:24:08 +01:00
|
|
|
parent::setData(array('first' => $data, 'second' => $data));
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|
|
|
|
|
2010-06-24 10:24:08 +01:00
|
|
|
/**
|
|
|
|
* Return only value of first password field.
|
|
|
|
*
|
|
|
|
* @return string The password.
|
|
|
|
*/
|
|
|
|
public function getData()
|
|
|
|
{
|
|
|
|
if ($this->isBound() && $this->isFirstEqualToSecond()) {
|
|
|
|
return $this->get('first')->getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2010-06-24 09:40:05 +01:00
|
|
|
}
|