Skip to content

Instantly share code, notes, and snippets.

@overnin
Last active December 26, 2015 09:49
Show Gist options
  • Save overnin/7132571 to your computer and use it in GitHub Desktop.
Save overnin/7132571 to your computer and use it in GitHub Desktop.
<?php
// in the validation rule
....
'content' => array(
'requiredConditional' => array(
'rule' => array('requiredConditionalFieldOrValue', 'type-interaction', 'announcement', 'question-answer', 'question-answer-keyword'),
'message' => 'The interaction required a content.',
),
'notempty' => array(
'rule' => 'notempty',
'message' => 'Content field cannot be empty.'
),
'validApostrophe' => array(
'rule' => array('notRegex', VusionConst::APOSTROPHE_REGEX),
'message' => VusionConst::APOSTROPHE_FAIL_MESSAGE
),
'validCustomizedContent' => array(
'rule' => array('validCustomizeContent', VusionConst::DOMAIN_NOT_RESPONSE)
'message' => 'noMessage'
),
),
....
public $validateFeedback = array(
'content' => array(
'notempty' => array(
'rule' => 'notempty',
'message' => 'Content field cannot be empty.'
),
'validApostrophe' => array(
'rule' => array('notRegex', VusionConst::APOSTROPHE_REGEX),
'message' => VusionConst::APOSTROPHE_FAIL_MESSAGE
),
'validCustomizeContent' => array(
'rule' => array('validCustomizeContent', VusionConst::DOMAIN_ALL)
'message' => 'noMessage'
),
)
);
// now the function
public function validCustomizeContent($field, $allowedDomain, $data)
{
if (isset($data[$field])) {
preg_match_all(VusionConst::CONTENT_VARIABLE_MATCHER_REGEX, $data[$field], $matches, PREG_SET_ORDER);
$allowed = array("domain", "key1", "key2", "keys3", "otherkey");
foreach($matches as $match) {
$match = array_intersect_key($match, array_flip($allowed));
foreach ($match as $key=>$value) {
if (!preg_match(VusionConst::CONTENT_VARIABLE_KEY_REGEX, $value)) {
return __("To be used as customized content, '%s' can only be composed of letter(s), digit(s) and/or space(s).", $value);
}
}
if (!preg_match($allowedDomain, $match['domain'])) {
return __("To be used as customized content, '%s' can only be either: %s.", $match['domain'], implode(',', $allowedDomain));
}
if ($match['domain'] == 'participant') {
if (isset($match['key2'])) {
return VusionConst::CONTENT_VARIABLE_DOMAIN_PARTICIPANT_FAIL;
}
} else if ($match['domain'] == 'contentVariable') {
if (isset($match['otherkey'])) {
return VusionConst::CONTENT_VARIABLE_DOMAIN_CONTENTVARIABLE_FAIL;
}
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment