contact-form icon indicating copy to clipboard operation
contact-form copied to clipboard

Custom field validation

Open brandonkelly opened this issue 7 years ago • 12 comments

brandonkelly avatar Mar 14 '17 21:03 brandonkelly

+1

jnaklaas avatar Oct 19 '17 09:10 jnaklaas

Any updates on this? Or any known workaround to make fields required?

StijnCoolen avatar May 23 '18 08:05 StijnCoolen

+1

maxfrischknecht avatar Jun 19 '18 11:06 maxfrischknecht

++1

dannystarkDE avatar Jul 31 '18 06:07 dannystarkDE

+++1

umkasanki avatar Sep 03 '18 17:09 umkasanki

+1

jovanymezura avatar Dec 07 '18 16:12 jovanymezura

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!

amityweb avatar Dec 21 '18 11:12 amityweb

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;
  }

});

?>

shantamg avatar Mar 01 '19 23:03 shantamg

+1

LauraMontgomery avatar Jul 27 '19 18:07 LauraMontgomery

+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?

Friksel avatar Apr 15 '20 15:04 Friksel

+1

jolora avatar Sep 15 '20 10:09 jolora

+1

lennartlott avatar Feb 05 '21 15:02 lennartlott