Can't validate model ?!
Hey everybody, I'm new to CakePHP, so don't shoot me if I made a stupid mistake. I'm having a problem validating my model I created. I think I done everything right, but it doesnt' work. I already took a look at http://book.cakephp.org/view/1143/Data-Validation and http://book.cakephp.org/view/1182/Validating-Data-from-the-Controller. According to the cookbook I done it right but somehow I can't get my model to be validated. This is my code so far: My Link_controller.php ?php class LinksController extends AppController { var $name = 'Links'; var $helpers = array('Html','Session'); function index() { $this-set(links,$this-Link-find('all')); } function add() { if(isset($this-data)) { $this-Link-set($this-data); if($this-Link-validates()) { debug($this-data); //if($this-Link-save($this-data)) $this-Session-setFlash('Data Saved Succesfully'); } } } } ? If i don't type anything in my form my add method gets to the debug statement. Which I find strange because I use the isset($this-data). my debug print out looks like this : Array ( [Link] = Array ( [link_name] = [link_url] = [link_category] = [link_img_url] = ) ) my link model ?php class Link extends AppModel { var $name = 'Link'; var $validate = array( 'link_name' = array( 'rule' = 'notEmpty' ), 'link_url' = array( 'rule' = 'notEmpty' ) ); /* var $validate = array( 'link_name' = array( 'link_name_rule1' = array( 'rule' = 'alphaNumeric', 'message' = 'Link name can only contain numbers and letters' ), 'link_name_rule2' = array( 'rule' = array('macLength',50), 'message' = 'Link name can maximum have 50 characters' ), 'link_name_rule3' = array( 'rule' = 'notEmpty', 'message' = 'The field cannot be empty' ), 'link_name_rule4' = array( 'rule' = 'isUnique', 'message' = 'The field must be unique' ) ), 'link_url' = array( 'link_url1' = array( 'rule' = 'notEmpty', 'message' = 'The url cannot be empty' ), 'link_url2' = array( 'rule' = array('url',true), 'message' = 'The given input isnot a valid url' ) ), 'link_category' = array( 'link_category' = array( 'rule' = 'notEmpty', 'message' = 'The category cannot be empty' ), 'link_category' = array( 'rule' = array('between',1,3), 'message' = 'The image category must be 1,2 or 3' ) ), 'link_img_url' = array( 'link_img_url1' = array( 'rule' = 'notEmpty', 'message' = 'The image url cannot be empty' ), 'link_img_url2' = array( 'rule' = array('extension',array('gif','jpeg','png','jpg')), 'message' = 'Please supply a valid image' ) ) );*/ } ? I first tried some simple rules but they also don't seem to work. and my add view: ?php echo $form-create('Link'); echo $form-input('link_name'); echo $form-input('link_url'); echo $form-input('link_category'); echo $form-input('link_img_url'); echo $form-end('Add Link'); ? I find it really strange could some one please help me find this stupid bug ? Thanks a lot, cheers -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at
Re: Can't validate model ?!
you should use if(!empty($this-data)) {} see baked code examples as a beginner i strongly recommend that you use baking to create your views and forms On 29 Mai, 21:47, Niels steni...@gmail.com wrote: Hey everybody, I'm new to CakePHP, so don't shoot me if I made a stupid mistake. I'm having a problem validating my model I created. I think I done everything right, but it doesnt' work. I already took a look athttp://book.cakephp.org/view/1143/Data-Validation andhttp://book.cakephp.org/view/1182/Validating-Data-from-the-Controller. According to the cookbook I done it right but somehow I can't get my model to be validated. This is my code so far: My Link_controller.php ?php class LinksController extends AppController { var $name = 'Links'; var $helpers = array('Html','Session'); function index() { $this-set(links,$this-Link-find('all')); } function add() { if(isset($this-data)) { $this-Link-set($this-data); if($this-Link-validates()) { debug($this-data); //if($this-Link-save($this-data)) $this-Session-setFlash('Data Saved Succesfully'); } } } } ? If i don't type anything in my form my add method gets to the debug statement. Which I find strange because I use the isset($this-data). my debug print out looks like this : Array ( [Link] = Array ( [link_name] = [link_url] = [link_category] = [link_img_url] = ) ) my link model ?php class Link extends AppModel { var $name = 'Link'; var $validate = array( 'link_name' = array( 'rule' = 'notEmpty' ), 'link_url' = array( 'rule' = 'notEmpty' ) ); /* var $validate = array( 'link_name' = array( 'link_name_rule1' = array( 'rule' = 'alphaNumeric', 'message' = 'Link name can only contain numbers and letters' ), 'link_name_rule2' = array( 'rule' = array('macLength',50), 'message' = 'Link name can maximum have 50 characters' ), 'link_name_rule3' = array( 'rule' = 'notEmpty', 'message' = 'The field cannot be empty' ), 'link_name_rule4' = array( 'rule' = 'isUnique', 'message' = 'The field must be unique' ) ), 'link_url' = array( 'link_url1' = array( 'rule' = 'notEmpty', 'message' = 'The url cannot be empty' ), 'link_url2' = array( 'rule' = array('url',true), 'message' = 'The given input isnot a valid url' ) ), 'link_category' = array( 'link_category' = array( 'rule' = 'notEmpty', 'message' = 'The category cannot be empty' ), 'link_category' = array( 'rule' = array('between',1,3), 'message' = 'The image category must be 1,2 or 3' ) ), 'link_img_url' = array( 'link_img_url1' = array( 'rule' = 'notEmpty', 'message' = 'The image url cannot be empty' ), 'link_img_url2' = array( 'rule' = array('extension',array('gif','jpeg','png','jpg')), 'message' = 'Please supply a valid image' ) ) );*/} ? I first tried some simple rules but they also don't seem to work. and my add view: ?php echo $form-create('Link'); echo $form-input('link_name'); echo $form-input('link_url'); echo $form-input('link_category'); echo $form-input('link_img_url'); echo $form-end('Add Link'); ? I find it really strange could some one please help me find this stupid bug ? Thanks a lot, cheers -- Our
Re: Can't validate model ?!
Thanks for the response but !empty doesn't work either. it still passes the if and de debug output is still: Array ( [Link] = Array ( [link_name] = [link_url] = [link_category] = [link_img_url] = ) ) when I don't input anything in the form. Maybe you're right of using the console to bake my first app but still my code should work. Could any body see the error or mistake I made ? On 30 May 2011, at 16:33, euromark wrote: you should use if(!empty($this-data)) {} see baked code examples as a beginner i strongly recommend that you use baking to create your views and forms On 29 Mai, 21:47, Niels steni...@gmail.com wrote: Hey everybody, I'm new to CakePHP, so don't shoot me if I made a stupid mistake. I'm having a problem validating my model I created. I think I done everything right, but it doesnt' work. I already took a look athttp://book.cakephp.org/view/1143/Data-Validation andhttp://book.cakephp.org/view/1182/Validating-Data-from-the-Controller. According to the cookbook I done it right but somehow I can't get my model to be validated. This is my code so far: My Link_controller.php ?php class LinksController extends AppController { var $name = 'Links'; var $helpers = array('Html','Session'); function index() { $this-set(links,$this-Link-find('all')); } function add() { if(isset($this-data)) { $this-Link-set($this-data); if($this-Link-validates()) { debug($this-data); //if($this-Link-save($this-data)) $this-Session-setFlash('Data Saved Succesfully'); } } } } ? If i don't type anything in my form my add method gets to the debug statement. Which I find strange because I use the isset($this-data). my debug print out looks like this : Array ( [Link] = Array ( [link_name] = [link_url] = [link_category] = [link_img_url] = ) ) my link model ?php class Link extends AppModel { var $name = 'Link'; var $validate = array( 'link_name' = array( 'rule' = 'notEmpty' ), 'link_url' = array( 'rule' = 'notEmpty' ) ); /* var $validate = array( 'link_name' = array( 'link_name_rule1' = array( 'rule' = 'alphaNumeric', 'message' = 'Link name can only contain numbers and letters' ), 'link_name_rule2' = array( 'rule' = array('macLength',50), 'message' = 'Link name can maximum have 50 characters' ), 'link_name_rule3' = array( 'rule' = 'notEmpty', 'message' = 'The field cannot be empty' ), 'link_name_rule4' = array( 'rule' = 'isUnique', 'message' = 'The field must be unique' ) ), 'link_url' = array( 'link_url1' = array( 'rule' = 'notEmpty', 'message' = 'The url cannot be empty' ), 'link_url2' = array( 'rule' = array('url',true), 'message' = 'The given input isnot a valid url' ) ), 'link_category' = array( 'link_category' = array( 'rule' = 'notEmpty', 'message' = 'The category cannot be empty' ), 'link_category' = array( 'rule' = array('between',1,3), 'message' = 'The image category must be 1,2 or 3' ) ), 'link_img_url' = array( 'link_img_url1' = array( 'rule' = 'notEmpty', 'message' = 'The image url cannot be empty' ), 'link_img_url2' = array( 'rule' = array('extension',array('gif','jpeg','png','jpg')),