mirror of
https://github.com/silverstripe/silverstripe-mathspamprotection
synced 2024-10-22 08:05:52 +02:00
Inserted missing validator notify callback object
This commit is contained in:
parent
dcccf5dd91
commit
0d88c9da19
@ -2,7 +2,7 @@
|
||||
|
||||
/**
|
||||
* {@link FormField} for adding an optional maths protection question to a form.
|
||||
*
|
||||
*
|
||||
* @package mathspamprotection
|
||||
*/
|
||||
|
||||
@ -12,7 +12,7 @@ class MathSpamProtectorField extends SpamProtectorField {
|
||||
* @var bool If MathSpamProtection is enabled
|
||||
*/
|
||||
private static $enabled = true;
|
||||
|
||||
|
||||
/**
|
||||
* Outputs the field HTML to the the web browser
|
||||
*
|
||||
@ -31,37 +31,47 @@ class MathSpamProtectorField extends SpamProtectorField {
|
||||
'maxlength' => ($this->maxLength) ? $this->maxLength : null,
|
||||
'size' => ($this->maxLength) ? min( $this->maxLength, 30 ) : null
|
||||
);
|
||||
|
||||
|
||||
return $this->createTag('input', $attributes);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the spam question
|
||||
*
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function Title() {
|
||||
return sprintf(_t('MathSpamProtectionField.SPAMQUESTION', "Spam protection question: %s"), self::get_math_question());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Validates the value submitted by the user with the one saved
|
||||
* into the {@link Session}
|
||||
* into the {@link Session} and then notify callback object
|
||||
* with the spam checking result.
|
||||
*
|
||||
* @return bool
|
||||
* @return bool
|
||||
*/
|
||||
function validate($validator) {
|
||||
if(!self::is_enabled()) return true;
|
||||
|
||||
if(!self::correct_answer($this->Value())){
|
||||
$validator->validationError(
|
||||
$this->name,
|
||||
_t(
|
||||
'MathSpamProtectionField.INCORRECTSOLUTION',
|
||||
"Incorrect solution to the spam protection question, please try again.",
|
||||
PR_MEDIUM
|
||||
),
|
||||
"error"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates the question from random variables, which are also saved to the session.
|
||||
*
|
||||
@ -78,16 +88,16 @@ class MathSpamProtectorField extends SpamProtectorField {
|
||||
$v1 = Session::get("mathQuestionV1");
|
||||
$v2 = Session::get("mathQuestionV2");
|
||||
}
|
||||
|
||||
|
||||
return sprintf(
|
||||
_t('MathSpamProtection.WHATIS',"What is %s plus %s?"),
|
||||
MathSpamProtectorField::digit_to_word($v1),
|
||||
_t('MathSpamProtection.WHATIS',"What is %s plus %s?"),
|
||||
MathSpamProtectorField::digit_to_word($v1),
|
||||
MathSpamProtectorField::digit_to_word($v2)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks the given answer if it matches the addition of the saved session variables.
|
||||
* Checks the given answer if it matches the addition of the saved session variables.
|
||||
* Users can answer using words or digits.
|
||||
*
|
||||
* @return bool
|
||||
@ -95,13 +105,13 @@ class MathSpamProtectorField extends SpamProtectorField {
|
||||
public static function correct_answer($answer){
|
||||
$v1 = Session::get("mathQuestionV1");
|
||||
$v2 = Session::get("mathQuestionV2");
|
||||
|
||||
|
||||
Session::clear('mathQuestionV1');
|
||||
Session::clear('mathQuestionV2');
|
||||
|
||||
|
||||
return (MathSpamProtectorField::digit_to_word($v1 + $v2) == $answer || ($v1 + $v2) == $answer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper method for converting digits to their equivalent english words
|
||||
*
|
||||
@ -126,13 +136,13 @@ class MathSpamProtectorField extends SpamProtectorField {
|
||||
_t('MathSpamProtection.FIFTEEN', 'fifteen'),
|
||||
_t('MathSpamProtection.SIXTEEN', 'sixteen'),
|
||||
_t('MathSpamProtection.SEVENTEEN', 'seventeen'),
|
||||
_t('MathSpamProtection.EIGHTEEN', 'eighteen'));
|
||||
|
||||
_t('MathSpamProtection.EIGHTEEN', 'eighteen'));
|
||||
|
||||
if($num < 0) return "minus ".($numbers[-1*$num]);
|
||||
|
||||
|
||||
return $numbers[$num];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns true when math spam protection is enabled
|
||||
*
|
||||
@ -141,7 +151,7 @@ class MathSpamProtectorField extends SpamProtectorField {
|
||||
public static function is_enabled() {
|
||||
return (bool) self::$enabled;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Set whether math spam protection is enabled
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user