Skip to content

Commit

Permalink
feat: add form content blocks (#321)
Browse files Browse the repository at this point in the history
* feat: add form content blocks

* fix: validation
  • Loading branch information
andreiio authored Apr 20, 2023
1 parent db5d428 commit 7e89c50
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 2 deletions.
4 changes: 3 additions & 1 deletion app/Http/Controllers/Front/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function submit(string $locale, FormSubmissionRequest $request, Form $for
{
$attributes = $request->validated();

$data = $form->blocks
$data = $form->blocks()
->onlyFields()
->get()
->map(function (FormField $field) use ($attributes) {
$value = $attributes[$field->name] ?? null;

Expand Down
4 changes: 3 additions & 1 deletion app/Http/Requests/Front/FormSubmissionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class FormSubmissionRequest extends BaseRequest
*/
public function rules(): array
{
return $this->form->blocks
return $this->form->blocks()
->onlyFields()
->get()
->mapWithKeys(function (Block $field) {
$rules = [
$field->checkbox('required') ? 'required' : 'nullable',
Expand Down
6 changes: 6 additions & 0 deletions app/Models/FormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Models;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Collection;

class FormField extends Block
Expand Down Expand Up @@ -36,4 +37,9 @@ public function options(string $field = 'options'): Collection
return collect(preg_split('/\r\n|\r|\n/', $rawOptions))
->filter();
}

public function scopeOnlyFields(Builder $query): Builder
{
return $query->whereNotIn('type', ['content', 'divider']);
}
}
25 changes: 25 additions & 0 deletions resources/js/components/Blocks/Form/Content.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<template>
<localized-field
field="form-input"
:label="$t('field.title')"
v-model="content.title"
/>

<localized-field
field="form-editor"
:label="$t('field.text')"
v-model="content.text"
/>
</template>

<script>
import { defineFormBlock } from '@/helpers';
export default defineFormBlock({
type: 'content',
fields: {
title: Object,
text: Object,
},
});
</script>
15 changes: 15 additions & 0 deletions resources/js/components/Blocks/Form/Divider.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<template>
<form-color-picker :label="$t('field.color')" v-model="content.color" />
</template>

<script>
import { defineFormBlock } from '@/helpers';
export default defineFormBlock({
type: 'divider',
icon: 'Editor/separator',
fields: {
color: String,
},
});
</script>
5 changes: 5 additions & 0 deletions resources/views/components/blocks/form/content.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<x-blocks._title :title="$block->translatedInput('title')" />

<div class="prose prose-blue md:prose-lg">
{!! $block->translatedInput('text') !!}
</div>
7 changes: 7 additions & 0 deletions resources/views/components/blocks/form/divider.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@php
$color = $block->input('color');
@endphp

<hr
@if (is_null($color)) class="border-gray-300"
@else style="border-color: {{ $color }}" @endif>

0 comments on commit 7e89c50

Please sign in to comment.