Last active
August 17, 2022 09:01
-
-
Save jacksleight/04fdc8c1f8d827313a68261ac94f34fa to your computer and use it in GitHub Desktop.
`statamic_form()` function for Blade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Statamic\Tags\Loader; | |
use Statamic\View\Antlers\Parser; | |
if (! function_exists('statamic_form')) { | |
function statamic_form($handle, $params = []) | |
{ | |
$form = Form::find($handle); | |
$html = app(Loader::class)->load('form', [ | |
'parser' => app(Parser::class), | |
'params' => $params, | |
'content' => '', | |
'context' => [], | |
'tag' => "form:{$handle}", | |
'tag_method' => $handle, | |
])->$handle(); | |
preg_match('/^<form (.*?)>(.*?)<\/form>$/is', $html, $match); | |
return [ | |
'handle' => $handle, | |
'attrs' => $match[1], | |
'params' => $match[2], | |
'errors' => optional(session()->get('errors'))->getBag("form.{$handle}"), | |
'success' => session("form.{$handle}.success"), | |
'fields' => $form->fields(), | |
'honeypot' => $form->honeypot(), | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@php | |
$form = statamic_form('contact', [ | |
'redirect' => url('thank-you'), | |
]); | |
@endphp | |
<div> | |
@if ($form['success']) | |
<x-success :message="$form['success']" /> | |
@else | |
<x-errors :errors="$form['errors']" /> | |
<form {!! $form['attrs'] !!}> | |
<div class="space-y-4"> | |
@foreach ($form['fields'] as $field) | |
<label> | |
{{ $field->display() }} | |
</label> | |
@if ($field->type() === 'text') | |
<input name="{{ $field->handle() }}" type="text"> | |
@elseif ($field->type() === 'select') | |
<select name="{{ $field->handle() }}"> | |
@foreach ($field->get('options') as $option) | |
{{-- ... --}} | |
@endforeach | |
</select> | |
@else | |
{{-- ... --}} | |
@endif | |
@endforeach | |
<button>Send Message</button> | |
</div> | |
{!! $form['params'] !!} | |
<input name="{!! $form['honeypot'] !!}"> | |
</form> | |
@endif | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The old values seem to work, so switching to the now native function.
Thanks a lot for all your work!!