Text.php 5.2 KB

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