Form elements (Fields)

As form element can be some class with interface Illuminate\Contracts\Support\Renderable. All form element should implement interface SleepingOwl\Admin\Contracts\FormElementInterface

Text

AdminFormElement::text(string $key, string $label = null)
// $key - Model key
// $label - Title

Number

AdminFormElement::number(string $key, string $label = null)
// $key - Model key
// $label - Title

Password

AdminFormElement::password(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->allowEmptyValue(); // If password set, skip "`"required" validation rule

$field->hashWithBcrypt(); // Before saving hash password with bcrypt
$field->hashWithMD5(); // Before saving hash password with md5
$field->hashWithSHA1(); // Before saving hash password with sha1

Hidden

AdminFormElement::hidden(string $key)
// $key - Model key

Date

AdminFormElement::date(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setFormat(string $fieldat); // Database date format
$field->setPickerFormat(string $fieldat); // Picker date format (Default: `sleeping_owl.dateFormat`)
$field->setCurrentDate(); // Set current date by default

Datetime

AdminFormElement::datetime(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setFormat(string $fieldat); // Database date format
$field->setPickerFormat(string $fieldat); // Picker date format (Default: `sleeping_owl.datetimeFormat`)
$field->setCurrentDate(); // Set current date by default
$field->setSeconds(bool); // Show seconds

Timestamp

AdminFormElement::timestamp(string $key, string $label = null)
// $key - Model key
// $label - Title

Time

AdminFormElement::time(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setFormat(string $fieldat); // Database date format
$field->setPickerFormat(string $fieldat); // Picker date format (Default: `sleeping_owl.timeFormat`)
$field->setCurrentDate(); // Set current date by default

File

File uploading via ajax and return relative file path

AdminFormElement::file(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setUploadPath(Closure $uploadPath); // Setting file upload path
// Example
$field->setUploadPath(function(\Illuminate\Http\UploadedFile $file) {
    // $file - file object
    return public_path('files');
});

$field->setUploadFileName(Closure $uploadPath); // Setting file name
// Example
$field->setUploadFileName(function(\Illuminate\Http\UploadedFile $file) {
    // $file - file object
    return $file->getClientOriginalName().'doctrine-meta'.$file->getClientOriginalExtension();
});

$field->maxSize(int $size); // Setting max file size
$field->minSize(int $size); // Setting min file size

Image

File uploading via ajax and return relative file path Use validation rule image

AdminFormElement::image(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setUploadPath(Closure $uploadPath); // Setting file upload path
// Example
$field->setUploadPath(function(\Illuminate\Http\UploadedFile $file) {
    // $file - file object
    return public_path('files');
});

$field->setUploadFileName(Closure $uploadPath); // Setting file name
// Example
$field->setUploadFileName(function(\Illuminate\Http\UploadedFile $file) {
    // $file - file object
    return $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();
});

$field->setUploadSettings(array settings); // Image resize settings 
// See http://image.intervention.io/
// Example
$field->setUploadSettings([
    'orientate' => [],
    'resize' => [1280, null, function ($constraint) {
        $constraint->upsize();
        $constraint->aspectRatio();
    }],
    'fit' => [200, 300, function ($constraint) {
        $constraint->upsize();
        $constraint->aspectRatio();
    }]
]);

$field->maxSize(int $size); // Setting max file size
$field->minSize(int $size); // Setting min file size

Images

File uploading via ajax and return relative file path Use validation rule image

This field by default set to model array of images

AdminFormElement::images(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

All method of image

$field->storeAsJson(); // Encode data to json
$field->storeAsComaSeparatedValue(); // Encode data to string with "," separator

Textarea

AdminFormElement::textarea(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setRows(int $rows); // Setting row numbers

Select

AdminFormElement::select(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

$field->setModelForOptions(string|\Illuminate\Database\Eloquent\Model $model, string $titleKey = null) // Setting model for options
$field->setDisplay(string $titleKey) // Setting model options title
$field->setFetchColumns(...$columns) // Setting model query select fields
$field->setLoadOptionsQueryPreparer(Closure $callback) // Modify query
// Example
$field->setLoadOptionsQueryPreparer(function($item, $query) {
	return $query->where('column', 'value')->were('owner_id', Auth::user()->id)
})

$field->setOptions(array $options) // Settings array of options

$field->setEnum(array $options) // Settings options enums

$field->nullable() // Allow blank
$field->exclude(array $keys) // Exclude options by key

MultiSelect

AdminFormElement::multiSelect(string $key, string $label = null)
// $key - Model key
// $label - Title

Methods

All method from Select

$field->taggable() // Allow select tags

Wysiwyg

AdminFormElement::wysiwyg(string $key, string $label = null, string $editor = null)
// $key - Model key
// $label - Title
// $editor - Editor key

Methods

$field->setFilteredValueToField(string $field) // Setting field key for compiled HTML value (For example if used markdown)
$field->disableFilter() // Disable compiling data to HTML (For example if used markdown)

$field->setEditor(string $editor) // Setting editor name (ckeditor, tinymce, simplemde, ...)
$field->setHeight(int $height) // Setting editor height
$field->setParameters(array $parameters) // Additionals params (Will be json encoded)

Ckeditor

Alias for wysiwyg

AdminFormElement::ckeditor(string $key, string $label = null)
// $key - Model key
// $label - Title

Checkbox

AdminFormElement::checkbox(string $key, string $label = null)
// $key - Model key
// $label - Title

Radio

All method from Select

AdminFormElement::radio(string $key, string $label = null)
// $key - Model key
// $label - Title

Html

AdminFormElement::html(string $html)

Custom

AdminFormElement::custom(Closure $callback = null)
// $callback - callback before saving

Methods

$field->setCallback(Closure $callback) // callback before saving
$field->setDisplay(Closure|string $display) // HTML for displaing

// Example
$field->setDisplay(function(Model $model) {
   return (string);
})

View

Extends Custom

AdminFormElement::view(string $view, array $data = [], Closure $callback = null)
// $view - view path
// $data - array of data for view (will be add Form model)
// $callback - callback before saving

Methods

$field->setCallback(Closure $callback) // callback before saving
$field->setView(string $path) // view path
$field->setData(array $data) // array of data for view (will be add Form model)

Upload

Field upload uses <input type="upload" />.

By add this field to form will be add html attribute enctype="multipart/form-data". You can add this attribute manually

return AdminForm::panel()
    ....
    ->setHtmlAttribute('enctype', 'multipart/form-data');

Use this trait KodiComponents\Support\Upload to work with this field,

Example

class User extends Model
{
    use \KodiComponents\Support\Upload;

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'image' => 'image', // or file | upload
    ];
}

Use cast file or upload for files, and image for images.

If cast is image, you can use http://image.intervention.io/ for resize.

/**
 * @return array
 */
public function getUploadSettings()
{
    return [
        'image' => [ // Model key
            // see http://image.intervention.io/api/fit
            'fit' => [300, 300, function ($constraint) {
                $constraint->upsize();
                $constraint->aspectRatio();
            }],
            // see http://image.intervention.io/api/crop
            'crop' => [300, 300],
            ...
        ],
        'image_small' => [
            ...
        ]
    ];
}

By default upload path is: public\storage\{table_name}\{field_name}\{file_name_hash:2chars}\{uniqid}.{ext}.

You can change default filename:

/**
 * @param UploadedFile $file
 *
 * @return string
 */
protected function getUploadFilename(\Illuminate\Http\UploadedFile $file)
{
    return md5($this->id).'.'.$file->getClientOriginalExtension();
}
/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'image' => 'image',
    'thumb' => 'image',
];

/**
 * @return array
 */
public function getUploadSettings()
{
    return [
        'image' => [
            'orientate' => [],
            'resize' => [1280, null, function ($constraint) {
                $constraint->upsize();
                $constraint->aspectRatio();
            }]
        ],
        'thumb' => [
            'orientate' => [],
            'fit' => [200, 300, function ($constraint) {
                $constraint->upsize();
                $constraint->aspectRatio();
            }]
        ]
    ];
}

/**
 * @param \Illuminate\Http\UploadedFile $file
 */
public function setUploadImageAttribute(\Illuminate\Http\UploadedFile $file = null)
{
    if (is_null($file)) {
        return;
    }

    foreach ($this->getUploadFields() as $field) {
        $this->{$field.'_file'} = $file;
    }
}

Model file fields

Validation

This field supports

AdminFormElement::upload('image', 'Image')->addValidationRule('image')

// or for file

AdminFormElement::upload('pdf', 'PDF')->addValidationRule('mime:pdf'),

!!!This trait uses getAttribute, mutateAttribute, setAttribute method!!!


API

addValidationRule

Adding validation rule

$field->addValidationRule(string $rule, string $message = null)

setValidationRules

Adding rules as array

required

Filed must be required

$field->required(string $message = null)

unique

Filed must be unique

$field->unique(string $message = null)

addValidationMessage

Change validation message for rule

$field->addValidationMessage(string $rule, string $message)

setView

Setting view template.

$field->setView(\Illuminate\View\View|string $view)

setPath

Setting model field key

$field->setPath(string $path)

setLabel

Setting title

$field->setName(string $label)

setDefaultValue

Setting default value

$field->setDefaultValue(mixed $defaultValue)

setHelpText

Setting help text

$field->setHelpText(string|\Illuminate\Contracts\Support\Htmlable $helpText)

setReadonly

Setting read only field

$field->setReadonly(bool $status)

setValueSkipped(bool|Closure $valueSkipped)

Sets flag to skip saving attribute value to model. By default false.

If setting value must be skipped validation rules will be applied but there will be no value in model.

For instance, when creating users you want to check if password and password_confirmation fields match, but you don’t need password_confirmation to be saved in model’s attributes:

AdminFormElement::text('password', 'Password')->required();
AdminFormElement::text('password_confirmation', 'Password confirmation')
    ->setValueSkipped(true)
    ->required()
    ->addValidationRule('same:password', 'Passwords must match!');

mutateValue

Mutate field value before form saving

$field->mutateValue(Closure $mutator)

Example

$field->mutateValue(function($value) {
    return bcrypt($value);
})