devilinc Posted January 10, 2011 Share Posted January 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/ Share on other sites More sharing options...
devilinc Posted January 10, 2011 Author Share Posted January 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/#findComment-1157318 Share on other sites More sharing options...
devilinc Posted January 10, 2011 Author Share Posted January 10, 2011 btw, my add function is not named as add() but instead custmerscreate() Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/#findComment-1157351 Share on other sites More sharing options...
thehippy Posted January 11, 2011 Share Posted January 11, 2011 Setup Data Validation in your model, Cake if I recall automagically takes care of the error handling. You can create customized callback functions for non-standard checks quite easily. Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/#findComment-1157703 Share on other sites More sharing options...
devilinc Posted January 19, 2011 Author Share Posted January 19, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/#findComment-1161773 Share on other sites More sharing options...
devilinc Posted January 19, 2011 Author Share Posted January 19, 2011 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' ) Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/#findComment-1161792 Share on other sites More sharing options...
devilinc Posted January 19, 2011 Author Share Posted January 19, 2011 i forgot to add this "it doesnt seem to work" Quote Link to comment https://forums.phpfreaks.com/topic/223944-cakephp-beforesave/#findComment-1161809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.