你好,請告訴我。 如何驗證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>
在此先感謝。
soryy,因爲在這篇文章中只有一個型號已使用這篇文章因此未heapled得多,但在我的情況下,有多個型號被調用。 所以請再次提醒我。 謝謝。 – Gagan