contact-form
contact-form copied to clipboard
Custom field validation
+1
Any updates on this? Or any known workaround to make fields required?
+1
++1
+++1
+1
Is there an update on this issue? I find it a bit strange no documentation on how to validate a custom field. Going to be necessary in most forms in custom fields to require something like a phone number. I dont like relying on HTML5 required attribute, nor Javascript, they are not fool proof, so server side is best!
In my particular case I need users to agree to the privacy policy for EU GDPR requirements, so quite important its fool proof!
Thanks a lot!
Here's what I have figured out (Craft 3):
Put this in config/contact-form.php
:
<?php
use craft\contactform\models\Submission;
use yii\base\Event;
use yii\base\ModelEvent;
Event::on(Submission::class, Submission::EVENT_BEFORE_VALIDATE, function(ModelEvent $e) {
$submission = $e->sender;
$message = Craft::$app->getRequest()->getParam('message');
if ($message['Custom Field'] != 'perfect response') {
$submission->addError('custom_field', 'This did not validate');
$e->isValid = false;
}
});
?>
+1
+1 The initial issue message is cleared, but if I am not missing something I can't find a solution for showing error messages on custom (message-) fields in the README still to do this without using the event workaround. Being able to validate custom fields like phone number and company feels like pretty basic functionality to me most contact pages have.
Any updates on this?
+1
+1