Skip to content

Instantly share code, notes, and snippets.

@richsage
Created February 9, 2011 15:26
Show Gist options
  • Save richsage/818641 to your computer and use it in GitHub Desktop.
Save richsage/818641 to your computer and use it in GitHub Desktop.
BaseForm.class.php
<?php
/**
* Base project form.
*
* @package sbtickets
* @subpackage form
* @author Your name here
* @version SVN: $Id: BaseForm.class.php 20147 2009-07-13 11:46:57Z FabianLange $
*/
class BaseForm extends sfFormSymfony
{
/**
* Form constructor method.
* We override so that we can apply required '*'s to labels if required.
*
* @param $object
* @param array $options
* @param string $CSRFSecret
*/
public function __construct($options = array(), $CSRFSecret = null)
{
parent::__construct( $options, $CSRFSecret);
// Tell the widget schema which fields are required
$this->widgetSchema->addOption('required_fields', $this->getRequiredFields());
$this->widgetSchema->addFormFormatter('table', new RequiredLabelsFormatterTable($this->widgetSchema));
}
/**
* Returns an array of fields marked as required
*
* @param sfValidatorSchema $validatorSchema
* @param string $format
* @return array $fields
*/
protected function getRequiredFields(sfValidatorSchema $validatorSchema = null, $format = null)
{
if (is_null($validatorSchema))
{
$validatorSchema = $this->validatorSchema;
}
if (is_null($format))
{
$format = $this->widgetSchema->getNameFormat();
}
$fields = array();
foreach ($validatorSchema->getFields() as $name => $validator)
{
$field = sprintf($format, $name);
if ($validator instanceof sfValidatorSchema)
{
// recurse down
$fields = array_merge($fields, $this->getRequiredFields($validator, $field.'[%s]'));
}
else if ($validator->getOption('required'))
{
// this field is required
$fields[] = $field;
}
}
return $fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment