i am trying to validate a check box, but does not seem to get it to work.(though learning from a video for pop), i don’t know if the rule properties(like min , max, required) are standard or i can use any name like ‘ticked’. all other rule value works except for the check boc.
<?php
require_once 'core/init.php';
if(Input::exists()){
//$accept = Input::get('accept');
//echo $accept;
//$accept=$_POST['accept'];
//echo $accept;
if (Token::check(Input::get('token')))
{
//Input::get('accept');
$validate = new Validate();
$validation = $validate->check($_POST, array(
'fullname' => array(
'required' => true),
'username' => array(
'required' => true,
'min' => 2,
'max' => 20,
'unique' => 'reusers'),
'password' => array(
'required' => true,
'min' => 6),
'repeatpassword' => array(
'required' => true,
'matches' => 'password'),
'email' => array(
'required' => true,
'unique' => 'reusers'),
'houseno' => array(
'required' => true),
'addressa' => array(
'required' => true),
'addressc' => array(
'required' => true),
'county' => array(
'required' => true),
'country' => array(
'required' => true),
'telno' => array(
'required' => true),
'accept' => array(
'ticked' => '1')
));
if($validation->passed()){
// insert in table
echo 'passed';
} else {
//output error
//print_r ($validation->errors());
foreach ($validation->errors() as $error) {
echo $error, '<br>';
}
}
}
}
?>
this is the method
public function check($source, $items = array()) {
foreach($items as $item => $rules) {
foreach($rules as $rule => $rule_value) {
$value = trim($source[$item]);
if($rule === 'required' && $rule_value === true && empty($value)) {
$this->addError("{$item} is required.");
} else if (!empty($value)) {
switch($rule) {
case 'min':
if(strlen($value) < $rule_value) {
$this->addError("{$item} must be a minimum of {$rule_value} characters.");
}
break;
case 'max':
if(strlen($value) > $rule_value) {
$this->addError("{$item} must be a maximum of {$rule_value} characters.");
}
break;
case 'is_numeric':
if(is_numeric($value) != $rule_value) {
$this->addError("{$item} must be a numeric value.");
}
break;
case 'matches':
if($value != $source[$rule_value]) {
$this->addError("{$rule_value} must match {$item}.");
}
break;
case 'unique':
$check = $this->_db->get('reusers', array($item, '=', $value));
if($check->count()) {
$this->addError("{$item} is already taken.");
}
break;
case 'ticked':
if($value != $rule_value) {
$this->addError("{$item} must be a checked.");
}
break;
}
}
}
this is the form part.
<div class='rl'>
<input name="accept" type="checkbox" class="tickbox" value="1" />
I agree to Reacheasy's<a href="termsandcondition.php">Terms and Conditions</a>
</div>