/** * @inheritdoc */ public function buildForm($formDefinition) { // Create the form object. $form = new Form($this->getSubmitUrl(isset($formDefinition['form-url']) ? $formDefinition['form-url'] : null), isset($formDefinition['target-url']) ? $formDefinition['target-url'] : null, isset($formDefinition['cancel-url']) ? $formDefinition['cancel-url'] : null, null, $this->getFormsModule()->registry()->getValues($this->getFormKey()), $this->getFormsModule()->registry()->getErrors($this->getFormKey()), array('value' => 'Next >'), null, array('value' => '< Back')); // Create fields. foreach ($formDefinition['fields'] as $name => $fieldDefinition) { $field = $this->buildField($form, $name, $fieldDefinition); if (!is_null($this->account) && !is_null($fieldValue = $this->account->getNamedFieldValue($name))) { $field->setDefaultValue($fieldValue->getValue()); } $form->addField($field); } // Create steps. foreach ($formDefinition['steps'] as $stepIndex => $stepDefinition) { // Create the step object. $oneWay = isset($stepDefinition['one-way']) ? $stepDefinition['one-way'] : false; $heading = isset($stepDefinition['heading']) ? $stepDefinition['heading'] : null; $errorHeading = isset($stepDefinition['error-heading']) ? $stepDefinition['error-heading'] : null; $step = new Step($form, $stepIndex, $oneWay, $heading, $errorHeading); // Add fieldsets to the step. foreach ($stepDefinition['fieldsets'] as $fieldsetName) { $fieldsetDefinition = $formDefinition['fieldsets'][$fieldsetName]; $step->addFieldset($this->buildFieldset($step, $fieldsetDefinition)); } // Add processors to the step. if (isset($stepDefinition['processors'])) { foreach ($stepDefinition['processors'] as $processorDefinition) { if (is_string($processorDefinition)) { $processorDefinition = array('module' => 'customer', 'method' => $processorDefinition); } $step->addProcessor($this->buildProcessor($processorDefinition)); } } // Add step to the form. $form->addStep($step); } return $form; }
/** * Create a single step of the form. * * @param FormInterface $form * @param array $formDefinition * @param integer $stepIndex * * @return \Sitegear\Form\StepInterface */ public function buildStep(FormInterface $form, array $formDefinition, $stepIndex) { LoggerRegistry::debug('FormBuilder::buildStep()'); $stepDefinition = $formDefinition['steps'][$stepIndex]; $oneWay = isset($stepDefinition['one-way']) ? $stepDefinition['one-way'] : false; $heading = isset($stepDefinition['heading']) ? $stepDefinition['heading'] : null; $errorHeading = isset($stepDefinition['error-heading']) ? $stepDefinition['error-heading'] : null; $step = new Step($form, $stepIndex, $oneWay, $heading, $errorHeading); if (isset($stepDefinition['fieldsets'])) { foreach ($stepDefinition['fieldsets'] as $fieldsetDefinition) { $step->addFieldset($this->buildFieldset($step, $fieldsetDefinition)); } } if (isset($stepDefinition['processors'])) { foreach ($stepDefinition['processors'] as $processorDefinition) { $step->addProcessor($this->buildProcessor($processorDefinition)); } } return $step; }
/** * The $formDefinition argument should be an array containing the following keys: * * 'submit-url' which is the URL for form submission. * 'module-name' which is the name of the module providing the purchase item. * 'type' which is the type of the purchase item. * 'id' which is the identifier of the purchase item, unique within items of the given module and type. * 'labels' which is an array containing keys 'quantity-field', 'no-value-option', and 'value-format', which may * contain tokens %label% and %value%. * * @param array $formDefinition * * @return \Sitegear\Form\FormInterface * * @throws \InvalidArgumentException */ public function buildForm($formDefinition) { $form = new Form($formDefinition['submit-url'], null, null, null, $this->getFormsModule()->registry()->getValues($this->getFormKey()), $this->getFormsModule()->registry()->getErrors($this->getFormKey())); $module = $this->getFormsModule()->getEngine()->getModule($formDefinition['module-name']); if (!$module instanceof PurchaseItemProviderModuleInterface) { throw new \InvalidArgumentException(sprintf('The specified module "%s" is not a valid purchase item provider.', $formDefinition['module-name'])); } // Add the hidden fields. // TODO Make 'module-name', 'type' and 'id' a single hidden field with encoded value $moduleField = new InputField($form, 'module', $formDefinition['module-name']); $moduleField->setSetting('type', 'hidden'); $form->addField($moduleField); $typeField = new InputField($form, 'type', $formDefinition['type']); $typeField->setSetting('type', 'hidden'); $form->addField($typeField); $idField = new InputField($form, 'id', $formDefinition['id']); $idField->setSetting('type', 'hidden'); $form->addField($idField); // Create the array of field names for references used by the single step of the form. $fields = array('module' => false, 'type' => false, 'id' => false); // Add a field to the form for every purchase item attribute. foreach ($module->getPurchaseItemAttributeDefinitions($formDefinition['type'], $formDefinition['id']) as $attribute) { $name = sprintf('attr_%s', $attribute['id']); $constraints = array(new ConditionalConstraint(new NotBlank())); // TODO Other field types - MultiInputField with radios and checkboxes $attributeField = new SelectField($form, $name, null, $attribute['label'], null, $constraints); $attributeField->setSetting('values', $this->buildAddTrolleyItemFormAttributeFieldValues($attribute, $formDefinition['labels']['no-value-option'], $formDefinition['labels']['value-format'])); $form->addField($attributeField); $fields[$name] = true; } // Add the quantity field, which is a standard text field with a label. $quantityField = new InputField($form, 'quantity', 1, $formDefinition['labels']['quantity-field']); $quantityField->addConditionalConstraint(new ConditionalConstraint(new NotBlank())); $quantityField->addConditionalConstraint(new ConditionalConstraint(new Range(array('min' => 1)))); $form->addField($quantityField); $fields['quantity'] = true; // Complete the form structure. $step = new Step($form, 0); $fieldset = new Fieldset($step); foreach ($fields as $field => $wrapped) { $fieldset->addFieldReference(new FieldReference($field, false, $wrapped)); } $form->addStep($step->addFieldset($fieldset)); return $form; }