/** * Add a field to the form container. * * @param Field $field The field definition. * * @return Field A reference to the field just added, to allow for function chaining to configure the field. * * @throws \InvalidArgumentException Thrown if the field definition is not valid, a field with the specified name is already defined, or adding the field would result in a duplicate field id. */ public function addField(Field $field) { if (!isset($field)) { throw new \InvalidArgumentException($this->__('Invalid field definition')); } elseif ($field->getFormContainer() !== $this) { throw new \InvalidArgumentException($this->__('Form container mismatch.')); } elseif (array_key_exists($field->fieldName, $this->formFields)) { throw new \InvalidArgumentException($this->__f('Field definition for the \'%1$s\' field is already defined.', array($field->fieldName))); } elseif (array_key_exists($field->fieldId, $this->fieldIds)) { throw new \InvalidArgumentException($this->__f('Field definition duplicates the field id \'%1$s\' already claimed by the field \'%2$s\'.', array($field->fieldId, $this->fieldIds[$field->fieldId]))); } $this->formFields[$field->fieldName] = $field; $this->fieldIds[$field->fieldId] =& $this->formFields[$field->fieldName]; return $this->formFields[$field->fieldName]; }