Version: current

      1 <?php
      2 /**
      3  * TextareaField creates a multi-line text field,
      4  * allowing more data to be entered than a standard
      5  * text field. It creates the <textarea> tag in the
      6  * form HTML.
      7  *
      8  * @package forms
      9  * @subpackage fields-basic
     10  */
     11 class TextareaField extends FormField {
     12     protected $rows, $cols, $disabled = false, $readonly = false;
     13
     14     /**
     15      * Create a new textarea field.
     16      *
     17      * @param $name Field name
     18      * @param $title Field title
     19      * @param $rows The number of rows
     20      * @param $cols The number of columns
     21      * @param $value The current value
     22      * @param $form The parent form.  Auto-set when the field is placed in a form.
     23      */
     24     function __construct($name, $title = null, $rows = 5, $cols = 20, $value = "", $form = null) {
     25         $this->rows = $rows;
     26         $this->cols = $cols;
     27         parent::__construct($name, $title, $value, $form);
     28     }
     29
     30     /**
     31      * Create the <textarea> or <span> HTML tag with the
     32      * attributes for this instance of TextareaField. This
     33      * makes use of {@link FormField->createTag()} functionality.
     34      *
     35      * @return HTML code for the textarea OR span element
     36      */
     37     function Field() {
     38         if($this->readonly) {
     39             $attributes = array(
     40                 'id' => $this->id(),
     41                 'class' => 'readonly' . ($this->extraClass() ? $this->extraClass() : ''),
     42                 'name' => $this->name,
     43                 'tabindex' => $this->getTabIndex(),
     44                 'readonly' => 'readonly'
     45             );
     46
     47             return $this->createTag(
     48                 'span',
     49                 $attributes,
     50                 (($this->value) ? nl2br(htmlentities($this->value, ENT_COMPAT, 'UTF-8')) : '<i>(' . _t('FormField.NONE', 'none') . ')</i>')
     51             );
     52         } else {
     53             $attributes = array(
     54                 'id' => $this->id(),
     55                 'class' => ($this->extraClass() ? $this->extraClass() : ''),
     56                 'name' => $this->name,
     57                 'rows' => $this->rows,
     58                 'cols' => $this->cols
     59             );
     60
     61             if($this->disabled) $attributes['disabled'] = 'disabled';
     62
     63             return $this->createTag('textarea', $attributes, htmlentities($this->value, ENT_COMPAT, 'UTF-8'));
     64         }
     65     }
     66
     67     /**
     68      * Performs a readonly transformation on this field. You should still be able
     69      * to copy from this field, and it should still send when you submit
     70      * the form it's attached to.
     71      * The element shouldn't be both disabled and readonly at the same time.
     72      */
     73     function performReadonlyTransformation() {
     74         $clone = clone $this;
     75         $clone->setReadonly(true);
     76         $clone->setDisabled(false);
     77         return $clone;
     78     }
     79
     80     /**
     81      * Performs a disabled transformation on this field. You shouldn't be able to
     82      * copy from this field, and it should not send any data when you submit the
     83      * form it's attached to.
     84      * The element shouldn't be both disabled and readonly at the same time.
     85      */
     86     function performDisabledTransformation() {
     87         $clone = clone $this;
     88         $clone->setDisabled(true);
     89         $clone->setReadonly(false);
     90         return $clone;
     91     }
     92
     93     function Type() {
     94         return parent::Type() . ( $this->readonly ? ' readonly' : '' );
     95     }
     96
     97     /**
     98      * Set the number of rows in the textarea
     99      *
    100      * @param int
    101      */
    102     function setRows($rows) {
    103         $this->rows = $rows;
    104     }
    105
    106     /**
    107      * Set the number of columns in the textarea
    108      *
    109      * @param int
    110      */
    111     function setColumns($cols) {
    112         $this->cols = $cols;
    113     }
    114 }
    115 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -