Path: blob/master/src/view/form/control/AphrontFormControl.php
12262 views
<?php12abstract class AphrontFormControl extends AphrontView {34private $label;5private $caption;6private $error;7private $name;8private $value;9private $disabled;10private $id;11private $controlID;12private $controlStyle;13private $required;14private $hidden;15private $classes;1617public function setHidden($hidden) {18$this->hidden = $hidden;19return $this;20}2122public function setID($id) {23$this->id = $id;24return $this;25}2627public function getID() {28return $this->id;29}3031public function setControlID($control_id) {32$this->controlID = $control_id;33return $this;34}3536public function getControlID() {37return $this->controlID;38}3940public function setControlStyle($control_style) {41$this->controlStyle = $control_style;42return $this;43}4445public function getControlStyle() {46return $this->controlStyle;47}4849public function setLabel($label) {50$this->label = $label;51return $this;52}5354public function getLabel() {55return $this->label;56}5758public function setCaption($caption) {59$this->caption = $caption;60return $this;61}6263public function getCaption() {64return $this->caption;65}6667public function setError($error) {68$this->error = $error;69return $this;70}7172public function getError() {73return $this->error;74}7576public function setName($name) {77$this->name = $name;78return $this;79}8081public function getName() {82return $this->name;83}8485public function setValue($value) {86$this->value = $value;87return $this;88}8990public function getValue() {91return $this->value;92}9394public function isValid() {95if ($this->error && $this->error !== true) {96return false;97}9899if ($this->isRequired() && $this->isEmpty()) {100return false;101}102103return true;104}105106public function isRequired() {107return $this->required;108}109110public function isEmpty() {111return $this->getValue() === null || !strlen($this->getValue());112}113114public function getSerializedValue() {115return $this->getValue();116}117118public function readSerializedValue($value) {119$this->setValue($value);120return $this;121}122123public function readValueFromRequest(AphrontRequest $request) {124$this->setValue($request->getStr($this->getName()));125return $this;126}127128public function readValueFromDictionary(array $dictionary) {129$this->setValue(idx($dictionary, $this->getName()));130return $this;131}132133public function setDisabled($disabled) {134$this->disabled = $disabled;135return $this;136}137138public function getDisabled() {139return $this->disabled;140}141142abstract protected function renderInput();143abstract protected function getCustomControlClass();144145protected function shouldRender() {146return true;147}148149public function addClass($class) {150$this->classes[] = $class;151return $this;152}153154final public function render() {155if (!$this->shouldRender()) {156return null;157}158159$custom_class = $this->getCustomControlClass();160161// If we don't have an ID yet, assign an automatic one so we can associate162// the label with the control. This allows assistive technologies to read163// form labels.164if (!$this->getID()) {165$this->setID(celerity_generate_unique_node_id());166}167168$input = phutil_tag(169'div',170array('class' => 'aphront-form-input'),171$this->renderInput());172173$error = $this->error;174if ($error !== null && strlen($error)) {175if ($error === true) {176$error = phutil_tag(177'span',178array('class' => 'aphront-form-error aphront-form-required'),179pht('Required'));180} else {181$error = phutil_tag(182'span',183array('class' => 'aphront-form-error'),184$error);185}186} else {187$error = null;188}189190$label = $this->label;191if ($label !== null && strlen($label)) {192$label = phutil_tag(193'label',194array(195'class' => 'aphront-form-label',196'for' => $this->getID(),197),198array(199$this->getLabel(),200$error,201));202} else {203$label = null;204$custom_class .= ' aphront-form-control-nolabel';205}206207$caption = $this->caption;208if ($caption !== null && strlen($caption)) {209$caption = phutil_tag(210'div',211array('class' => 'aphront-form-caption'),212$this->getCaption());213} else {214$caption = null;215}216217$classes = array();218$classes[] = 'aphront-form-control';219$classes[] = 'grouped';220$classes[] = $custom_class;221if ($this->classes) {222foreach ($this->classes as $class) {223$classes[] = $class;224}225}226227$style = $this->controlStyle;228if ($this->hidden) {229$style = 'display: none; '.$style;230}231232return phutil_tag(233'div',234array(235'class' => implode(' ', $classes),236'id' => $this->controlID,237'style' => $style,238),239array(240$label,241$error,242$input,243$caption,244));245}246}247248249