CMS  Version 3.9
RequiredCheckListQuestionValidator Class Reference

RequiredCheckListQuestion Validator. More...

Inherits RequiredValidator.

Public Member Functions

 RequiredCheckListQuestionValidator ($field, $title, $min=0, $answer="")
 
 writeClient ()
 
 validate ()
 

Public Attributes

 $min
 
 $answer
 
 $text
 

Detailed Description

RequiredCheckListQuestion Validator.

Verifies min count of checkboxes checked

Definition at line 116 of file validation.inc.

Member Function Documentation

◆ RequiredCheckListQuestionValidator()

RequiredCheckListQuestionValidator::RequiredCheckListQuestionValidator (   $field,
  $title,
  $min = 0,
  $answer = "" 
)

Definition at line 122 of file validation.inc.

123  {
124  $this->min = $min;
125  $this->answer = $answer;
126  $this->AbstractValidator($field, $title);
127 
128  $this->text = ($min < 2) ? "checkbox" : "checkboxes";
129  }

◆ validate()

RequiredCheckListQuestionValidator::validate ( )

Definition at line 157 of file validation.inc.

158  {
159  if(!$this->answer)
160  {
161  global $_POST;
162  $values = $_POST[$this->field];
163  // convert array of checks to comma delimated string and
164  // back again to eliminate blanks
165  if (is_array($values))
166  $valueAnswer = implode(",", array_values($values));
167  }
168  else
169  {
170  $valueAnswer = $this->answer->value;
171  }
172 
173  if (strlen($valueAnswer) > 1)
174  $count = count(explode(",", $valueAnswer));
175  elseif($valueAnswer)
176  $count = 1;
177  else
178  $count = 0;
179 
180  if ($count < $this->min)
181  {
182  return "You must check at least {$this->min} {$this->text} for {$this->title}.";
183  }
184  else
185  {
186  return "";
187  }
188  }
$_POST["owner_id"]
Definition: blog_form.inc:54

◆ writeClient()

RequiredCheckListQuestionValidator::writeClient ( )

Definition at line 131 of file validation.inc.

132  {
133  $script = <<<ENDSCRIPT
134 
135  var i = 1;
136  var countChecked = 0;
137  var max = form["count_{$this->field}"].value;
138  var fieldName;
139 
140  for(i=1; i <= max; i++)
141  {
142  fieldName = form["{$this->field}["+i+"]"];
143  if(fieldName.checked == true)
144  countChecked += 1;
145  }
146 
147  if ({$this->min} > 0 && countChecked < {$this->min})
148  {
149  alert("You must check at least {$this->min} {$this->text} for {$this->title}.");
150  return false;
151  }
152 ENDSCRIPT;
153 
154  return $script;
155  }
$chart max

Member Data Documentation

◆ $answer

RequiredCheckListQuestionValidator::$answer

Definition at line 119 of file validation.inc.

◆ $min

RequiredCheckListQuestionValidator::$min

Definition at line 118 of file validation.inc.

◆ $text

RequiredCheckListQuestionValidator::$text

Definition at line 120 of file validation.inc.


The documentation for this class was generated from the following file: