Text.php 5.3 KB

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