Skip to content

Instantly share code, notes, and snippets.

@pepebe
Last active January 13, 2021 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepebe/da5d26482479ab36719d8ef03b525641 to your computer and use it in GitHub Desktop.
Save pepebe/da5d26482479ab36719d8ef03b525641 to your computer and use it in GitHub Desktop.
Simple MODX Spamfilter
<?php
/*
Really primitive, but oddly efficient, anti-spambot validator for formit
1. Add weekday selectfield:
<div class="form-group [[!+fi.error.weekday:notempty=`has-error`]]">
<label for="weekday" class="col-sm-3 control-label">I'm not a robot! <sup>*</sup></label>
<div class="col-sm-9">
<select class="form-control" name="weekday" required>
<option value="">What day is it?</option>
<option value="Monday" [[!+fi.wochentag:FormItIsSelected=`Monday`]] >Monday</option>
<option value="Tuesday" [[!+fi.wochentag:FormItIsSelected=`Tuesday`]] >Tuesday</option>
<option value="Wednesday" [[!+fi.wochentag:FormItIsSelected=`Wednesday`]] >Wednesday</option>
<option value="Thursday" [[!+fi.wochentag:FormItIsSelected=`Thursday`]] >Thursday</option>
<option value="Friday" [[!+fi.wochentag:FormItIsSelected=`Friday`]] >Friday</option>
<option value="Saturday" [[!+fi.wochentag:FormItIsSelected=`Saturday`]] >Saturday</option>
<option value="Sunday" [[!+fi.wochentag:FormItIsSelected=`Sunday`]] >Sunday</option>
</select>
<span class="help-block">[[!+fi.error.weekday]]</span>
</div>
</div>
2. Add formit parameters:
&customValidators=`checkWeekday`
&validate=`message:required,
weekday:checkWeekday:required,
email:email:required,
name:required`
*/
$weekdays = array(
'Sunday' => 0,
'Monday' => 1,
'Tuesday' => 2,
'Wednesday' => 3,
'Thursday' => 4,
'Friday' => 5,
'Saturday' => 6
);
if(!array_key_exists($value, $weekdays)){
/* Should only be possible if something messed with the selectfield */
$validator->addError('weekday','Please check your input');
return false;
}
$weekday = $value;
$w = date('w');
if($weekdays[$weekday] == $w) {
return true;
}
else {
$validator->addError('weekday',"Today isn't <strong>" . $weekday . "</strong>. <br>Please check your calendar");
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment