我为register
中的MembersController
操作创建了一个vue。但是当我使用来自memberType
模型的值添加名为MemberType
的字段时。我无法验证此字段。我对任何建议持开放态度,谢谢! :)
查看 add
<?php
echo $this->Form->create('Member', array(
"type" => "file",
"url" => array("controller" => "Member", "action" => "add"),
"inputDefaults" => array("class" => "form-control", "div" => "form-group")
));
echo $this->Form->input("Member.NAME", array(
"error" => array(
"attributes" => array("escape" => false, "wrap" => "span", "class" => "alert-danger")
)
));
echo $this->Form->input("Membertype", array(
"type" => "select",
"empty" => true,
"error" => array(
"attributes" => array("escape" => false, "wrap" => "span", "class" => "alert-danger")
)
));
echo $this->Form->end(array(
"label" => "Register",
"class" => "btn btn-primary"
));
?>
控制器 MemberController
<?php
class MembresController extends AppController {
public function register(){
$this->set('membertypes', $this->Member->MemberType->find('list'));
if ($this->request->is('post')) {
$this->Membre->create();
if ($this->Membre->save(array(
"Membre" => array(
"NAME" => $this->request->data["NAME"],
"type_id" => $this->request->data["Membertype"],
)
))) {
$this->Session->setFlash(
'Registred',
"default",
array("class" => "alert alert-success")
);
return $this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(
"Not registered",
"default",
array("class" => "alert alert-danger")
);
}
}
}
Members
表:
create table Member
(
ID int not null AUTO_INCREMENT,
type_id int not null,
NOM varchar(255) not null,
primary key (ID)
);
MemberType
表:
create table MemberType
(
ID int not null AUTO_INCREMENT,
NAME varchar(255) not null,
primary key (ID)
);
答案 0 :(得分:0)
这可能不是官方的方法,但如果它们嵌套在您的数据数组中,您可以验证这些字段。
$saveData = [
'YourSavingModel' => [
'name' => 'blub',
'MemberType' => [whatever]
]
现在您可以将'MemberType'添加到$ model-&gt; validate中,它还会根据您的规则验证这些值。
请注意,它不会以这种方式保存信息。
验证后,您需要将阵列更改为现在的方式。