Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308<?php
abstract class AphrontFormControl extends AphrontView {
private $label; private $ariaLabel; private $caption; private $error; private $name; private $value; private $disabled; private $id; private $controlID; private $controlStyle; private $required; private $hidden; private $classes; private $hasCopyButton;
public function setHidden($hidden) { $this->hidden = $hidden; return $this; }
public function setID($id) { $this->id = $id; return $this; }
public function getID() { return $this->id; }
public function setControlID($control_id) { $this->controlID = $control_id; return $this; }
public function getControlID() { return $this->controlID; }
public function setControlStyle($control_style) { $this->controlStyle = $control_style; return $this; }
public function getControlStyle() { return $this->controlStyle; }
public function setLabel($label) { $this->label = $label; return $this; }
/** * Explicitly set an aria-label attribute for accessibility. Only to be used * when no visible label is already set via setLabel(). * @param string $aria_label aria-label text to add to the form control */ public function setAriaLabel($aria_label) { $this->ariaLabel = $aria_label; return $this; }
public function getAriaLabel() { return $this->ariaLabel; }
public function getLabel() { return $this->label; }
/** * Set the Caption * The Caption shows a tip usually nearby the related input field. * @param string|PhutilSafeHTML|null $caption * @return self */ public function setCaption($caption) { $this->caption = $caption; return $this; }
/** * Get the Caption * The Caption shows a tip usually nearby the related input field. * @return string|PhutilSafeHTML|null */ public function getCaption() { return $this->caption; }
public function setError($error) { $this->error = $error; return $this; }
public function getError() { return $this->error; }
public function setName($name) { $this->name = $name; return $this; }
public function getName() { return $this->name; }
public function setValue($value) { $this->value = $value; return $this; }
/** * Whether a button is displayed next to the control which allows the user to * copy the value in the form control. Common use cases include <input> * (AphrontFormTextControl) and <textarea> (AphrontFormTextAreaControl) * elements displaying read-only data such as tokens or passphrases. This is * only to get the CSS right; actual button implementation is in subclasses. * * @return bool */ public function getHasCopyButton() { return $this->hasCopyButton; }
/** * Whether to display a button next to the control which allows the user to * copy the value in the form control. Common use cases include <input> * (AphrontFormTextControl) and <textarea> (AphrontFormTextAreaControl) * elements displaying read-only data such as tokens or passphrases. This is * only to get the CSS right; actual button implementation is in subclasses. * * @param bool $has_copy_button */ public function setHasCopyButton($has_copy_button) { $this->hasCopyButton = $has_copy_button; return $this; }
public function getValue() { return $this->value; }
public function isValid() { if ($this->error && $this->error !== true) { return false; }
if ($this->isRequired() && $this->isEmpty()) { return false; }
return true; }
public function isRequired() { return $this->required; }
public function isEmpty() { return !strlen($this->getValue()); }
public function getSerializedValue() { return $this->getValue(); }
public function readSerializedValue($value) { $this->setValue($value); return $this; }
public function readValueFromRequest(AphrontRequest $request) { $this->setValue($request->getStr($this->getName())); return $this; }
public function readValueFromDictionary(array $dictionary) { $this->setValue(idx($dictionary, $this->getName())); return $this; }
public function setDisabled($disabled) { $this->disabled = $disabled; return $this; }
public function getDisabled() { return $this->disabled; }
abstract protected function renderInput(); abstract protected function getCustomControlClass();
protected function shouldRender() { return true; }
public function addClass($class) { $this->classes[] = $class; return $this; }
final public function render() { if (!$this->shouldRender()) { return null; }
$custom_class = $this->getCustomControlClass();
// If we don't have an ID yet, assign an automatic one so we can associate // the label with the control. This allows assistive technologies to read // form labels. if (!$this->getID()) { $this->setID(celerity_generate_unique_node_id()); }
$class = 'aphront-form-input'; if ($this->getHasCopyButton()) { $class = $class.' has-copy-button'; }
$input = phutil_tag( 'div', array('class' => $class), $this->renderInput());
$error = null; if ($this->getError()) { $error = $this->getError(); if ($error === true) { $error = phutil_tag( 'span', array('class' => 'aphront-form-error aphront-form-required'), pht('Required')); } else { $error = phutil_tag( 'span', array('class' => 'aphront-form-error'), $error); } }
if (phutil_nonempty_string($this->getLabel())) { $label = phutil_tag( 'label', array( 'class' => 'aphront-form-label', 'for' => $this->getID(), ), array( $this->getLabel(), $error, )); } else { $label = null; $custom_class .= ' aphront-form-control-nolabel'; }
// The Caption can be stuff like PhutilSafeHTML and other objects that // can be casted to a string. After this cast we have never null. $has_caption = phutil_string_cast($this->getCaption()) !== '';
if ($has_caption) { $caption = phutil_tag( 'div', array('class' => 'aphront-form-caption'), $this->getCaption()); } else { $caption = null; }
$classes = array(); $classes[] = 'aphront-form-control'; $classes[] = 'grouped'; $classes[] = $custom_class; if ($this->classes) { foreach ($this->classes as $class) { $classes[] = $class; } }
$style = $this->controlStyle; if ($this->hidden) { $style = 'display: none; '.$style; }
return phutil_tag( 'div', array( 'class' => implode(' ', $classes), 'id' => $this->controlID, 'style' => $style, ), array( $label, $error, $input, $caption, )); }}