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();
|
2013-07-14 16:37:17 +02:00
|
|
|
private $content = array();
|
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
|
|
|
private $values = array();
|
|
|
|
private $isValid;
|
2013-07-14 16:37:17 +02:00
|
|
|
private $validateFormPageCallback;
|
|
|
|
private $adjustFormPageCallback;
|
|
|
|
private $pageErrors;
|
|
|
|
private $pageName;
|
|
|
|
|
|
|
|
|
|
|
|
public function setPageName($page_name) {
|
|
|
|
$this->pageName = $page_name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPageName() {
|
|
|
|
return $this->pageName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addPageError($page_error) {
|
|
|
|
$this->pageErrors[] = $page_error;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPageErrors() {
|
|
|
|
return $this->pageErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setAdjustFormPageCallback($adjust_form_page_callback) {
|
|
|
|
$this->adjustFormPageCallback = $adjust_form_page_callback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setValidateFormPageCallback($validate_form_page_callback) {
|
|
|
|
$this->validateFormPageCallback = $validate_form_page_callback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addInstructions($text, $before = null) {
|
|
|
|
$tag = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'aphront-form-instructions',
|
|
|
|
),
|
|
|
|
$text);
|
|
|
|
|
|
|
|
$append = true;
|
|
|
|
if ($before !== null) {
|
|
|
|
for ($ii = 0; $ii < count($this->content); $ii++) {
|
|
|
|
if ($this->content[$ii] instanceof AphrontFormControl) {
|
|
|
|
if ($this->content[$ii]->getName() == $before) {
|
|
|
|
array_splice($this->content, $ii, 0, array($tag));
|
|
|
|
$append = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($append) {
|
|
|
|
$this->content[] = $tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function addRemarkupInstructions($remarkup, $before = null) {
|
|
|
|
return $this->addInstructions(
|
|
|
|
PhabricatorMarkupEngine::renderOneObject(
|
|
|
|
id(new PhabricatorMarkupOneOff())->setContent($remarkup),
|
|
|
|
'default',
|
|
|
|
$this->getUser()), $before);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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;
|
2013-07-14 16:37:17 +02:00
|
|
|
$this->content[] = $control;
|
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
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2013-07-14 16:37:17 +02:00
|
|
|
public function adjustFormPage() {
|
|
|
|
if ($this->adjustFormPageCallback) {
|
|
|
|
call_user_func($this->adjustFormPageCallback, $this);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function validateFormPage() {
|
|
|
|
if ($this->validateFormPageCallback) {
|
|
|
|
return call_user_func($this->validateFormPageCallback, $this);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
public function getKey() {
|
|
|
|
return $this->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render() {
|
2013-07-14 16:37:17 +02:00
|
|
|
return $this->content;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-07-14 16:37:17 +02:00
|
|
|
$this->isValid = $this->validateControls() && $this->validateFormPage();
|
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
|
|
|
}
|
|
|
|
return $this->isValid;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function readFromRequest(AphrontRequest $request) {
|
|
|
|
foreach ($this->getControls() as $name => $control) {
|
|
|
|
$control->readValueFromRequest($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function readFromObject($object) {
|
2013-10-25 22:59:02 +02:00
|
|
|
foreach ($this->getControls() as $name => $control) {
|
2013-11-26 17:57:17 +01:00
|
|
|
if (is_array($object)) {
|
|
|
|
$control->readValueFromDictionary($object);
|
|
|
|
}
|
2013-10-25 22:59:02 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|