Text.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace Dcat\Admin\Form\Field;
  3. use Dcat\Admin\Admin;
  4. use Dcat\Admin\Form\Field;
  5. class Text extends Field
  6. {
  7. use PlainInput;
  8. /**
  9. * Render this filed.
  10. *
  11. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  12. */
  13. public function render()
  14. {
  15. $this->initPlainInput();
  16. $this->prepend('<i class="ti-pencil"></i>')
  17. ->defaultAttribute('type', 'text')
  18. ->defaultAttribute('id', $this->id)
  19. ->defaultAttribute('name', $this->getElementName())
  20. ->defaultAttribute('value', old($this->column, $this->value()))
  21. ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
  22. ->defaultAttribute('placeholder', $this->getPlaceholder());
  23. $this->addVariables([
  24. 'prepend' => $this->prepend,
  25. 'append' => $this->append,
  26. ]);
  27. return parent::render();
  28. }
  29. /**
  30. * Set input type.
  31. *
  32. * @param string $type
  33. * @return $this
  34. */
  35. public function type(string $type)
  36. {
  37. return $this->attribute('type', $type);
  38. }
  39. /**
  40. * Set "data-match" attribute.
  41. *
  42. * @see http://1000hz.github.io/bootstrap-validator/
  43. *
  44. * @param string|Field $field
  45. * @param string $error
  46. * @return $this
  47. */
  48. public function same($field, ?string $error = null)
  49. {
  50. $field = $field instanceof Field ? $field : $this->form->field($field);
  51. $name = $field->column();
  52. if ($name.'_confirmation' === $this->column) {
  53. $field->rules('confirmed');
  54. } else {
  55. $this->rules('nullable|same:'.$name);
  56. }
  57. $attributes = [
  58. 'data-match' => '#'.$field->getElementId(),
  59. 'data-match-error' => str_replace([':attribute', ':other'], [$this->column, $name], $error ?: trans('admin.validation.match'))
  60. ];
  61. return $this->attribute($attributes);
  62. }
  63. /**
  64. * @param int $length
  65. * @param string|null $error
  66. * @return $this
  67. */
  68. public function minLength(int $length, ?string $error = null)
  69. {
  70. $this->rules('nullable|min:'.$length);
  71. return $this->attribute([
  72. 'data-minlength' => $length,
  73. 'data-minlength-error' => str_replace(
  74. [':attribute', ':min'],
  75. [$this->column, $length],
  76. $error ?: trans('admin.validation.minlength')
  77. ),
  78. ]);
  79. }
  80. /**
  81. * @param int $length
  82. * @param string|null $error
  83. * @return $this
  84. */
  85. public function maxLength(int $length, ?string $error = null)
  86. {
  87. Admin::script(
  88. <<<'JS'
  89. LA.extendValidator('maxlength', function ($el) {
  90. return $el.val().length > $el.attr('data-maxlength');
  91. });
  92. JS
  93. );
  94. $this->rules('max:'.$length);
  95. return $this->attribute([
  96. 'data-maxlength' => $length,
  97. 'data-maxlength-error' => str_replace(
  98. [':attribute', ':max'],
  99. [$this->column, $length],
  100. $error ?: trans('admin.validation.maxlength')
  101. ),
  102. ]);
  103. }
  104. /**
  105. * Add inputmask to an elements.
  106. *
  107. * @param array $options
  108. *
  109. * @return $this
  110. */
  111. public function inputmask($options)
  112. {
  113. $options = $this->jsonEncodeOptions($options);
  114. $this->script = "$('{$this->getElementClassSelector()}').inputmask($options);";
  115. return $this;
  116. }
  117. /**
  118. * Encode options to Json.
  119. *
  120. * @param array $options
  121. *
  122. * @return $json
  123. */
  124. protected function jsonEncodeOptions($options)
  125. {
  126. $data = $this->prepareOptions($options);
  127. $json = json_encode($data['options']);
  128. $json = str_replace($data['toReplace'], $data['original'], $json);
  129. return $json;
  130. }
  131. /**
  132. * Prepare options.
  133. *
  134. * @param array $options
  135. *
  136. * @return array
  137. */
  138. protected function prepareOptions($options)
  139. {
  140. $original = [];
  141. $toReplace = [];
  142. foreach ($options as $key => &$value) {
  143. if (is_array($value)) {
  144. $subArray = $this->prepareOptions($value);
  145. $value = $subArray['options'];
  146. $original = array_merge($original, $subArray['original']);
  147. $toReplace = array_merge($toReplace, $subArray['toReplace']);
  148. } elseif (preg_match('/function.*?/', $value)) {
  149. $original[] = $value;
  150. $value = "%{$key}%";
  151. $toReplace[] = "\"{$value}\"";
  152. }
  153. }
  154. return compact('original', 'toReplace', 'options');
  155. }
  156. /**
  157. * Add datalist element to Text input.
  158. *
  159. * @param array $entries
  160. *
  161. * @return $this
  162. */
  163. public function datalist($entries = [])
  164. {
  165. $this->defaultAttribute('list', "list-{$this->id}");
  166. $datalist = "<datalist id=\"list-{$this->id}\">";
  167. foreach ($entries as $k => $v) {
  168. $value = is_string($k) ? "value=\"{$k}\"" : '';
  169. $datalist .= "<option {$value}>{$v}</option>";
  170. }
  171. $datalist .= '</datalist>';
  172. Admin::script("$('#list-{$this->id}').parent().hide()");
  173. return $this->append($datalist);
  174. }
  175. }