我想创建一个规则,该规则应该从输入数组中验证是否需要atelast,并且应该是数字。输入数组为goal_target[#]
,其中#由后端处理,具有唯一值。
我在模型中创建此规则。至少需要在控制器中处理一个并在数据集中设置值。这是控制器动作:
//Removing initial zeros "0" $this->request->data['goal_target'] = array_map(function($v) { return ltrim($v, '0'); }, $this->request->data['goal_target']); //Removing empty values from input array $this->request->data['goal_target'] = array_filter($this->request->data['goal_target']); //Creating a data set for passing in model $this->request->data['Goal']['targets'] = $this->request->data['goal_target']; $this->Goal->set($this->request->data); if(!$json_response && $this->Goal->AddValidate()) { //Adding goal } else { $json_response['validations'] = $this->Goal->validationErrors; } echo json_encode($json_response);
在我的模态中:
public function AddValidate() { $validateAdd = array( 'targets'=>array( 'numeric' => array( 'rule' => 'numeric', 'allowEmpty' => true, 'message' => 'Numbers only' ) ) ); $this->validate = $validateAdd; return $this->validates(); }
即使对数值也无法正常工作。它始终返回消息Numbers only
。我做错了什么请告诉我。另外请告诉我是否可以在上述规则中获得所需的一个。