如何在cakephp中验证没有模型名称的表单字段

时间:2014-02-17 07:26:13

标签: php cakephp cakephp-2.3

如何在没有型号名称的情况下验证表单数据。当我打印$this->data时,我得到以下数组格式的数据。但字段不包含模型名称。

Array
 (
  [select_family] => Select
  [select_cores] => Select
  [select_color] => Select
  [select_back_color] => Select
  [select_panel] => Select
  [select_design] => Select
  [select_thickness] => Select
  [select_edge] => Select
  [quantity] => 
  [save] => Save

)

提前致谢。

3 个答案:

答案 0 :(得分:0)

您必须使用模型名称,因为验证不知道在哪里验证。

Ofcourse您可以在验证前设置模型名称(例如beforeValidation回调或控制器中的$this->Model->set()之前)

答案 1 :(得分:0)

如果您从UI获取数据,那么您只需要在控制器的操作中执行此操作

(
[select_family] => $this->request->data['x'],
[select_cores] => $this->request->data['x'],
[select_color] => $this->request->data['x'],
[select_back_color] => $this->request->data['x'],
[select_panel] => $this->request->data['x'],
[select_design] => $this->request->data['x'],
[select_thickness] => $this->request->data['x'],
[select_edge] => $this->request->data['x'],
[quantity] => 
[save] => Save

)

其中x是您的UI输入字段名称。

答案 2 :(得分:0)

您可以保存并验证这些数据结构:

默认值:

Array
(
    [ModelName] => Array
    (
        [fieldname1] => 'value'
        [fieldname2] => 'value'
    )
)

保存多条记录:

$data = array(
    array('title' => 'title 1'),
    array('title' => 'title 2')
);

因此,在验证/保存之前,请将数据数组转换为适当的格式。例如:

$data['ModelName'] = $this->data;
$this->save($data);

$data = array($this->data);
$this->saveMany($data);