2012-11-22 43 views
3

你好,請告訴我。 如何驗證cakephp中的關聯模型? 我在一個控制器中使用多個模型,控制器名稱是'AdminsController'。 這些模型AdminsController如何驗證cakephp中的關聯模型

$uses=array('index','Static_page_view','Admin','Categories','Products', 'contact','User','Rams');' 

現在我想驗證 '類別'(型號)。我定義了一組驗證規則。但不能驗證。而當我使用'管理員'(模型)它完美的作品。

<?php   
class AdminsController extends AppController {  

    public $uses=array('index','Static_page_view','Admin','Categories','Products', 'contact','User','Rams'); 

public function index() 
    {  
    if(!empty($this->request->data)) //this checks if the form is being submitted and is not empty 
     { 


      if($this->Admin->save($this->request->data))//this saves the data from the form and also return true or false 
       { 
        $this->Session->setFlash('The post was successfully added!'); 
        $this->redirect(array('action'=>'index')); 
       } 
      else 
       { 
        $this->Session->setFlash('The post was not saved, please try again'); 
       } 

}

public function Add_Category() 

{ 

if(!empty($this->request->data)) //this checks if the form is being submitted and is not empty 
{ 



     if($this->Rams->save($this->request->data))//this saves the data from the form and also return true or false 
     { 
      $this->Session->setFlash('The post was successfully added!'); 
      $this->redirect(array('action'=>'Add_Category')); 
     } 
     else 
     { 
      $this->Session->setFlash('The post was not saved, please try again'); 
     } 


} 
} 
    } 

\程序\型號\管理員

 <?php 
    class Admin extends AppModel { 


    public $validate = array(['Admin'] => array(
      'name'=>array(
       'title_must_not_be_blank'=>array(
        'rule'=>'notEmpty', 
        'message'=>'Please Enter your Name!' 
       ) 
       ), 
      'email'=>array(
       'body_must_not_be_blank'=>array(
        'rule'=>'notEmpty', 
        'message'=>'Please Enter your Email!' 
       ) 
      ), 
      'phone'=>array(
        'body_must_not_be_blank'=>array(
          'rule'=>'notEmpty', 
          'message'=>'Please Enter your phone!' 
        ) 
      ), 
      'query'=>array(
        'body_must_not_be_blank'=>array(
          'rule'=>'notEmpty', 
          'message'=>'Please Enter your Query!' 
        ) 
      ) 
     )); 
    } 

\app\Model\categories 

<?php 
class Categories extends AppModel { 


public $validate = array(
     'name'=>array(
      'title_must_not_be_blank'=>array(
       'rule'=>'notEmpty', 
       'message'=>'Please Enter your Name!' 
      ) 
      ), 
     'email'=>array(
      'body_must_not_be_blank'=>array(
       'rule'=>'notEmpty', 
       'message'=>'Please Enter your Email!' 
      ) 
     ), 
     'phone'=>array(
       'body_must_not_be_blank'=>array(
         'rule'=>'notEmpty', 
         'message'=>'Please Enter your phone!' 
       ) 
     ), 
     'query'=>array(
       'body_must_not_be_blank'=>array(
         'rule'=>'notEmpty', 
         'message'=>'Please Enter your Query!' 
       ) 
     ) 
    ); 
} 


    \app\View\admins\index.ctp 

<h2>Add a Post</h2> 
<?php 
echo json_encode($this->validationErrors); 

//<!--create the form 2parameter:the post model and the 2nd is the form is submitted to which action--> 
echo $this->Form->create('Admin', array('action'=>'index')); 
echo $this->Form->input('name');//<!--We have not specified the field so type becomes text as the according to the database field type--> 
echo $this->Form->input('email'); 
echo $this->Form->input('phone'); 
echo $this->Form->input('query');//<!--We have not specified the field so type becomes textarea as the according to the database field type--> 
echo $this->Form->end('Create a Post');//<!--ends the form and create the text on button the same as specified--> 
?> 

\app\View\admins\category.ctp 

<head> 
<title>Admin Panel</title> 
</head> 

<body> 
<div id="container"> 
<?php echo $this->element("header"); ?> 
<div id="content"> 
<?php echo $this->element("left-content"); ?> 
<div id="right-content"> 
<div id="upper"> 
<h3>Add SubCategory</h3> 
</div><!---upper end---> 

<?php 
echo $this->Form->create('Admin', array('class' => "righform")); 
$options = array(); 
?> 
<fieldset class="textbox"> 
<label class="cate"><span>Main Category :</span> 
<select name="parentId"> 
<?php foreach($name as $row){?> 
<option value="<?php echo $row['Categories']['id'];?>"><?php echo $row['Categories']['name']; ?></option> 
<?php } ?> 
</select> 
</label> 
<br /><br /><br /> 
<label class="cate"> 
<?php echo $this->Form->input('name'); ?> 
<br /><br /><br /> 
<label class="cate1"> 
<?php echo $this->Form->input('Description'); ?> 
<br /><br /><br /> 
<br /><br /><br /> 

<td><button class="button" type="submit">Submit</button></td> 
</fieldset> 
</form> 
</div> 
</div><!---main content end---> 
</div><!----content end----> 
<?php echo $this->element("footer"); ?> 
</div><!---container end----> 
</body> 
</html> 

在此先感謝。

回答

2

如果你的模型是通過關係連接讓說管理員有OnetoOne /多對多/一對多類別

關係可以通過

$this->Admin->Category->saveAll(array('validate'=> 'only')); // pass 'only' string not only 

驗證,或者如果你想使用與默認模型無關的任何模型,然後只需首先將其加載到當前控制器上,然後驗證它,讓說類別與當前模型沒有關係,然後簡單地

$this->loadModel('category'); 
$this->category->set($this->data); 
$this->category->saveAll(array('validate'=> 'only')); 

希望它會幫助你

+0

我不知道我有什麼問題,但它似乎不可能驗證預期的默認模型。 – Gagan

+0

問題在於你的模型沒有加載,首先在你要加載模型的地方調試你的代碼,你會從那裏得到一個解決方案 –