This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Form/RepeatedField.php

91 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/*
* This file is part of the Symfony package.
2010-10-02 11:38:11 +01:00
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form;
/**
* A field for repeated input of values
*
* @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
*/
class RepeatedField extends FieldGroup
{
/**
* The prototype for the inner fields
* @var FieldInterface
*/
protected $prototype;
/**
* 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;
parent::__construct($innerField->getKey(), $options);
}
/**
* {@inheritDoc}
*/
protected function configure()
{
$field = clone $this->prototype;
$field->setKey('first');
$field->setPropertyPath('first');
$this->add($field);
$field = clone $this->prototype;
$field->setKey('second');
$field->setPropertyPath('second');
$this->add($field);
parent::configure();
}
/**
* Returns whether both entered values are equal
*
* @return bool
*/
public function isFirstEqualToSecond()
{
return $this->get('first')->getData() === $this->get('second')->getData();
}
/**
* Sets the values of both fields to this value
*
* @param mixed $data
*/
public function setData($data)
{
parent::setData(array('first' => $data, 'second' => $data));
}
/**
* 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;
}
}