laravel-admin必填项加星号

  • Jesse
  • 2018-08-03 15:27:27
  • 7498

1.文件修改vendor/encore/laravel-admin/src/Form/Field.php

新增:

/**
 * Form element label classes.
 *
 * @var array
 */
protected $elementLabelClass = [];
/**
 * Add the element label class.
 *
 * @param $class
 *
 * @return $this
 */
public function addElementLabelClass($class)
{
    if (is_array($class) || is_string($class)) {
        $this->elementLabelClass = array_merge($this->elementLabelClass, (array) $class);

        $this->elementLabelClass = array_unique($this->elementLabelClass);
    }

    return $this;
}

修改方法(约740行左右)getViewElementClasses

public function getViewElementClasses()
{
    if ($this->horizontal) {
        return [
            'label'      => "col-sm-{$this->width['label']} "  . implode(' ', $this->elementLabelClass),
            'field'      => "col-sm-{$this->width['field']}",
            'form-group' => 'form-group ',
        ];
    }

    return ['label' => '', 'field' => '', 'form-group' => ''];
}


2.css修改,在public/vendor/laravel-admin/laravel-admin/laravel-admin.css, 新增

.form-group > .must-input.control-label:after {
    content: "*";
    color: red;
}


3.使用

$form->text('name', '名称')->addElementLabelClass('must-input');


本文出自 showcj,转载时请注明出处及相应链接。