Jump to content

CakePHP beforeSave()


devilinc

Recommended Posts

i want to check a condition when the user enters a text field input in the form....i want to check that the value doesnt exceed a value already listed...i run this condition before if($this->save($this->data))....what happens on submission is that the condition succeeds BUT the form entries entered by customer gets wiped out completely and he has to enter from scratch....i want on form submit to display an error message and the text fields should retain all the values and better still if the error message comes below the text field with conditional statement.....what do i do??? please help.....saw a beforeSave() ran it but I think I am running it the wrong way....i just enter the conditon test inside beforeSave() written in my model.....but i dont make a call to it in the controller....so could anyone guide me?

Link to comment
Share on other sites

ok now i get the beforeSave() working correctly with my form values still there but my url is changed now....i pass an id also with the url but now the id is not present and hence my function never runs and i get the form with the values entered back to me.........it doesnt save...what am i doing wrong?

Link to comment
Share on other sites

  • 2 weeks later...

i dont know what i am missing but it doesnt work for me....this is what i put in my model


var $validate   = array( 'customer_name' => array('valid' => array('rule' => 'notEmpty','message' => 'This field cannot be left blank'),
											  'unique' => array('rule' => 'isUnique','message' => 'This Customer Name has already been used')),

						'customer_monthlyamount' => array('rule' => array('checkLimit', $_SESSION['limit']),'message' => 'you have exceeded your limit'),
						'customer_loginname'=>array('valid' => array('rule' => 'notEmpty','message' => 'This field cannot be left blank'),
											  'unique' => array('rule' => 'isUnique','message' => 'This Login Name has already been selected by a user. Please choose a different login name.'),'customer_loginnameRule-1' => array('rule' => 'alphaNumeric','message' => 'Only alphabets and numbers allowed','last' => true),
        
													),  

						'customer_email' => array('valid' => array('rule' => 'email','message' => 'Please enter a valid email address'),
											  'unique' => array('rule' => 'isUnique','message' => 'This email address has already been used')),
						'customer_confpassword' => array('rule' => array('minLength','6'),'required' => true,'allowEmpty' => false,'message' => 'Password must have at least 6 characters'),'customer_loginpassword' => array('rule' => array('minLength','6'),'required' => true,'allowEmpty' => false,'message' => 'Password must have at least 6 characters'),'customer_loginpassword' => array('identicalFieldValues' => array('rule' => array('identicalFieldValues', 'customer_confpassword' ),'message' => 'Confirm password must be same as Password')),
					  );



function checkLimit($check,$limit){
		return $check['customer_monthlyamount'] < $limit;	
	}

 

 

 

 

but it doesnt seem to work........$_SESSION['limit'] is the value from my function but it doesnt seem to work......

 

 

 

tried beforeSave() too.....here it works for add with my error message displayed but for edit the logic works but no error message displayed...

var $validate   = array( 'customer_name' => array('valid' => array('rule' => 'notEmpty','message' => 'This field cannot be left blank'),
											  'unique' => array('rule' => 'isUnique','message' => 'This Customer Name has already been used')),
						'customer_monthlyamount' => array('rule' => 'notEmpty'),
						'customer_loginname'=>array('valid' => array('rule' => 'notEmpty','message' => 'This field cannot be left blank'),
											  'unique' => array('rule' => 'isUnique','message' => 'This Login Name has already been selected by a user. Please choose a different login name.'),'customer_loginnameRule-1' => array('rule' => 'alphaNumeric','message' => 'Only alphabets and numbers allowed','last' => true),
        
													),  

						'customer_email' => array('valid' => array('rule' => 'email','message' => 'Please enter a valid email address'),
											  'unique' => array('rule' => 'isUnique','message' => 'This email address has already been used')),
						'customer_confpassword' => array('rule' => array('minLength','6'),'required' => true,'allowEmpty' => false,'message' => 'Password must have at least 6 characters'),'customer_loginpassword' => array('rule' => array('minLength','6'),'required' => true,'allowEmpty' => false,'message' => 'Password must have at least 6 characters'),'customer_loginpassword' => array('identicalFieldValues' => array('rule' => array('identicalFieldValues', 'customer_confpassword' ),'message' => 'Confirm password must be same as Password')),
					  );
function beforeSave(){
		//if(!isset($id)){
			if($this->data['Customer']['customer_monthlyamount'] <= $_SESSION['limit']){
				$this->data['Customer']['customer_monthlyamount'] = $this->data['Customer']['customer_monthlyamount'];				
				//echo "<font color ='red'>the monthly limit alloted for your deparment is over.sorry you cannot register</font>";
				return true;
			}else{
					echo "<font color ='red'>the monthly limit alloted for your deparment is over.sorry you cannot register</font>";
					return false;
				}
		//}
	}

 

can you please direct me on the right path?

 

Link to comment
Share on other sites

the add function with error message comes correctly below the textfield input with the check limit function if it is static value for the checkLimit function

'customer_monthlyamount' => array(
								'rule' => array('checkLimit', 1500),
								'message' => 'You have exceeded your alotted limit'
							)

 

but if it is

'customer_monthlyamount' => array(
								'rule' => array('checkLimit', $_SESSION['limit']),
								'message' => 'You have exceeded your alotted limit'
							)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.