こんにちは教えてください。cakephpで関連モデルを検証するには? 1 つのコントローラーで複数のモデルを使用しており、コントローラー名は「AdminsController」です。これらは AdminsController のモデルです
$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>
前もって感謝します。