Contact Form Component
Processes a contact form submission using a component class.
{{ sprig('ContactForm', {
message: 'Say hello',
}) }}
{#--- ContactForm.php ---#}
<?php
namespace sprig\components;
use putyourlightson\sprig\base\Component;
class ContactForm extends Component
{
public $success;
public $error;
public $email;
public $message;
protected $template = '_components/contact-form';
public function send()
{
$this->success = SomeEmailApi::send([
'email' => $this->email,
'message' => $this->message,
);
if (!$this->success) {
$this->error = SomeEmailApi::getError();
}
}
}
{#-- _components/contact-form --#}
{% if success %}
Thank you for getting in touch!
{% else %}
{% if error %}
<p class="error">{{ error }}</p>
{% endif %}
<form sprig s-action="send">
<input type="email" name="email" value="{{ email }}">
<textarea name="message">{{ message }}</textarea>
</form>
{% endif %}