0

Hi,

How do I prevent my user from submitting an empty form?
When I do that, I'm getting a HTTP 404 not found error in IE and a blank page in Firefox.
None of the validation errors are showing up.

I even tried putting an else stmt in the StudentsController's add function and that too is not firing. Nothing shows up for die(debug($this->Student->validationErrors)) either:

if (!empty($this->data)){
			die(debug($this->Student->validationErrors)); 			$student=$this->Student->saveAll($this->data);
			
			if (!empty($student))
			   {
				$this->Session->setFlash('Your child\'s admission has been received. We will send you an email shortly.');
						$this->redirect(array('controller'=>'pages', 'action'=>'home'));

				}
			else{
				$this->Session->setFlash(__('Your admission could not be saved. Please, try again.', true));		         }
		
	    } //for if (!empty....

thank you.

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by trav04
0

You can use isset to check if the form data has been set.

Example :

<?php
if (isset($_GET['test'])){
 echo "Not an empty form";
}else{
 echo "Empty form";
}
?>
0

i tried that, i'm still getting Http 404 PAGE not found in IE and in firefox, the same page is displaying blank when i click submit.

0

The following is my complete code:

students_controller.php

<?php
      class StudentsController extends AppController{
      var $name='Students';
      var $helpers = array('Html','Form','Ajax','Javascript');
      var $components=array('Security','RequestHandler');

     function beforeFilter()//executed before any controller action logic
	{	
		    //parent::beforeFilter();
	    if(isset($this->Security) && $this->RequestHandler->isAjax() && $this->action = 'getcities'){
	        $this->Security->enabled = false;
			} 
			
	}

   
    if (!empty($this->data)){
	die(debug($this->Student->validationErrors)); 
	$student=$this->Student->saveAll($this->data);
			
	if (!empty($student))
	   {$this->Session->setFlash('Your child\'s admission has been received. 
              We will send you an email shortly.');
	    $this->redirect(array('controller'=>'pages', 'action'=>'home'));
	    }
	else{
	     $this->Session->setFlash(__('Your admission could not be saved. 
               Please, try again.', true));		         
              }
		
           } //for if (!empty....
        $states=$this->Student->MerryParent->State->find('list');
		$this->set('states',$states);
		
		$cities=array();	
		//$this->set(compact('states','cities'));
        }//end function
      }
      ?>

Following are my models with validation:
student.php

<?php
       class Student extends AppModel{
	var $name='Student';
	var $belongsTo=array('MerryParent','MerryClass','State','City');
	
	var $validate=array(
			'name'=>array(
					'rule'=>array('minLength',3),
					'required'=>true,
					'allowEmpty'=>false,
					'message'=>'Name is required!'
					),
			'dob'=>array(
					'rule'=>'date',
					'required'=>true,
					'allowEmpty'=>false,
					'message'=>'Enter a valid birth date!'
					),
			'class_id'=>array(
					'rule'=>'notEmpty', 
					'message'=>'Which class are you enquiring about?'
					)
			//'city_id'=>array('rule'=>'notEmpty','message'=>'Please select your city','required'=>true, 'allowEmpty'=>false)
			);
}
?>

merry_parent.php

<?php
        class MerryParent extends AppModel{
	var $name='MerryParent';
	var $hasMany=array(
		'Student'=>array(
			'className'=>'Student',
			'foreignKey'=>'merry_parent_id'
			        )
				);
	var $belongsTo=array('State','City','MerryClass');
	
	var $validate=array(
				'initial'=>array(
					'rule'=>'notEmpty',
					'message'=>'Please select your initial'
					),
				'name'=>array(
					'rule'=>array('minLength',3),
					'required'=>true,
					'allowEmpty'=>false,
					'message'=>'Name is required!'
					),
				'email'=>array(
					'rule'=>'email',
					'required'=>true, 
					'allowEmpty'=>false,
					'message'=>'Valid email address required!'
					),
				'landline'=>array(
				       'rule'=>array('custom','/(0[0-9]{2,4}-[2-9][0-9]{5,7})/'), 
					   'required'=>false, 
					   'allowEmpty'=>true, 
					   'message'=>'Invalid phone number! phone number format: eg 020-22345678 OR 0544-7573758 OR 02345-874567'
					   ),
				'mobile'=>array(
				     'rule'=>array('custom','/([89]{1}[0-9]{9})/'), 
					 'required'=>true, 
					 'allowEmpty'=>false, 
					 'message'=>'Invalid mobile number! mobile number format: eg 9876543211'
					 ),
				'address'=>array(
				     'rule'=>array('alphaNumeric',array('minLength',1)),
					 'required'=>true, 
					 'allowEmpty'=>false, 
					 'message'=>'Please enter your address.'
					 ),
			'state_id'=>array(
					'rule'=>'notEmpty',
					//'required'=>true,
					'message'=>'Please select your state'
					),
								                  'postal_code'=>array(
				         'rule'=>array('numeric',6),
						 'required'=>true, 
						 'allowEmpty'=>false,
						 'message'=>'valid postal code required!'
						 )
				);//closing bracket for $validate array
       }
      ?>

thank you.

0

I know this is a old thread , But may i see your add/edit.ctp and the function in the controller?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.