[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/view/form/control/ -> AphrontFormControl.php (source)

   1  <?php
   2  
   3  abstract class AphrontFormControl extends AphrontView {
   4  
   5    private $label;
   6    private $caption;
   7    private $error;
   8    private $name;
   9    private $value;
  10    private $disabled;
  11    private $id;
  12    private $controlID;
  13    private $controlStyle;
  14    private $formPage;
  15    private $required;
  16    private $hidden;
  17  
  18    public function setHidden($hidden) {
  19      $this->hidden = $hidden;
  20      return $this;
  21    }
  22  
  23    public function setID($id) {
  24      $this->id = $id;
  25      return $this;
  26    }
  27  
  28    public function getID() {
  29      return $this->id;
  30    }
  31  
  32    public function setControlID($control_id) {
  33      $this->controlID = $control_id;
  34      return $this;
  35    }
  36  
  37    public function getControlID() {
  38      return $this->controlID;
  39    }
  40  
  41    public function setControlStyle($control_style) {
  42      $this->controlStyle = $control_style;
  43      return $this;
  44    }
  45  
  46    public function getControlStyle() {
  47      return $this->controlStyle;
  48    }
  49  
  50    public function setLabel($label) {
  51      $this->label = $label;
  52      return $this;
  53    }
  54  
  55    public function getLabel() {
  56      return $this->label;
  57    }
  58  
  59    public function setCaption($caption) {
  60      $this->caption = $caption;
  61      return $this;
  62    }
  63  
  64    public function getCaption() {
  65      return $this->caption;
  66    }
  67  
  68    public function setError($error) {
  69      $this->error = $error;
  70      return $this;
  71    }
  72  
  73    public function getError() {
  74      return $this->error;
  75    }
  76  
  77    public function setName($name) {
  78      $this->name = $name;
  79      return $this;
  80    }
  81  
  82    public function getName() {
  83      return $this->name;
  84    }
  85  
  86    public function setValue($value) {
  87      $this->value = $value;
  88      return $this;
  89    }
  90  
  91    public function getValue() {
  92      return $this->value;
  93    }
  94  
  95    public function isValid() {
  96      if ($this->error && $this->error !== true) {
  97        return false;
  98      }
  99  
 100      if ($this->isRequired() && $this->isEmpty()) {
 101        return false;
 102      }
 103  
 104      return true;
 105    }
 106  
 107    public function isRequired() {
 108      return $this->required;
 109    }
 110  
 111    public function isEmpty() {
 112      return !strlen($this->getValue());
 113    }
 114  
 115    public function getSerializedValue() {
 116      return $this->getValue();
 117    }
 118  
 119    public function readSerializedValue($value) {
 120      $this->setValue($value);
 121      return $this;
 122    }
 123  
 124    public function readValueFromRequest(AphrontRequest $request) {
 125      $this->setValue($request->getStr($this->getName()));
 126      return $this;
 127    }
 128  
 129    public function readValueFromDictionary(array $dictionary) {
 130      $this->setValue(idx($dictionary, $this->getName()));
 131      return $this;
 132    }
 133  
 134    public function setFormPage(PHUIFormPageView $page) {
 135      if ($this->formPage) {
 136        throw new Exception('This control is already a member of a page!');
 137      }
 138      $this->formPage = $page;
 139      return $this;
 140    }
 141  
 142    public function getFormPage() {
 143      if ($this->formPage === null) {
 144        throw new Exception('This control does not have a page!');
 145      }
 146      return $this->formPage;
 147    }
 148  
 149    public function setDisabled($disabled) {
 150      $this->disabled = $disabled;
 151      return $this;
 152    }
 153  
 154    public function getDisabled() {
 155      return $this->disabled;
 156    }
 157  
 158    abstract protected function renderInput();
 159    abstract protected function getCustomControlClass();
 160  
 161    protected function shouldRender() {
 162      return true;
 163    }
 164  
 165    final public function render() {
 166      if (!$this->shouldRender()) {
 167        return null;
 168      }
 169  
 170      $custom_class = $this->getCustomControlClass();
 171  
 172      // If we don't have an ID yet, assign an automatic one so we can associate
 173      // the label with the control. This allows assistive technologies to read
 174      // form labels.
 175      if (!$this->getID()) {
 176        $this->setID(celerity_generate_unique_node_id());
 177      }
 178  
 179      $input = phutil_tag(
 180        'div',
 181        array('class' => 'aphront-form-input'),
 182        $this->renderInput());
 183  
 184      if (strlen($this->getLabel())) {
 185        $label = phutil_tag(
 186          'label',
 187          array(
 188            'class' => 'aphront-form-label',
 189            'for' => $this->getID(),
 190          ),
 191          $this->getLabel());
 192      } else {
 193        $label = null;
 194        $custom_class .= ' aphront-form-control-nolabel';
 195      }
 196  
 197      if (strlen($this->getError())) {
 198        $error = $this->getError();
 199        if ($error === true) {
 200          $error = phutil_tag(
 201            'div',
 202            array('class' => 'aphront-form-error aphront-form-required'),
 203            pht('Required'));
 204        } else {
 205          $error = phutil_tag(
 206            'div',
 207            array('class' => 'aphront-form-error'),
 208            $error);
 209        }
 210      } else {
 211        $error = null;
 212      }
 213  
 214      if (strlen($this->getCaption())) {
 215        $caption = phutil_tag(
 216          'div',
 217          array('class' => 'aphront-form-caption'),
 218          $this->getCaption());
 219      } else {
 220        $caption = null;
 221      }
 222  
 223      $classes = array();
 224      $classes[] = 'aphront-form-control';
 225      $classes[] = $custom_class;
 226  
 227      $style = $this->controlStyle;
 228      if ($this->hidden) {
 229        $style = 'display: none; '.$style;
 230      }
 231  
 232      return phutil_tag(
 233        'div',
 234        array(
 235          'class' => implode(' ', $classes),
 236          'id' => $this->controlID,
 237          'style' => $style,
 238        ),
 239        array(
 240          $label,
 241          $error,
 242          $input,
 243          $caption,
 244  
 245          // TODO: Remove this once the redesign finishes up.
 246          phutil_tag('div', array('style' => 'clear: both;'), ''),
 247        ));
 248    }
 249  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1