Skip to content

Example form

tanthammar edited this page Sep 27, 2020 · 13 revisions

The Field class is used to declare your form fields.

public $photos;

public function fields()
{
    $brand_options = Brand::orderBy('name')->get()->pluck('id', 'name')->all();

    return [
        Select::make('Brand', 'brand_id')->options($brand_options)->help('Please select a brand.'),
        Input::make('Name')->rules(['required', Rule::unique('cars', 'name')->ignore($this->model->id)]),
        Select::make('Color')->options(['Red', 'Green', 'Blue']),
        Repeater::make('Owners')->fields([
            Input::make('Name')->placeholder('Name')->rules('required'),
            Input::make('Phone')->type('tel')->placeholder('Phone')->rules('required'),
        ])->sortable()->help('Click plus to add an Owner'),
        Checkbox::make('Insurable')->placeholder('Is the car insurable?')->rules('accepted'),
        Radio::make('Fuel Type')->options(['Gas', 'Diesel', 'Electric'])->default('Diesel'),
        Checkboxes::make('Features')->options(['Stereo', 'Bluetooth', 'Navigation'])->rules('required|min:2'),
        Textarea::make('Description'),
        FileUpload::make('Photos')->multiple()->rules('nullable|mimes:png,jpg,jpeg,gif,tiff|max:1024')->accept("image/*"),
    ];
}

KeyVal example

public function fields()
{
 return [
    Keyval::make(trans('fields.settings').' '.trans('fields.upfront'), 'upfronts')->fields([
        Checkbox::make(trans('fields.upfront_auto'), 'auto')
            ->placeholder(trans('fields.auto'))
            ->help(trans('fields.upfront_auto_hint'))
            ->rules('required|boolean')
            ->colspan(12),
        Input::make(trans('fields.amount'), 'amount')
            ->type('number')
            ->prefix('kr')
            ->placeholder(300)
            ->help(trans('fields.upfront_amount_hint'))
            ->rules('nullable|numeric|integer|min:10')
            ->colspan(6),
        Input::make(trans('fields.percent'),'amount_percent')
            ->type('number')
            ->prefix('%')
            ->placeholder(50)
            ->help(trans('fields.percent_hint') . ' ' . trans('fields.upfront_percent_hint'))
            ->rules('nullable|numeric|integer|between:1,100')
            ->colspan(6),
    ])->help(trans('fields.upfront_help')),
 ];
}
Clone this wiki locally