Input examples

protected function fields()
{
    return [        
        $this->name(),
        $this->text(),
        $this->password(),
        $this->passwordConfirmation(),
        $this->url(),
        $this->number(),
        $this->tel(),
        $this->email(),
        $this->dateTimeLocal(),
        $this->select(),
        $this->checkbox(),
        $this->checkboxes(),
        $this->radio(),
        $this->textarea(),
        $this->range(),        
    ];
}

protected function number()
{
    return Input::make('Number')
        ->type('number')
        ->prefix('#')
        ->step(2)
        ->min(5)
        ->max(10)
        ->rules('required|integer');
}

protected function dateTimeLocal()
{
    return Input::make('Date Time Local', 'birthday')
        ->type('datetime-local')
        ->step(7)
        ->min('2020-09-15')
        ->max('2021-12-31')
        ->default(now()->toDateTimeLocalString('minute')) //or now()->format('Y-m-d\TH:i')
        ->rules('required');
}

protected function range()
{
    return Range::make('Range')
        ->default(3)
        ->min(1)
        ->max('100')
        ->step(1)
        ->rules('required|integer');
}

protected function tel()
{
    return Input::make('Phone')
        ->type('tel')
        ->prefix('phone:')
        ->placeholder('### ### ### ###')
        ->rules('required|alpha_dash');
}

protected function email()
{
    //example: set the validation based on if the model exists in optional model binding
    $email_rule = optional($this->model)->exists
        ? ['required', 'email', Rule::unique('users', 'email')->ignore($this->model->id)]
        : 'required|email|unique:users,email';
    return Input::make('Email')
        ->fieldWidth('w-full sm:max-w-sm')
        ->type('email')
        ->prefix('@')
        ->rules($email_rule);
}

protected function url()
{
    return Input::make('Url', 'url')
        ->type('url')
        ->prefix('https://')
        ->rules('required|active_url');
}

protected function text()
{
    return Input::make('Text')
        ->help('Some help text')
        ->icon('file-user')//requires blade ui kit icons
        ->rules('required|string');
}

protected function textarea()
{
    return Textarea::make('Textarea')
        ->placeholder('Tell me a story')
        ->rows(3)
        ->rules('required');
}

protected function checkbox()
{
    return Checkbox::make('Checkbox') //cast to boolean on model
        ->placeholder('The checkbox label')
        ->rules('boolean')
        ->default(1); //equivalent to checked
}

/**
 * Checkboxes, associative array
 * @return Checkboxes
 */
protected function checkboxes()
{
    $options = ['Wifi' => 'wf', 'Bluetooth' => 'bl', 'Ethernet' => 'eth'];
    return Checkboxes::make('Checkboxes')
        ->options($options)
        ->rules(['array', Rule::in(array_values($options))])
        ->default(['wf']);
}

/**
 * Radio, associative array
 * @return Radio
 */
protected function radio()
{
    $options = ['Wifi' => 'wf', 'Bluetooth' => 'bl', 'Ethernet' => 'eth'];
    return Radio::make('Radio')
        ->options($options)
        ->rules([Rule::in(array_values($options))])
        ->default('wf');
}

protected function select()
{
    $options = Brand::pluck('id', 'name')->toArray();
    return Select::make('Brand', 'brand_id')
        ->options($options)
        ->placeholder('Please select a brand.')
        ->rules(['nullable', Rule::in(array_values($options))]);
}

protected function name()
{
    return Input::make('Name')
        ->rules([
            'required', 
            Rule::unique('cars', 'name')->ignore($this->model->id)
    ]);
}

protected function password()
{
    return Input::make('Password')
        ->type('password')
        ->rules('required|alpha_dash|min:8');
}

protected function passwordConfirmation()
{
    return Input::make('Repeat password', 'password_confirmation')
        ->type('password')
        ->custom()
        ->rules('required|same:form_data.password');
}

Last updated