public static function setup(sfForm $form) { $formatter = new pmWidgetFormSchemaFormatterTable($form); $form->getWidgetSchema()->addFormFormatter("pm_table", $formatter); $form->getWidgetSchema()->setFormFormatterName("pm_table"); $form->unsetFields(); // auto configure widgets pmWidgetFactory::replaceWidgets($form); // auto configure validators pmValidatorFactory::replaceValidators($form); $form->configureWidgets(); $form->configureValidators(); if ($form instanceof pmFormPropel) { $sf_user = sfContext::getInstance()->getUser(); if (method_exists($sf_user, "getGuardUser")) { $user_id = $sf_user->getGuardUser()->getId(); if (array_key_exists("created_by", $form->getWidgetSchema()->getFields()) && $form->getObject()->isNew()) { $form->getObject()->setCreatedBy($user_id); } if (array_key_exists("updated_by", $form->getWidgetSchema()->getFields())) { $form->getObject()->setUpdatedBy($user_id); } } } }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $activeProjectFound = false; $userRoleManager = sfContext::getInstance()->getUserRoleManager(); $projectList = $userRoleManager->getAccessibleEntities('Project'); foreach ($projectList as $project) { if ($project->getIsDeleted() != 0) { $activeProjectFound = true; break; } } $requiredMess = __('Select a project'); if (!$activeProjectFound) { $requiredMess = __("No Projects Defined"); } $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = __(ucwords(str_replace("_", " ", $this->attributes['id']))); $validator = new sfValidatorString(); if ($this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $validator = new sfValidatorString(array('required' => true), array('required' => $requiredMess)); } $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); // $validatorSchema = $form->getValidatorSchema(); $widgetSchema[$this->attributes['id']] = $this; // $widgetSchema[$this->attributes['id']]->setLabel(ucwords(str_replace("_", " ", $this->attributes['id']))); }
/** * Embeds this widget into the form. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = ucwords(str_replace("_", " ", $this->attributes['id'])); $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], new sfValidatorPass()); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); $widgetSchema[$this->attributes['id']] = $this; $widgetSchema[$this->attributes['id']]->setLabel(__(ucwords(str_replace("_", " ", $this->attributes['id'])))); $validatorSchema[$this->attributes['id']] = new ohrmValidatorDateRange(array(), array("invalid" => "Insert a correct date")); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); $widgetSchema[$this->attributes['id']] = $this; $widgetSchema[$this->attributes['id']]->setLabel(ucwords(str_replace("_", " ", $this->attributes['id']))); $required = $requiredMessage = __(ValidationMessages::REQUIRED); $validatorSchema[$this->attributes['id']] = new ohrmValidatorConditionalFilter(array(), array('required' => $requiredMessage)); }
/** * AJAX action to add new invoice items * @param sfWebRequest $request * @return unknown_type */ public function executeAjaxAddInvoiceItem(sfWebRequest $request) { $index = 'new_item_invoice_' . time(); $item = new Item(); $item->common_id = $request->getParameter('invoice_id'); $form = new sfForm(); $form->getWidgetSchema()->setNameFormat('invoice[%s]'); $form->embedForm('Items', new FormsContainer(array($index => new ItemForm($item)), 'ItemForm')); $params = array('invoiceItemForm' => $form['Items'][$index], 'item' => $item, 'isNew' => true, 'rowId' => $index); return $this->renderPartial('invoiceRow', $params); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = ucwords(str_replace("_", " ", $this->attributes['id'])); $validator = new sfValidatorChoice(array('choices' => array_keys($this->subDivisionList))); if ($this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; } $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
public function embedWidgetIntoForm(sfForm $form) { $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); $widgetSchema['from_date'] = new ohrmWidgetDatePicker(array(), array('id' => 'from_date')); $widgetSchema['from_date']->setLabel("From "); $form->setValidator('from_date', new sfValidatorDate()); $widgetSchema['to_date'] = new ohrmWidgetDatePicker(array(), array('id' => 'to_date')); $widgetSchema['to_date']->setLabel("To "); $form->setValidator('to_date', new sfValidatorDate()); $validatorSchema->setPostValidator(new sfValidatorSchemaCompare('from_date', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'to_date', array('throw_global_error' => true), array('invalid' => 'The from date ("%left_field%") must be before the to date ("%right_field%")'))); return $form; }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = ucwords(str_replace("_", " ", $this->attributes['id'])); $validator = new sfValidatorString(); if ($this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $validator = new sfValidatorString(array('required' => true), array('required' => 'No employees added to the system')); } $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = __(ucwords(str_replace("_", " ", $this->attributes['id']))); $validator = new sfValidatorString(array('required' => false)); if ($this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $validator = new sfValidatorString(array('required' => true), array('required' => 'Add a project to view')); } $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
public function embedWidgetIntoForm(sfForm &$form) { $requiredMess = 'Select a location'; $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = ucwords(str_replace("_", " ", $this->attributes['id'])); $validator = new sfValidatorString(); if (isset($this->attributes['required']) && $this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $validator = new sfValidatorString(array('required' => true), array('required' => $requiredMess)); } $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); $widgetSchema[$this->attributes['id']] = $this; $widgetSchema[$this->attributes['id']]->setLabel(ucwords(str_replace("_", " ", $this->attributes['id']))); $requiredMessage = __(ValidationMessages::REQUIRED); $validatorSchema[$this->attributes['id']] = new ohrmValidatorConditionalFilter(array(), array('required' => $requiredMessage)); //$form->setValidator('date_period', new sfValidatorString()); // $validatorSchema[$this->attributes['id']] = new ohrmValidatorDateRange(array(), array("invalid" => "Insert a correct date")); // $validatorSchema[$this->attributes['id']] = new sfValidatorPass(); // $validatorSchema->setPostValidator(new ohrmValidatorSchemaDateRange($this->attributes['id'], ohrmValidatorSchemaDateRange::LESS_THAN_EQUAL, $this->attributes['id'], // array('throw_global_error' => true), // array('invalid' => 'The from date ("%left_field%") must be before the to date ("%right_field%")') // )); }
/** * Configures some things on the sfWidgetSchema of the form */ protected function configureWidgetSchema(sfForm $form) { $widgetSchema = $form->getWidgetSchema(); $requiredFields = $form->getRequiredFields(); $widgetSchema->addOption('required_fields', $requiredFields); $formFormatters = sfSympalConfig::get('form', 'form_formatters', array()); $catalogue = $widgetSchema->getFormFormatter()->getTranslationCatalogue(); foreach ($formFormatters as $name => $class) { $formFormatter = new $class($widgetSchema); // persist the translation catalogue if ($catalogue) { $formFormatter->setTranslationCatalogue($catalogue); } $widgetSchema->addFormFormatter($name, $formFormatter); } }
public static function replaceValidators(sfForm $form) { foreach ($form->getWidgetSchema()->getFields() as $name => $widget) { if ($widget instanceof mtWidgetFormInputDate) { $form->setValidator($name, self::getDateValidator()); } elseif ($widget instanceof sfWidgetFormFilterDate) { $form->getValidator($name)->setOption("from_date", self::getDateValidator(array("required" => false))); $form->getValidator($name)->setOption("to_date", self::getDateValidator(array("required" => false))); } if ($name == "attachment") { $form->setValidator($name, new sfValidatorFile(array("required" => false, "path" => sfConfig::get("sf_upload_dir")))); } elseif ($name == "email") { $form->setValidator($name, new sfValidatorEmail()); } } }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = __(ucwords(str_replace("_", " ", $this->attributes['id']))); $required = false; if (isset($this->attributes['required']) && $this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $required = true; } $requiredMess = $label . ' is required.'; //$validator = new sfValidatorString(array('required' => $required), array('required' => $requiredMess)); $validator = new sfValidatorPass(); $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $userObj = sfContext::getInstance()->getUser()->getAttribute("user"); $projectList = $userObj->getActiveProjectList(); $requiredMess = __('Select a project'); if ($projectList == null) { $requiredMess = __("No Projects Defined"); } $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = __(ucwords(str_replace("_", " ", $this->attributes['id']))); $validator = new sfValidatorString(); if ($this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $validator = new sfValidatorString(array('required' => true), array('required' => $requiredMess)); } $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
/** * Gets the fields that represents the form. * * If no form.display parameter is passed in the configuration, * all the fields from the form are returned (dynamically). * * @param sfForm $form The form with the fields * @param string $context The display context */ public function getFormFields(sfForm $form, $context) { $config = $this->getConfig(); $method = sprintf('get%sDisplay', ucfirst($context)); if (!($itemsLists = $this->{$method}())) { $itemsLists = $this->getFormDisplay(); } if ($itemsLists) { $fields = array(); // with itemsLists? if (!is_array(current($itemsLists))) { $itemsLists = array($itemsLists); } foreach ($itemsLists as $itemsList => $fieldsets) { $fields[$itemsList] = array(); // with fieldsets? if (!is_array(current($fieldsets))) { $fieldsets = array('NONE' => $fieldsets); } foreach ($fieldsets as $fieldset => $names) { $fields[$itemsList][$fieldset] = array(); foreach ($names as $name) { list($name, $flag) = sfModelGeneratorConfigurationField::splitFieldWithFlag($name); if (!isset($this->configuration[$context]['fields'][$name])) { $this->configuration[$context]['fields'][$name] = new sfModelGeneratorConfigurationField($name, array_merge(isset($config['default'][$name]) ? $config['default'][$name] : array(), isset($config['form'][$name]) ? $config['form'][$name] : array(), isset($config[$context][$name]) ? $config[$context][$name] : array(), array('is_real' => false, 'type' => 'Text', 'flag' => $flag))); } $field = $this->configuration[$context]['fields'][$name]; $field->setFlag($flag); $fields[$itemsList][$fieldset][$name] = $field; } } } return $fields; } $fields = array(); foreach ($form->getWidgetSchema()->getPositions() as $name) { $fields[$name] = new sfModelGeneratorConfigurationField($name, array_merge(isset($config['default'][$name]) ? $config['default'][$name] : array(), isset($config['form'][$name]) ? $config['form'][$name] : array(), isset($config[$context][$name]) ? $config[$context][$name] : array(), array('is_real' => false, 'type' => 'Text'))); } return array(array('NONE' => $fields)); }
public static function replaceWidgets(sfForm $form) { foreach ($form->getWidgetSchema()->getFields() as $name => $widget) { if ($widget instanceof sfWidgetFormDate) { $form->setWidget($name, self::getDateWidget()); } elseif ($widget instanceof sfWidgetFormFilterDate) { $form->getWidget($name)->setOption("from_date", self::getDateWidget(array("use_own_help" => false))); $form->getWidget($name)->setOption("to_date", self::getDateWidget(array("use_own_help" => false))); $form->getWidget($name)->setOption("template", __("from %from_date% to %to_date%")); } elseif ($widget instanceof sfWidgetFormTextarea) { $form->getWidget($name)->setAttribute("rows", 15); $form->getWidget($name)->setAttribute("cols", 100); } if ($name == "attachment") { $form->setWidget($name, new sfWidgetFormInputFile()); } elseif ($name == "created_by" || $name == "updated_by") { if ($form instanceof sfFormPropel) { $form->setWidget($name, new sfWidgetFormInputHidden()); } } elseif ($name == "password") { $form->setWidget($name, new sfWidgetFormInputPassword()); } } }
/** * Embeds this widget into the form. Sets label and validator for this widget. * @param sfForm $form */ public function embedWidgetIntoForm(sfForm &$form) { $widgetSchema = $form->getWidgetSchema(); $widgetSchema[$this->attributes['id']] = $this; $label = __(ucwords(str_replace("_", " ", $this->attributes['id']))); $required = false; $required = false; if (isset($this->attributes['required']) && $this->attributes['required'] == "true") { $label .= "<span class='required'> * </span>"; $required = true; } $validator = new sfValidatorCallback(array('callback' => array($this, 'validate'), 'required' => $required), array('required' => __(ValidationMessages::REQUIRED))); $widgetSchema[$this->attributes['id']]->setLabel($label); $form->setValidator($this->attributes['id'], $validator); }
/** * Change the content slot form value widget * * @param string $type The type of the widget * @param sfForm $form The form whose slot will be modified * @param string $fieldName The name of the "slot" field on the form * @return void */ public static function changeContentSlotValueWidget(sfSympalContentSlot $slot, sfForm $form) { // in case the type is blank $type = $slot->type ? $slot->type : 'Text'; $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); $contentSlotTypes = sfSympalConfig::get('content_slot_types', null, array()); $options = isset($contentSlotTypes[$type]) ? $contentSlotTypes[$type] : array(); $widgetClass = isset($options['widget_class']) ? $options['widget_class'] : 'sfWidgetFormSympal' . $type; $widgetOptions = isset($options['widget_options']) ? $options['widget_options'] : array(); $validatorClass = isset($options['validator_class']) ? $options['validator_class'] : 'sfValidatorFormSympal' . $type; $validatorOptions = isset($options['validator_options']) ? $options['validator_options'] : array(); $validatorOptions['required'] = false; /* * Setup the widget and validator: 3 cases: * 1) widget_class & is validator_class are not false, so we setup widget/validator using those * 2) widget_class & validator_class ARE false, the slot is a column - get the widget/validator from the content form * 3) All else fails, leave widget & validator alone */ if ($widgetClass && $validatorClass) { $widgetSchema['value'] = new $widgetClass($widgetOptions, array('class' => 'slot_' . strtolower($type))); $validatorSchema['value'] = new $validatorClass($validatorOptions); } elseif ($slot->is_column) { $contentForm = $slot->getContentSlotColumnForm(); $contentWidgetSchema = $contentForm->getWidgetSchema(); $contentValidatorSchema = $contentForm->getValidatorSchema(); $widgetSchema['value'] = $contentForm->getWidgetSchema()->offsetGet($slot->name); $validatorSchema['value'] = $contentForm->getValidatorSchema()->offsetGet($slot->name); } }
$t->is((string) $f['id'], '<input type="hidden" name="id" value="1" id="id" />', '->offsetGet() returns a sfFormField'); $t->is((string) $f['first_name'], '<input type="text" name="first_name" value="Fabien" id="first_name" />', '->offsetGet() returns a sfFormField'); $t->is((string) $f['last_name'], '<input type="text" name="last_name" value="Potencier" id="last_name" />', '->offsetGet() returns a sfFormField'); // renderUsing() $t->diag('->renderUsing()'); $f = new sfForm(); $f->setWidgets(array('name' => new sfWidgetFormInputText())); $output = <<<EOF <li> <label for="name">Name</label> <input type="text" name="name" id="name" /> </li> EOF; $t->is($f->renderUsing('list'), fix_linebreaks($output), 'renderUsing() renders the widget schema using the given form formatter'); $t->is($f->getWidgetSchema()->getFormFormatterName(), 'table', 'renderUsing() does not persist form formatter name for the current form instance'); $w = $f->getWidgetSchema(); $w->addFormFormatter('custom', new sfWidgetFormSchemaFormatterList($w)); $t->is($f->renderUsing('custom'), fix_linebreaks($output), 'renderUsing() renders a custom form formatter'); try { $f->renderUsing('nonexistant'); $t->fail('renderUsing() throws an exception if formatter name does not exist'); } catch (InvalidArgumentException $e) { $t->pass('renderUsing() throws an exception if formatter name does not exist'); } // renderHiddenFields() $t->diag('->renderHiddenFields()'); $f = new sfForm(); $f->setWidgets(array('id' => new sfWidgetFormInputHidden(), 'name' => new sfWidgetFormInputText(), 'is_admin' => new sfWidgetFormInputHidden())); $output = '<input type="hidden" name="id" id="id" /><input type="hidden" name="is_admin" id="is_admin" />'; $t->is($f->renderHiddenFields(), $output, 'renderHiddenFields() renders all hidden fields, no visible fields');
/** * Adds a new NameValueForm into the specified location for a 'generic' settings form. * @param string to Location where to insert the item * Ex: config['taxes'][new_1234567890][<name_or_value>] * |_ 'to' parameter |_ from NameValueForm * @return void * @author Carlos Escribano <*****@*****.**> **/ public function executeAddNewNameValueItem(sfWebRequest $request) { $this->forward404Unless($to = $request->getParameter('to')); $configForm = new sfForm(); $configForm->getWidgetSchema()->setNameFormat('config[%s]'); $index = 'new_' . time(); switch ($to) { case 'taxes': $subform = new FormsContainer(array($index => new TaxForm()), 'TaxForm'); break; case 'seriess': $subform = new FormsContainer(array($index => new SeriesForm()), 'SeriesForm'); break; } $configForm->embedForm($to, $subform); return $this->renderText($configForm[$to][$index]); }
/** * Embeds a sfForm into the current form. * * @param string $name The field name * @param sfForm $form A sfForm instance * @param string $decorator A HTML decorator for the embedded form */ public function embedForm($name, sfForm $form, $decorator = null) { $name = (string) $name; if (true === $this->isBound() || true === $form->isBound()) { throw new LogicException('A bound form cannot be embedded'); } unset($form[self::$CSRFFieldName]); $this->embeddedForms[$name] = $form; $widgetSchema = $form->getWidgetSchema(); $this->setDefault($name, $form->getDefaults()); $decorator = null === $decorator ? $widgetSchema->getFormFormatter()->getDecoratorFormat() : $decorator; $this->widgetSchema[$name] = new sfWidgetFormSchemaDecorator($widgetSchema, $decorator); $this->validatorSchema[$name] = new sfValidatorPass(); // keep widgetSchema synchronized $form->setWidgetSchema($this->widgetSchema[$name]->getWidget()); $this->resetFormFields(); }
public function __construct(sfForm $form) { parent::__construct($form->getWidgetSchema()); $this->setForm($form); }
/** * Embed a Doctrine_Collection relationship in to a form * * [php] * $userForm = new UserForm($user); * $userForm->embedRelation('Groups'); * * @param string $relationName The name of the relation * @param string $formClass The name of the form class to use * @param array $formArguments Arguments to pass to the constructor (related object will be shifted onto the front) * * @throws InvalidArgumentException If the relationship is not a collection */ public function embedRelation($relationName, $formClass = null, $formArgs = array()) { // FIXME: Where exactly is getTable() declared? // It triggers __call() which retrieves a property called $table, // which doesn't seem to be declared anywhere so it triggers __get() // which then again tries to call getTable() (if it exists) or // returns $this->table, which eventually doesn't exists? // I don't think this is works throw new sfException('Not implemented'); $relation = $this->object->getTable()->getRelation($relationName); if ($relation->getType() !== Doctrine_Relation::MANY) { throw new InvalidArgumentException('You can only embed a relationship that is a collection.'); } $r = new ReflectionClass(null === $formClass ? $relation->getClass() . 'Form' : $formClass); $subForm = new sfForm(); foreach ($this->object[$relationName] as $index => $childObject) { $form = $r->newInstanceArgs(array_merge(array($childObject), $formArgs)); $subForm->embedForm($index, $form); $subForm->getWidgetSchema()->setLabel($index, (string) $childObject); } $this->embedForm($relationName, $subForm); }
/** * Embed a Doctrine_Collection relationship in to a form * * [php] * $userForm = new UserForm($user); * $userForm->embedRelation('Groups AS groups'); * * @param string $relationName The name of the relation and an optional alias * @param string $formClass The name of the form class to use * @param array $formArguments Arguments to pass to the constructor (related object will be shifted onto the front) * @param string $innerDecorator A HTML decorator for each embedded form * @param string $decorator A HTML decorator for the main embedded form * * @throws InvalidArgumentException If the relationship is not a collection */ public function embedRelation($relationName, $formClass = null, $formArgs = array(), $innerDecorator = null, $decorator = null) { if (false !== ($pos = stripos($relationName, ' as '))) { $fieldName = substr($relationName, $pos + 4); $relationName = substr($relationName, 0, $pos); } else { $fieldName = $relationName; } $relation = $this->getObject()->getTable()->getRelation($relationName); $r = new ReflectionClass(null === $formClass ? $relation->getClass() . 'Form' : $formClass); if (Doctrine_Relation::ONE == $relation->getType()) { $this->embedForm($fieldName, $r->newInstanceArgs(array_merge(array($this->getObject()->{$relationName}), $formArgs)), $decorator); } else { $subForm = new sfForm(); foreach ($this->getObject()->{$relationName} as $index => $childObject) { $form = $r->newInstanceArgs(array_merge(array($childObject), $formArgs)); $subForm->embedForm($index, $form, $innerDecorator); $subForm->getWidgetSchema()->setLabel($index, (string) $childObject); } $this->embedForm($fieldName, $subForm, $decorator); } }
/** * Assert form fields * * @param array $expected - array(name, title) * @param sfForm $form * @param string $message */ protected function assertFormFields($expected, sfForm $form, $message = null) { sort($expected); $actual = array_keys($form->getWidgetSchema()->getFields()); sort($actual); $this->assertEquals($expected, $actual, $message); }
public function embedRelations(array $relations) { $this->embedRelations = $relations; $this->getEventDispatcher()->connect('form.post_configure', array($this, 'listenToFormPostConfigureEvent')); foreach ($relations as $relationName => $relationSettings) { $relationSettings = $this->addDefaultRelationSettings($relationSettings); $relation = $this->getObject()->getTable()->getRelation($relationName); if (!$relationSettings['noNewForm']) { $containerName = 'new_' . $relationName; $formLabel = $relationSettings['newFormLabel']; if (!$relation->isOneToOne()) { if ($relationSettings['multipleNewForms']) { $newFormsCount = $relationSettings['newFormsInitialCount']; $subForm = $this->newFormsContainerFormFactory($relationSettings, $containerName); for ($i = 0; $i < $newFormsCount; $i++) { // we need to create new forms with cloned object inside (otherwise only the last new values would be saved) $newForm = $this->embeddedFormFactory($relationName, $relationSettings, $relation, $i + 1); $subForm->embedForm($i, $newForm); } $subForm->getWidgetSchema()->setLabel($formLabel); $this->embedForm($containerName, $subForm); } else { $newForm = $this->embeddedFormFactory($relationName, $relationSettings, $relation, $formLabel); $this->embedForm($containerName, $newForm); } } elseif ($relation->isOneToOne() && !$this->getObject()->relatedExists($relationName)) { $newForm = $this->embeddedFormFactory($relationName, $relationSettings, $relation, $formLabel); $this->embedForm($containerName, $newForm); } } $formClass = null === $relationSettings['formClass'] ? $relation->getClass() . 'Form' : $relationSettings['formClass']; $formArgs = null === $relationSettings['formClassArgs'] ? array() : $relationSettings['formClassArgs']; if (isset($formArgs[0]) && !array_key_exists('ah_add_delete_checkbox', $formArgs[0]) || !isset($formArgs[0])) { $formArgs[0]['ah_add_delete_checkbox'] = true; } if ($relation->isOneToOne()) { $form = new $formClass($this->getObject()->{$relationName}, $formArgs[0]); $this->embedForm($relationName, $form); //maybe we need this: if (!$this->getObject()->relatedExists($relationName)) unset($this[$relation->getLocalColumnName()]); } else { $subForm = new sfForm(); foreach ($this->getObject()->{$relationName} as $index => $childObject) { $form = new $formClass($childObject, $formArgs[0]); $subForm->embedForm($index, $form); // check if existing embedded relations should have a different label if (null === $relationSettings['customEmbeddedFormLabelMethod'] || !method_exists($childObject, $relationSettings['customEmbeddedFormLabelMethod'])) { $subForm->getWidgetSchema()->setLabel($index, (string) $childObject); } else { $subForm->getWidgetSchema()->setLabel($index, $childObject->{$relationSettings}['customEmbeddedFormLabelMethod']()); } } $this->embedForm($relationName, $subForm); } if ($relationSettings['formFormatter']) { $this->switchFormatter($relationName, $relationSettings['formFormatter']); } /* * Unset the relation form(s) if: * (1. One-to-many relation and there are no related objects yet (count of embedded forms is 0) OR * 2. One-to-one relation and embedded form is new (no related object yet)) * AND * (3. Option `displayEmptyRelations` was either not set by the user or was set by the user and is false) */ if ((!$relation->isOneToOne() && count($this->getEmbeddedForm($relationName)->getEmbeddedForms()) === 0 || $relation->isOneToOne() && $this->getEmbeddedForm($relationName)->isNew()) && !$relationSettings['displayEmptyRelations']) { unset($this[$relationName]); } if ($relationSettings['newFormAfterExistingRelations'] && isset($this[$relationName]) && isset($this['new_' . $relationName])) { $this->getWidgetSchema()->moveField('new_' . $relationName, sfWidgetFormSchema::AFTER, $relationName); } } $this->getEventDispatcher()->disconnect('form.post_configure', array($this, 'listenToFormPostConfigureEvent')); }
/** * Change the content slot form value widget * * @param sfSympalContentSlot $contentSlot * @param sfForm $form * @return void */ public static function changeContentSlotValueWidget(sfSympalContentSlot $contentSlot, sfForm $form) { if ($contentSlot->is_column) { return; } $type = $contentSlot->type ? $contentSlot->type : 'Text'; $widgetSchema = $form->getWidgetSchema(); $validatorSchema = $form->getValidatorSchema(); $contentSlotTypes = sfSympalConfig::get('content_slot_types', null, array()); $options = isset($contentSlotTypes[$type]) ? $contentSlotTypes[$type] : array(); $widgetClass = isset($options['widget_class']) ? $options['widget_class'] : 'sfWidgetFormSympal' . $type; $widgetOptions = isset($options['widget_options']) ? $options['widget_options'] : array(); $validatorClass = isset($options['validator_class']) ? $options['validator_class'] : 'sfValidatorFormSympal' . $type; $validatorOptions = isset($options['validator_options']) ? $options['validator_options'] : array(); $validatorOptions['required'] = false; $widgetSchema['value'] = new $widgetClass($widgetOptions); $validatorSchema['value'] = new $validatorClass($validatorOptions); }