vendor/contao/core-bundle/src/Resources/contao/forms/FormTextField.php line 130

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Contao.
  4.  *
  5.  * (c) Leo Feyer
  6.  *
  7.  * @license LGPL-3.0-or-later
  8.  */
  9. namespace Contao;
  10. /**
  11.  * Class FormTextField
  12.  *
  13.  * @property string  $value
  14.  * @property string  $type
  15.  * @property integer $maxlength
  16.  * @property boolean $mandatory
  17.  * @property integer $min
  18.  * @property integer $max
  19.  * @property integer $step
  20.  * @property string  $placeholder
  21.  * @property boolean $hideInput
  22.  *
  23.  * @author Leo Feyer <https://github.com/leofeyer>
  24.  *
  25.  * @todo Rename to FormText in Contao 5.0
  26.  */
  27. class FormTextField extends Widget
  28. {
  29.     /**
  30.      * Submit user input
  31.      *
  32.      * @var boolean
  33.      */
  34.     protected $blnSubmitInput true;
  35.     /**
  36.      * Add a for attribute
  37.      *
  38.      * @var boolean
  39.      */
  40.     protected $blnForAttribute true;
  41.     /**
  42.      * Template
  43.      *
  44.      * @var string
  45.      *
  46.      * @todo Rename to form_text in Contao 5.0
  47.      */
  48.     protected $strTemplate 'form_textfield';
  49.     /**
  50.      * The CSS class prefix
  51.      *
  52.      * @var string
  53.      */
  54.     protected $strPrefix 'widget widget-text';
  55.     /**
  56.      * Add specific attributes
  57.      *
  58.      * @param string $strKey   The attribute key
  59.      * @param mixed  $varValue The attribute value
  60.      */
  61.     public function __set($strKey$varValue)
  62.     {
  63.         switch ($strKey)
  64.         {
  65.             case 'minlength':
  66.                 if ($varValue && $this->rgxp != 'digit' && $this->rgxp != 'natural')
  67.                 {
  68.                     $this->arrAttributes['minlength'] =  $varValue;
  69.                 }
  70.                 break;
  71.             case 'maxlength':
  72.                 if ($varValue && $this->rgxp != 'digit' && $this->rgxp != 'natural')
  73.                 {
  74.                     $this->arrAttributes['maxlength'] =  $varValue;
  75.                 }
  76.                 break;
  77.             case 'mandatory':
  78.                 if ($varValue)
  79.                 {
  80.                     $this->arrAttributes['required'] = 'required';
  81.                 }
  82.                 else
  83.                 {
  84.                     unset($this->arrAttributes['required']);
  85.                 }
  86.                 parent::__set($strKey$varValue);
  87.                 break;
  88.             case 'min':
  89.             case 'minval':
  90.                 if ($this->rgxp == 'digit' || $this->rgxp == 'natural')
  91.                 {
  92.                     $this->arrAttributes['min'] = $varValue;
  93.                 }
  94.                 break;
  95.             case 'max':
  96.             case 'maxval':
  97.                 if ($this->rgxp == 'digit' || $this->rgxp == 'natural')
  98.                 {
  99.                     $this->arrAttributes['max'] = $varValue;
  100.                 }
  101.                 break;
  102.             case 'step':
  103.                 if ($varValue && $this->type == 'number')
  104.                 {
  105.                     $this->arrAttributes[$strKey] = $varValue;
  106.                 }
  107.                 else
  108.                 {
  109.                     unset($this->arrAttributes[$strKey]);
  110.                 }
  111.                 break;
  112.             case 'placeholder':
  113.                 $this->arrAttributes[$strKey] = $varValue;
  114.                 break;
  115.             default:
  116.                 parent::__set($strKey$varValue);
  117.                 break;
  118.         }
  119.     }
  120.     /**
  121.      * Return a parameter
  122.      *
  123.      * @param string $strKey The parameter key
  124.      *
  125.      * @return mixed The parameter value
  126.      */
  127.     public function __get($strKey)
  128.     {
  129.         switch ($strKey)
  130.         {
  131.             case 'value':
  132.                 // Hide the Punycode format (see #2750)
  133.                 if ($this->rgxp == 'url')
  134.                 {
  135.                     try
  136.                     {
  137.                         return Idna::decodeUrl($this->varValue);
  138.                     }
  139.                     catch (\InvalidArgumentException $e)
  140.                     {
  141.                         return $this->varValue;
  142.                     }
  143.                 }
  144.                 if ($this->rgxp == 'email' || $this->rgxp == 'friendly')
  145.                 {
  146.                     return Idna::decodeEmail($this->varValue);
  147.                 }
  148.                 return $this->varValue;
  149.             case 'type':
  150.                 if ($this->hideInput)
  151.                 {
  152.                     return 'password';
  153.                 }
  154.                 // Use the HTML5 types (see #4138) but not the date, time and datetime types (see #5918)
  155.                 switch ($this->rgxp)
  156.                 {
  157.                     case 'digit':
  158.                         // Allow floats (see #7257)
  159.                         if (!isset($this->arrAttributes['step']))
  160.                         {
  161.                             $this->addAttribute('step''any');
  162.                         }
  163.                         // no break
  164.                     case 'natural':
  165.                         return 'number';
  166.                     case 'phone':
  167.                         return 'tel';
  168.                     case 'email':
  169.                         return 'email';
  170.                     case 'url':
  171.                         return 'url';
  172.                 }
  173.                 return 'text';
  174.             case 'min':
  175.             case 'minval':
  176.                 if ($this->rgxp == 'digit')
  177.                 {
  178.                     return $this->arrAttributes['min'];
  179.                 }
  180.                 return parent::__get($strKey);
  181.             case 'max':
  182.             case 'maxval':
  183.                 if ($this->rgxp == 'digit')
  184.                 {
  185.                     return $this->arrAttributes['max'];
  186.                 }
  187.                 return parent::__get($strKey);
  188.             default:
  189.                 return parent::__get($strKey);
  190.         }
  191.     }
  192.     /**
  193.      * Trim the values
  194.      *
  195.      * @param mixed $varInput The user input
  196.      *
  197.      * @return mixed The validated user input
  198.      */
  199.     protected function validator($varInput)
  200.     {
  201.         if (\is_array($varInput))
  202.         {
  203.             return parent::validator($varInput);
  204.         }
  205.         // Convert to Punycode format (see #5571)
  206.         if ($this->rgxp == 'url')
  207.         {
  208.             try
  209.             {
  210.                 $varInput Idna::encodeUrl($varInput);
  211.             }
  212.             catch (\InvalidArgumentException $e)
  213.             {
  214.             }
  215.         }
  216.         elseif ($this->rgxp == 'email' || $this->rgxp == 'friendly')
  217.         {
  218.             $varInput Idna::encodeEmail($varInput);
  219.         }
  220.         return parent::validator($varInput);
  221.     }
  222.     /**
  223.      * Generate the widget and return it as string
  224.      *
  225.      * @return string The widget markup
  226.      */
  227.     public function generate()
  228.     {
  229.         return sprintf(
  230.             '<input type="%s" name="%s" id="ctrl_%s" class="text%s%s" value="%s"%s%s',
  231.             $this->type,
  232.             $this->strName,
  233.             $this->strId,
  234.             ($this->hideInput ' password' ''),
  235.             ($this->strClass ' ' $this->strClass ''),
  236.             StringUtil::specialchars($this->value),
  237.             $this->getAttributes(),
  238.             $this->strTagEnding
  239.         );
  240.     }
  241. }
  242. class_alias(FormTextField::class, 'FormTextField');