1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-15 03:12:41 +01:00
phorge-phorge/src/view/form/PHUIFormPageView.php

134 lines
2.8 KiB
PHP
Raw Normal View History

Add a basic multipage form Summary: Ref T2232. Very busy day on IRC so I feel like I've made 20 minutes of progress in 1-minute spurts here, but this adds the basics for a form that can have multiple pages and automatically handle pagination and reading to/from the request, objects and responses. The UIExample is reasonably instructive. Basically, you make a form, add pages to the form, and add controls to the pages. The core flow control looks like this: if ($request->isFormPost()) { $form->readFromRequest($request); // (1) if ($form->isComplete()) { // (2) $response = $form->writeToResponse($response); // (3) // Process result here. // (4) } } else { $form->readFromObject($object); // (5) } The key parts are: # This reads the form state from the request, including reading all the inactive pages. # This tests if all pages are valid and the user just clicked "Done" on the last page. # This produces a "response", which might be writing to an object (for simpler forms) or creating a transaction record (for more complex forms). # Here, we would save the object or apply the transactions. # When the user views the form for the first time, we preload all the values from some object (which might just be empty). Ultimate goal here is to fix repository creation to not be a terrible pit of awfulness. There are probably a lot of rough edges and missing features still, but this seems to not be totally crazy. I'm using two submit buttons with different names which doesn't work on IE7 or something, but we can JS our way out of that if we need to. Test Plan: Paged forward and backward through the form. Reviewers: btrahan, chad Reviewed By: btrahan CC: aran Maniphest Tasks: T2232 Differential Revision: https://secure.phabricator.com/D6003
2013-05-23 23:39:01 +02:00
<?php
/**
* @concrete-extensible
*/
class PHUIFormPageView extends AphrontView {
private $key;
private $form;
private $controls = array();
private $values = array();
private $isValid;
public function addControl(AphrontFormControl $control) {
$name = $control->getName();
if (!strlen($name)) {
throw new Exception("Form control has no name!");
}
if (isset($this->controls[$name])) {
throw new Exception(
"Form page contains duplicate control with name '{$name}'!");
}
$this->controls[$name] = $control;
$control->setFormPage($this);
return $this;
}
public function getControls() {
return $this->controls;
}
public function getControl($name) {
if (empty($this->controls[$name])) {
throw new Exception("No page control '{$name}'!");
}
return $this->controls[$name];
}
protected function canAppendChild() {
return false;
}
public function setPagedFormView(PHUIPagedFormView $view, $key) {
if ($this->key) {
throw new Exception("This page is already part of a form!");
}
$this->form = $view;
$this->key = $key;
return $this;
}
public function getKey() {
return $this->key;
}
public function render() {
return $this->getControls();
}
public function getForm() {
return $this->form;
}
public function getRequestKey($key) {
return $this->getForm()->getRequestKey('p:'.$this->key.':'.$key);
}
public function validateObjectType($object) {
return true;
}
public function validateResponseType($response) {
return true;
}
protected function validateControls() {
$result = true;
foreach ($this->getControls() as $name => $control) {
if (!$control->isValid()) {
$result = false;
break;
}
}
return $result;
}
public function isValid() {
if ($this->isValid === null) {
$this->isValid = $this->validateControls();
}
return $this->isValid;
}
public function readFromRequest(AphrontRequest $request) {
foreach ($this->getControls() as $name => $control) {
$control->readValueFromRequest($request);
}
return $this;
}
public function readFromObject($object) {
return $this;
}
public function writeToResponse($response) {
return $this;
}
public function readSerializedValues(AphrontRequest $request) {
foreach ($this->getControls() as $name => $control) {
$key = $this->getRequestKey($name);
$control->readSerializedValue($request->getStr($key));
}
return $this;
}
public function getSerializedValues() {
$dict = array();
foreach ($this->getControls() as $name => $control) {
$key = $this->getRequestKey($name);
$dict[$key] = $control->getSerializedValue();
}
return $dict;
}
}