Text.php 5.5 KB

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