Radio buttons group and array validation problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • florin pojum
    New Member
    • May 2011
    • 3

    Radio buttons group and array validation problem

    Hello everybody,

    This is my first post on this forum and I wish to share with you my problem, hoping that someone has gone through the same problem as I have.

    My form looks like this:

    Form html code:

    Code:
    <form method="post" action="process.php">
    <input type="text" name="fullname"/>
    <textarea name="message"></textarea>
    
     <input type="radio" name="how"  value="1" />
     <input type="radio" name="how"  value="2" />
     <input type="radio" name="how"  value="3" />
     
    <input type="submit" value="Send">
    
    </form>
    Form process:

    Code:
    <?php require 'rules.php';
    function get_errors($form_data,$rules){
    	// returns an array of errors
    	$errors=array();
    	foreach($form_data as $name=>$value){
    		if(!isset($rules[$name]))continue;
    		$rule=$rules[$name];
    		$hname=htmlspecialchars($rule['title']);
    		if(isset($rule['required']) && $rule['required'] && !$value)
    		$errors[]='Field <strong>'.$hname.'</strong> is required.';
    		$rules[$name]['found']=true;
    	}
    	foreach($rules as $name=>$values){
    		if(!isset($values['found']) && isset($values['required']) && $values['required'])
    			$errors[]='Field '.htmlspecialchars($name).' is required.';
    	}
    	return $errors;
    }
    $errors=get_errors($_POST,$form_rules,$message);
    if(!count($errors)){
    
    	echo 'Message sent';
    }
    else{
    	echo $errors;
    }
    ?>
    And the rules.php looks like this:

    Code:
    <?php
    $form_rules=array(		  
    	'fullname'=>array(
    		'required'=>true,
    		'title'=>'Full Name',
    	),
    	'message'=>array(
    		'required'=>true,
    		'title'=>'Your Message',
    	),
    	'how'=> array ( 
                    'required'=> true,
                    'title'=>'How to',
       )
    );
    ?>
    The validation message returns the "title" of the fullname and message fields (Full Name, Your Message) but for the how radio group returns "how" instead of "How to".

    Any help will be very appreciated.
  • JKing
    Recognized Expert Top Contributor
    • Jun 2007
    • 1206

    #2
    Remove the commas after the last elements in your nested arrays.
    Code:
    <?php
    $form_rules=array(          
        'fullname'=>array(
            'required'=>true,
            'title'=>'Full Name'
        ),
        'message'=>array(
            'required'=>true,
            'title'=>'Your Message'
        ),
        'how'=> array ( 
                    'required'=> true,
                    'title'=>'How to'
       )
    );
    ?>
    Nothing else jumped out at me.

    Comment

    • florin pojum
      New Member
      • May 2011
      • 3

      #3
      Thanks a lot for your help, I tried to remove the commas as u suggested but I get the same result: the error message doesn't return the "title" for the radio buttons group - how, it displays how instead of How to.

      Comment

      • JKing
        Recognized Expert Top Contributor
        • Jun 2007
        • 1206

        #4
        Okay, a few things here.
        Line 19 of process.php
        Code:
        $errors=get_errors($_POST,$form_rules,$message);
        You are passing in three variables when the function is only built to accept two.

        The second foreach of the get_errors function is where the "how" is being added. This is because the array is two dimensional and you are only accessing the first dimension which is the name of the nested array.

        Comment

        • florin pojum
          New Member
          • May 2011
          • 3

          #5
          Thanks again for your reply, your suggestion was helpful, here is how I solved my problem with second foreach:

          Code:
          foreach($rules as $name){
          	  
          		if(!isset($name['found']) && isset($name['required']) && $name['required'])
          			$errors[]='Field '.htmlspecialchars($name['title']).' is required.';
          		
          	  }
          Kind Regards,

          Comment

          Working...