Text.php 5.3 KB

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