123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- namespace Dcat\Admin\Form\Field;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form\Field;
- class Text extends Field
- {
- use PlainInput;
- /**
- * Render this filed.
- *
- * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
- */
- public function render()
- {
- $this->initPlainInput();
- $this->prepend('<i class="ti-pencil"></i>')
- ->defaultAttribute('type', 'text')
- ->defaultAttribute('id', $this->id)
- ->defaultAttribute('name', $this->getElementName())
- ->defaultAttribute('value', old($this->column, $this->value()))
- ->defaultAttribute('class', 'form-control '.$this->getElementClassString())
- ->defaultAttribute('placeholder', $this->getPlaceholder());
- $this->addVariables([
- 'prepend' => $this->prepend,
- 'append' => $this->append,
- ]);
- return parent::render();
- }
- /**
- * Set input type.
- *
- * @param string $type
- * @return $this
- */
- public function type(string $type)
- {
- return $this->attribute('type', $type);
- }
- /**
- * Set "data-match" attribute.
- *
- * @see http://1000hz.github.io/bootstrap-validator/
- *
- * @param string $field
- * @param string $error
- * @return $this
- */
- public function confirm(string $field, ?string $error = null, ?string $fieldSelector = null)
- {
- if (! $fieldSelector && $this->form) {
- $column = $this->form->field($field);
- $column->rules('confirmed');
- $fieldSelector = '#'.$column->getElementId();
- }
- $attributes = [
- 'data-match' => $fieldSelector,
- 'data-match-error' => str_replace(':attribute', $field, $error ?: trans('admin.validation.match'))
- ];
- return $this->attribute($attributes);
- }
- /**
- * @param int $length
- * @param string|null $error
- * @return $this
- */
- public function minLength(int $length, ?string $error = null)
- {
- $this->rules('nullable|min:'.$length);
- return $this->attribute([
- 'data-minlength' => $length,
- 'data-minlength-error' => str_replace(
- [':attribute', ':min'],
- [$this->column, $length],
- $error ?: trans('admin.validation.minlength')
- ),
- ]);
- }
- /**
- * @param int $length
- * @param string|null $error
- * @return $this
- */
- public function maxLength(int $length, ?string $error = null)
- {
- Admin::script(
- <<<'JS'
- LA.extendValidator('maxlength', function ($el) {
- return $el.val().length > $el.attr('data-maxlength');
- });
- JS
- );
- $this->rules('max:'.$length);
- return $this->attribute([
- 'data-maxlength' => $length,
- 'data-maxlength-error' => str_replace(
- [':attribute', ':max'],
- [$this->column, $length],
- $error ?: trans('admin.validation.maxlength')
- ),
- ]);
- }
- /**
- * Set error messages for individual form field.
- *
- * @see http://1000hz.github.io/bootstrap-validator/
- *
- * @param string $error
- * @return $this
- */
- public function validationError(string $error)
- {
- return $this->attribute('data-error', $error);
- }
- /**
- * Add inputmask to an elements.
- *
- * @param array $options
- *
- * @return $this
- */
- public function inputmask($options)
- {
- $options = $this->jsonEncodeOptions($options);
- $this->script = "$('{$this->getElementClassSelector()}').inputmask($options);";
- return $this;
- }
- /**
- * Encode options to Json.
- *
- * @param array $options
- *
- * @return $json
- */
- protected function jsonEncodeOptions($options)
- {
- $data = $this->prepareOptions($options);
- $json = json_encode($data['options']);
- $json = str_replace($data['toReplace'], $data['original'], $json);
- return $json;
- }
- /**
- * Prepare options.
- *
- * @param array $options
- *
- * @return array
- */
- protected function prepareOptions($options)
- {
- $original = [];
- $toReplace = [];
- foreach ($options as $key => &$value) {
- if (is_array($value)) {
- $subArray = $this->prepareOptions($value);
- $value = $subArray['options'];
- $original = array_merge($original, $subArray['original']);
- $toReplace = array_merge($toReplace, $subArray['toReplace']);
- } elseif (preg_match('/function.*?/', $value)) {
- $original[] = $value;
- $value = "%{$key}%";
- $toReplace[] = "\"{$value}\"";
- }
- }
- return compact('original', 'toReplace', 'options');
- }
- /**
- * Add datalist element to Text input.
- *
- * @param array $entries
- *
- * @return $this
- */
- public function datalist($entries = [])
- {
- $this->defaultAttribute('list', "list-{$this->id}");
- $datalist = "<datalist id=\"list-{$this->id}\">";
- foreach ($entries as $k => $v) {
- $datalist .= "<option value=\"{$k}\">{$v}</option>";
- }
- $datalist .= '</datalist>';
- return $this->append($datalist);
- }
- }
|