Skip to content

Instantly share code, notes, and snippets.

@khepin
Created March 5, 2011 04:12
Show Gist options
  • Save khepin/856102 to your computer and use it in GitHub Desktop.
Save khepin/856102 to your computer and use it in GitHub Desktop.
<?php
public function configure()
{
// not really useful, but just in case!
parent::configure();
$this->postConfigure();
}
public function postConfigure()
{
// get required fields from the config file
$formConfig = sfConfig::get('app_form_fields');
// Be sure to use this and not $this->formFieldSchema as it might not be
// initiated yet.
$formFieldSchema = $this->getFormFieldSchema();
// For some reason I have to use the BaseForm sometimes which has no name,
// this avoids breaking everything in such cases.
if($this->getName() == '')
{
return;
}
if(isset($formConfig[$this->getName()]))
{
$requiredFields = $formConfig[$this->getName()];
}
// get out of here if there is no configuration for this form (the list of required fields is empty)
if(!isset($requiredFields))
{
return;
}
foreach($this->widgetSchema->getFields() as $fieldname => $widget)
{
// if the config file requires this field, we set the option accordingly
if(in_array($fieldname, $requiredFields))
{
$this->validatorSchema[$fieldname]->setOption('required', true);
}
// If the field is required we add a class to the label so it displays in the browser
if(!is_null($this->validatorSchema[$fieldname])
&& $this->validatorSchema[$fieldname]->getOption('required') == true)
{
$this->widgetSchema[$fieldname]->setLabel(
$formFieldSchema[$fieldname]->
renderLabel(null, array('class' => 'required_form_field'))
);
}
$this->formFieldSchema = $formFieldSchema;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment