Text.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. *
  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. * @return $this
  69. */
  70. public function minLength(int $length, ?string $error = null)
  71. {
  72. $this->rules('nullable|min:'.$length);
  73. return $this->attribute([
  74. 'data-minlength' => $length,
  75. 'data-minlength-error' => str_replace(
  76. [':attribute', ':min'],
  77. [$this->label, $length],
  78. $error ?: trans('admin.validation.minlength')
  79. ),
  80. ]);
  81. }
  82. /**
  83. * @param int $length
  84. * @param string|null $error
  85. *
  86. * @return $this
  87. */
  88. public function maxLength(int $length, ?string $error = null)
  89. {
  90. Admin::script(
  91. <<<'JS'
  92. LA.extendValidator('maxlength', function ($el) {
  93. return $el.val().length > $el.attr('data-maxlength');
  94. });
  95. JS
  96. );
  97. $this->rules('max:'.$length);
  98. return $this->attribute([
  99. 'data-maxlength' => $length,
  100. 'data-maxlength-error' => str_replace(
  101. [':attribute', ':max'],
  102. [$this->label, $length],
  103. $error ?: trans('admin.validation.maxlength')
  104. ),
  105. ]);
  106. }
  107. /**
  108. * Add inputmask to an elements.
  109. *
  110. * @param array $options
  111. *
  112. * @return $this
  113. */
  114. public function inputmask($options)
  115. {
  116. $options = $this->jsonEncodeOptions($options);
  117. $this->script = "$('{$this->getElementClassSelector()}').inputmask($options);";
  118. return $this;
  119. }
  120. /**
  121. * Encode options to Json.
  122. *
  123. * @param array $options
  124. *
  125. * @return $json
  126. */
  127. protected function jsonEncodeOptions($options)
  128. {
  129. $data = $this->prepareOptions($options);
  130. $json = json_encode($data['options']);
  131. $json = str_replace($data['toReplace'], $data['original'], $json);
  132. return $json;
  133. }
  134. /**
  135. * Prepare options.
  136. *
  137. * @param array $options
  138. *
  139. * @return array
  140. */
  141. protected function prepareOptions($options)
  142. {
  143. $original = [];
  144. $toReplace = [];
  145. foreach ($options as $key => &$value) {
  146. if (is_array($value)) {
  147. $subArray = $this->prepareOptions($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. $this->defaultAttribute('list', "list-{$this->id}");
  169. $datalist = "<datalist id=\"list-{$this->id}\">";
  170. foreach ($entries as $k => $v) {
  171. $value = is_string($k) ? "value=\"{$k}\"" : '';
  172. $datalist .= "<option {$value}>{$v}</option>";
  173. }
  174. $datalist .= '</datalist>';
  175. Admin::script("$('#list-{$this->id}').parent().hide()");
  176. return $this->append($datalist);
  177. }
  178. }