Commit ea166e4b by Scott

Fix missing title errors when recategorizing questions

parent 41faf774
......@@ -51,23 +51,38 @@ class qa_filter_basic
public function filter_question(&$question, &$errors, $oldquestion)
{
$this->validate_length($errors, 'title', @$question['title'], qa_opt('min_len_q_title'),
max(qa_opt('min_len_q_title'), min(qa_opt('max_len_q_title'), QA_DB_MAX_TITLE_LENGTH)));
if ($oldquestion === null) {
// a new post requires these fields be set
$question['title'] = isset($question['title']) ? $question['title'] : '';
$question['content'] = isset($question['content']) ? $question['content'] : '';
$question['text'] = isset($question['text']) ? $question['text'] : '';
$question['tags'] = isset($question['tags']) ? $question['tags'] : array();
}
$this->validate_length($errors, 'content', @$question['content'], 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
$qminlength = qa_opt('min_len_q_title');
$qmaxlength = max($qminlength, min(qa_opt('max_len_q_title'), QA_DB_MAX_TITLE_LENGTH));
$this->validate_field_length($errors, $question, 'title', $qminlength, $qmaxlength);
$this->validate_length($errors, 'content', @$question['text'], qa_opt('min_len_q_content'), null); // for display
$this->validate_field_length($errors, $question, 'content', 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
$this->validate_field_length($errors, $question, 'text', qa_opt('min_len_q_content'), null); // for display
if (isset($question['tags'])) {
$counttags=count($question['tags']);
$mintags=min(qa_opt('min_num_q_tags'), qa_opt('max_num_q_tags'));
if ($counttags<$mintags)
$errors['tags']=qa_lang_sub('question/min_tags_x', $mintags);
elseif ($counttags>qa_opt('max_num_q_tags'))
$errors['tags']=qa_lang_sub('question/max_tags_x', qa_opt('max_num_q_tags'));
else
$this->validate_length($errors, 'tags', qa_tags_to_tagstring($question['tags']), 0, QA_DB_MAX_TAGS_LENGTH); // for storage
$counttags = count($question['tags']);
$maxtags = qa_opt('max_num_q_tags');
$mintags = min(qa_opt('min_num_q_tags'), $maxtags);
if ($counttags < $mintags) {
$errors['tags'] = qa_lang_sub('question/min_tags_x', $mintags);
}
elseif ($counttags > $maxtags) {
$errors['tags'] = qa_lang_sub('question/max_tags_x', $maxtags);
}
else {
$tagstring = qa_tags_to_tagstring($question['tags']);
if (qa_strlen($tagstring) > QA_DB_MAX_TAGS_LENGTH) { // for storage
$errors['tags'] = qa_lang_sub('main/max_length_x', $maxlength);
}
}
}
$this->validate_post_email($errors, $question);
......@@ -113,6 +128,30 @@ class qa_filter_basic
}
/**
* Check that a field meets the length requirements. If we're editing the post we can ignore missing fields.
*
* @param array $errors Array of errors, with keys matching $post
* @param array $post The post containing the field we want to validate
* @param string $key The element of $post to validate
* @param int $minlength
* @param int $maxlength
*/
private function validate_field_length(&$errors, &$post, $key, $minlength, $maxlength)
{
// skip the field is key not set (for example, 'title' when recategorizing questions)
if (array_key_exists($key, $post)) {
$length = qa_strlen($post[$key]);
if ($length < $minlength) {
$errors[$key] = $minlength == 1 ? qa_lang('main/field_required') : qa_lang_sub('main/min_length_x', $minlength);
}
else if (isset($maxlength) && ($length > $maxlength)) {
$errors[$key] = qa_lang_sub('main/max_length_x', $maxlength);
}
}
}
/**
* Wrapper function for validating a post's email address.
*
* @deprecated This function will become private in Q2A 1.8. It is specific to this plugin and
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment