你好,请告诉我。 如何在cakephp中验证相关模型? 我在一个控制器中使用多个模型,控制器名称是'AdminsController'。 这些是AdminsController中的Model
$uses=array('index','Static_page_view','Admin','Categories','Products', 'contact','User','Rams');'
现在我要验证'类别'(模型)。我已经定义了一组验证规则。但不能确认。当我使用'Admin'(MODEL)时它完美无缺。
<?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>
先谢谢。
答案 0 :(得分:2)
如果您的模型通过关系连接,请说管理员与类别有一个OnetoOne / manytomany / onetomany关系
你可以通过
验证它$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'));
希望它能帮到你
答案 1 :(得分:0)