/lib/form/sfForm.class.php
PHP | 1339 lines | 650 code | 169 blank | 520 comment | 55 complexity | 8c12fad44f6c2649c1ba65b56bc40479 MD5 | raw file
- <?php
- /*
- * This file is part of the symfony package.
- * (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.
- */
- /**
- * sfForm represents a form.
- *
- * A form is composed of a validator schema and a widget form schema.
- *
- * sfForm also takes care of CSRF protection by default.
- *
- * A CSRF secret can be any random string. If set to false, it disables the
- * CSRF protection, and if set to null, it forces the form to use the global
- * CSRF secret. If the global CSRF secret is also null, then a random one
- * is generated on the fly.
- *
- * @package symfony
- * @subpackage form
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id$
- */
- class sfForm implements ArrayAccess, Iterator, Countable
- {
- protected static
- $CSRFSecret = false,
- $CSRFFieldName = '_csrf_token',
- $toStringException = null;
- protected
- $widgetSchema = null,
- $validatorSchema = null,
- $errorSchema = null,
- $formFieldSchema = null,
- $formFields = array(),
- $isBound = false,
- $taintedValues = array(),
- $taintedFiles = array(),
- $values = null,
- $defaults = array(),
- $fieldNames = array(),
- $options = array(),
- $count = 0,
- $localCSRFSecret = null,
- $embeddedForms = array();
- /**
- * Constructor.
- *
- * @param array $defaults An array of field default values
- * @param array $options An array of options
- * @param string $CSRFSecret A CSRF secret
- */
- public function __construct($defaults = array(), $options = array(), $CSRFSecret = null)
- {
- $this->setDefaults($defaults);
- $this->options = $options;
- $this->localCSRFSecret = $CSRFSecret;
- $this->validatorSchema = new sfValidatorSchema();
- $this->widgetSchema = new sfWidgetFormSchema();
- $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
- $this->setup();
- $this->configure();
- $this->addCSRFProtection($this->localCSRFSecret);
- $this->resetFormFields();
- }
- /**
- * Returns a string representation of the form.
- *
- * @return string A string representation of the form
- *
- * @see render()
- */
- public function __toString()
- {
- try
- {
- return $this->render();
- }
- catch (Exception $e)
- {
- self::setToStringException($e);
- // we return a simple Exception message in case the form framework is used out of symfony.
- return 'Exception: '.$e->getMessage();
- }
- }
- /**
- * Configures the current form.
- */
- public function configure()
- {
- }
- /**
- * Setups the current form.
- *
- * This method is overridden by generator.
- *
- * If you want to do something at initialization, you have to override the configure() method.
- *
- * @see configure()
- */
- public function setup()
- {
- }
- /**
- * Renders the widget schema associated with this form.
- *
- * @param array $attributes An array of HTML attributes
- *
- * @return string The rendered widget schema
- */
- public function render($attributes = array())
- {
- return $this->getFormFieldSchema()->render($attributes);
- }
- /**
- * Renders the widget schema using a specific form formatter
- *
- * @param string $formatterName The form formatter name
- * @param array $attributes An array of HTML attributes
- *
- * @return string The rendered widget schema
- */
- public function renderUsing($formatterName, $attributes = array())
- {
- $currentFormatterName = $this->widgetSchema->getFormFormatterName();
- $this->widgetSchema->setFormFormatterName($formatterName);
- $output = $this->render($attributes);
- $this->widgetSchema->setFormFormatterName($currentFormatterName);
- return $output;
- }
- /**
- * Renders hidden form fields.
- *
- * @param boolean $recursive False will prevent hidden fields from embedded forms from rendering
- *
- * @return string
- *
- * @see sfFormFieldSchema
- */
- public function renderHiddenFields($recursive = true)
- {
- return $this->getFormFieldSchema()->renderHiddenFields($recursive);
- }
- /**
- * Renders global errors associated with this form.
- *
- * @return string The rendered global errors
- */
- public function renderGlobalErrors()
- {
- return $this->widgetSchema->getFormFormatter()->formatErrorsForRow($this->getGlobalErrors());
- }
- /**
- * Returns true if the form has some global errors.
- *
- * @return Boolean true if the form has some global errors, false otherwise
- */
- public function hasGlobalErrors()
- {
- return (Boolean) count($this->getGlobalErrors());
- }
- /**
- * Gets the global errors associated with the form.
- *
- * @return array An array of global errors
- */
- public function getGlobalErrors()
- {
- return $this->widgetSchema->getGlobalErrors($this->getErrorSchema());
- }
- /**
- * Binds the form with input values.
- *
- * It triggers the validator schema validation.
- *
- * @param array $taintedValues An array of input values
- * @param array $taintedFiles An array of uploaded files (in the $_FILES or $_GET format)
- */
- public function bind(array $taintedValues = null, array $taintedFiles = null)
- {
- $this->taintedValues = $taintedValues;
- $this->taintedFiles = $taintedFiles;
- $this->isBound = true;
- $this->resetFormFields();
- if (null === $this->taintedValues)
- {
- $this->taintedValues = array();
- }
- if (null === $this->taintedFiles)
- {
- if ($this->isMultipart())
- {
- throw new InvalidArgumentException('This form is multipart, which means you need to supply a files array as the bind() method second argument.');
- }
- $this->taintedFiles = array();
- }
- try
- {
- $this->doBind(self::deepArrayUnion($this->taintedValues, self::convertFileInformation($this->taintedFiles)));
- $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
- // remove CSRF token
- unset($this->values[self::$CSRFFieldName]);
- }
- catch (sfValidatorErrorSchema $e)
- {
- $this->values = array();
- $this->errorSchema = $e;
- }
- }
- /**
- * Cleans and binds values to the current form.
- *
- * @param array $values A merged array of values and files
- */
- protected function doBind(array $values)
- {
- $this->values = $this->validatorSchema->clean($values);
-