*/
protected $__action;
+ /**
+ * form mode (see `self::FORM_MODE_*` constants)
+ * @var null|string
+ */
+ protected $__formMode;
+
/**
* `method` property of the HTML `form` element
* @var string
return $this;
}
+ /**
+ * @inheritDoc
+ */
+ public function formMode(string $formMode): IFormDocument {
+ if ($this->__formMode !== null) {
+ throw new \BadMethodCallException("Form mode has already been set");
+ }
+
+ if ($formMode !== self::FORM_MODE_CREATE && $formMode !== self::FORM_MODE_UPDATE) {
+ throw new \InvalidArgumentException("Unknown form mode '{$formMode}' given.");
+ }
+
+ $this->__formMode = $formMode;
+
+ return $this;
+ }
+
/**
* @inheritDoc
*/
return $this->enctype;
}
+ /**
+ * @inheritDoc
+ */
+ public function getFormMode(): string {
+ if ($this->__formMode === null) {
+ $this->__formMode = self::FORM_MODE_CREATE;
+ }
+
+ return $this->__formMode;
+ }
+
/**
* @inheritDoc
*/
/**
* @inheritDoc
*/
- public function loadValuesFromObject(IStorableObject $object): IFormDocument {
- /** @var IFormNode $node */
- foreach ($this->getIterator() as $node) {
- if ($node instanceof IFormField && $node->isAvailable()) {
- $node->loadValueFromObject($object);
- }
+ public function getPrefix(): string {
+ if ($this->__prefix === null) {
+ return '';
}
- return $this;
+ return $this->__prefix . '_';
}
/**
* @inheritDoc
*/
- public function getPrefix(): string {
- if ($this->__prefix === null) {
- return '';
+ public function loadValuesFromObject(IStorableObject $object): IFormDocument {
+ $this->formMode(self::FORM_MODE_UPDATE);
+
+ /** @var IFormNode $node */
+ foreach ($this->getIterator() as $node) {
+ if ($node instanceof IFormField && $node->isAvailable()) {
+ $node->loadValueFromObject($object);
+ }
}
- return $this->__prefix . '_';
+ return $this;
}
/**
* @since 3.2
*/
interface IFormDocument extends IFormParentNode {
+ /**
+ * represents the form mode for creating a new object
+ * @var string
+ */
+ const FORM_MODE_CREATE = 'create';
+
+ /**
+ * represents the form mode for updating a new object
+ * @var string
+ */
+ const FORM_MODE_UPDATE = 'update';
+
/**
* Sets the `action` property of the HTML `form` element and returns this document.
*
*/
public function build(): IFormDocument;
+ /**
+ * Sets the form mode (see `self::FORM_MODE_*` constants).
+ *
+ * @param string $formMode form mode
+ * @return static this document
+ *
+ * @throws \BadMethodCallException if the form mode has already been set
+ * @throws \InvalidArgumentException if the given form mode is invalid
+ */
+ public function formMode(string $formMode): IFormDocument;
+
/**
* Returns the `action` property of the HTML `form` element.
*
*/
public function getEnctype();
+ /**
+ * Returns the form mode (see `self::FORM_MODE_*` constants).
+ *
+ * The form mode can help validators to determine whether a new object
+ * is added or an existing object is edited. If no form mode has been
+ * explicitly set, `self::FORM_MODE_CREATE` is set and returned.
+ *
+ * @return string form mode
+ */
+ public function getFormMode(): string;
+
/**
* Returns the `method` property of the HTML `form` element. If no method
* has been set, `post` is returned.
* Loads the field values from the given object and returns this document.
*
* Per default, for each field, `IFormField::loadValueFromObject()` is called.
+ * This method automatically sets the form mode to `self::FORM_MODE_UPDATE`.
*
* @param IStorableObject $object object used to load field values
* @return static this document