Welcome Guest, Not a member yet? Register   Sign In
form_validation callback_rule issue
#1

[eluser]mindriot[/eluser]
Hey i'm trying to write a custom rule for a form with a bunch of checkboxes. But running into a bit of a brick wall.

The entire group of checkboxes needs to have at least one value set so the group should be required.. However, if the "none" checkbox is selected, no other selected options should be allowed to be selected or posted.

The View - Only showing the selected question excerpt
Code:
<li class="question4">
     &lt;?php echo form_label('North Shore Times','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[0]', '1',set_checkbox('4_read_papers','1')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('North Harbour News','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[1]', '2',set_checkbox('4_read_papers','2')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Western Leader','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[2]', '3',set_checkbox('4_read_papers','3')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Central Leader','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[3]', '4',set_checkbox('4_read_papers','4')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Auckland City Harbour News','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[4]', '5',set_checkbox('4_read_papers','5')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Waiheke Marketplace','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[5]', '6',set_checkbox('4_read_papers','6')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Manukau Courier','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[6]', '7',set_checkbox('4_read_papers','7')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Papakura Courier','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[7]', '8',set_checkbox('4_read_papers','8')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Nor-West News','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[8]', '9',set_checkbox('4_read_papers','9')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Rodney Times','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[9]', '10',set_checkbox('4_read_papers','10')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('Eastern Courier','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[10]', '11',set_checkbox('4_read_papers','11')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('East and Bays Courier','',array('class'=>'subLabel')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[11]', '12',set_checkbox('4_read_papers','12')); ?&gt;
    </li>
    <li class="question4">
     &lt;?php echo form_label('None','',array('class'=>'subLabel none')); ?&gt;
     &lt;?php echo form_checkbox('4_read_papers[12]', '13',set_checkbox('4_read_papers','13')); ?&gt;
    </li>

Controller Rule being setup
Code:
$this->form_validation->set_rules('4_read_papers', 'Question 4:', 'required|callback_question4_check');

Custom Set Rule, as far as I have.
(First half checks the posted values are what i'm expecting. The second half else is my attempt at trying to unset any other post values if the value '13' is picked up, 13 being the value of the none checkbox.)

Code:
public function question4_check($str)
{

  $error = '';
  $valid_values = array('1', '2', '3', '4', '5', '6' , '7' , '8', '9', '10', '11', '12', '13');
  foreach($str as $key=>$value){
   if(!in_array($value,$valid_values))
   {
    $error = true;
   }
  }
  if ( isset($str) && $error)
  {
    $this->form_validation->set_message('question4_check', 'The Question 4 field: You must select an option from the %s field');
    return FALSE;
  }
  
  else
  {
   if(in_array('13',$this->form_validation->_field_data['4_read_papers']['postdata']))
   {
    foreach($this->form_validation->_field_data['4_read_papers']['postdata'] as $key =>$value)
    {
     if($value != '13'){
      unset($this->form_validation->_field_data['4_read_papers']['postdata'][$key]);
      unset($_POST['4_read_papers'][$key]);
     }
    }
   }
   return TRUE;
  }
}

I'd like this all to work with javascript turned off. So the server side validation is rock solid. But no matter what I try the other options remain set and get posted alongside the None value.

Either two options are preferable..
If none is selected. Trash all other $_POST data relating to that and just pass through the none value. OR keep both the None value selected and the other values that were posted alongside and point the user to the face he cannot have both.

Really appreciate anyone looking at this and throwing in their 2 cents where I am going wrong.




Theme © iAndrew 2016 - Forum software by © MyBB