Search the Community
Showing results for tags 'models'.
-
So I am working on an MVC application where the controller methods instantiate a factory that is suppose to return a model. In this case, the factory methods do some kind of work with a database or API to save/get/ data which then is placed inside a model and returned by the method. So lets say I have a factory method called createUser(email, password); Now I am going to call this method in the controller. I want my controller lean and mean and not fat! The model can be fat but NOT the controller and so my methods for validation at the moment are inside the model. Where do I call this validation? Inside the controller before the factory? Inside the createUser() method in the factory before it does it's database and api work to return a model??? Where?!? Here are two scenarios that come to mind: -Instantiate the User model inside the controller method before the factory and validate the input data. If it returns without any problems, send it to the factory to return a model of the User with the newly created data. -Instantiate the factory, call createUser(input data) and inside the create user method, do the model validation first before getting/saving the data to an API or database? If the validation fails in the createUser() method, return a model with error messages and any of the bad data. If their are no validation problems, return a model.
-
View is not displaying error messages.Validation done at model level My view is add.ctp <?php echo $this->Session->Flash(); foreach($this->form->validationerrors as $errors) { echo $errors['Publishers']; } if(isset($updates)) { $id=$updates['Agreement']['id']; $pubname=$updates['Agreement']['publisherid']; } echo $this->Form->create('Agreement', array('action' => 'add', 'type' => 'file')); //echo $this->Form->file('File'); echo "<table><tr><td>Publisher Name</td><td>".$this->Form->input('Publishers',array('label'=>false,'default'=>@$pubname))."</td></tr>"; echo "<tr><td>Agreement File</td><td>".$this->Form->file('File',array('label'=>false))."</td></tr>"; echo "<tr><td colspan='2'>".$this->Form->input('id',array('label'=>false,'default'=>@$id,'type'=>'hidden'))."</td></tr>"; echo "<tr><td colspan='2'>".$this->Form->submit('Upload',array('label'=>false,'after' => $this->Html->link('Cancel', array('action' => 'view'))))."</td></tr></table>"; echo $this->Form->end(); ?> Model is <?php //License Agreements //Created By Dharmender On 8-2-2013 class Agreement extends AppModel { var $name = 'Agreement'; var $validate=array( 'Publishers' => array( 'rule' => 'notEmpty', 'message' => 'Please enter publisher name.' )); } ?> Controller is <?php class AgreementsController extends AppController { var $name = 'Agreements'; var $components = array('Session'); function add($id=null) { if($id!=null) { $updates=$this->Agreement->findById($id); $this->set('updates',$updates); } if(@$this->request->data['Agreement']['id']==NULL) { $this->Agreement->create(); if (!empty($this->request->data) && is_uploaded_file($this->request->data['Agreement']['File']['tmp_name'])) { $fileData = fread(fopen($this->request->data['Agreement']['File']['tmp_name'], "r"), $this->request->data['Agreement']['File']['size']); $this->request->data['Agreement']['name'] = $this->request->data['Agreement']['File']['name']; $this->request->data['Agreement']['type'] = $this->request->data['Agreement']['File']['type']; $this->request->data['Agreement']['size'] = $this->request->data['Agreement']['File']['size']; $this->request->data['Agreement']['data'] = $fileData; if($this->request->data['Agreement']['Publishers']=='ram') { $id2=1; } else { $id2=2; } $this->request->data['Agreement']['publisherid']=$id2; $this->request->data['Agreement']['createdon'] = date ('Y-m-d H:i:s'); if($this->Agreement->save($this->data)) { $this->Session->setFlash('File Uploaded Successfully!'); $this->Redirect('view'); } else { $this->Session->setFlash('Error in File Uploading!'); } } } else { $this->Agreement->id=$this->request->data['Agreement']['id']; if (!empty($this->request->data) && is_uploaded_file($this->request->data['Agreement']['File']['tmp_name'])) { $fileData = fread(fopen($this->request->data['Agreement']['File']['tmp_name'], "r"), $this->request->data['Agreement']['File']['size']); $this->request->data['Agreement']['name'] = $this->request->data['Agreement']['File']['name']; $this->request->data['Agreement']['type'] = $this->request->data['Agreement']['File']['type']; $this->request->data['Agreement']['size'] = $this->request->data['Agreement']['File']['size']; $this->request->data['Agreement']['data'] = $fileData; if($this->request->data['Agreement']['Publishers']=='ram') { $id2=1; } else { $id2=2; } $this->request->data['Agreement']['publisherid']=$id2; $this->request->data['Agreement']['createdon'] = date ('Y-m-d H:i:s'); if($this->Agreement->save($this->data)) { $this->Session->setFlash('Record Updated Successfully!'); $this->Redirect('view'); } else { $this->Session->setFlash('Error in File Updating!'); } } } }