第一个问题是我的表中有以下验证器和公共函数
UsersTable.php
$validator
->scalar('name')
->maxLength('name', 45)
->requirePresence('name', 'create')
->notEmptyString('name', 'You must enter a name for the user.');
$validator
->add('name', 'custom', array('rule' => 'checkExistingUser', 'message' => 'This user already appears to be in the system.', 'on' => 'create'));
public function checkExistingUser($value,$context)
{
return $this->find('all', ['conditions' => ['Users.name' => $context['data']['name'], 'Users.user_type_id' => $context['data']['user_type_id']]])->count() < 1 ;
}
当我保存下面的表单时,我收到消息“方法checkExistingUser不存在”。当在表模型中明确定义该方法时,为什么不识别该方法?我想念什么吗?
add.ctp
<?php echo $this->Form->create($user);?>
<fieldset>
<legend><?php echo __('Add User'); ?></legend>
<?php
echo $this->Form->control('name', ['type' => 'text']);
echo $this->Form->control('user_type_id');
echo $this->Form->control('owner', array('type' => 'text', 'label' => "Owner Name"));
echo $this->Form->control('owner_contact', array('type' => 'text', 'label' => "Owner Contact (phone, email etc)"));
echo $this->Form->control('description', ['type' => 'textarea']);
echo $this->Form->control('ia_exception', array('type' => 'text', 'label' => "IA Exception Number"));
echo $this->Form->control('is_manual', array('type' => 'checkbox', 'label' => "Password Updated Manually"));
echo $this->Form->control('Environment', ['type' => 'select', 'multiple' => 'true', 'label' => 'Environment(s)']);
?>
</fieldset>
<div class="buttons">
<?php
echo $this->Form->button('Save', ['type'=> 'submit', 'name' => 'submit']);
echo $this->Form->button('Cancel', ['type' => 'button', 'name'=>'cancel', 'onClick' => 'history.go(-1);return true;']);
echo $this->Form->end();
?>
</div>
UsersController.php
function add() {
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->set('The user has been saved');
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->set('The user could not be saved. Please, try again.');
}
}
$userTypes = $this->Users->UserTypes->find('list');
$changeSteps = $this->Users->ChangeSteps->find('list');
$environments = $this->Users->Environments->find('list');
$this->set(compact('user','userTypes', 'changeSteps', 'environments'));
}
第二个问题是当我尝试提交表单以检查验证器对于空的 name 字段是否正常工作时,我没有收到消息“您必须输入用户名”。相反,我收到一条消息,指出“此字段为必填字段”。为什么不显示来自notEmptyString的消息?哪里是“此字段必填”?
答案 0 :(得分:0)
对于第一个问题,我必须在验证器中添加一个提供程序。
我改变了
package webproxy
import (
"fmt"
"io"
"net/http"
)
type proxyHandler struct {
requestHook RequestHookFunc
responseHook ResponseHookFunc
}
func getHandler(requestHookIn RequestHookFunc, responseHookIn ResponseHookFunc) http.Handler {
fmt.Printf("%#v\n", requestHookIn)
return &proxyHandler{requestHook: requestHookIn, responseHook: responseHookIn}
}
// ServeHttp
func (handler *proxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Printf("%#v\n", handler.requestHook)
if handler.requestHook != nil {
fmt.Println("Yaha")
handler.requestHook(r)
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
io.WriteString(w, "This HTTP response has both headers before this text and trailers at the end.\n")
}
对此
$validator
->add('name', 'custom', array('rule' => 'checkExistingUser', 'message' => 'This user already appears to be in the system.', 'on' => 'create'));
答案 1 :(得分:0)
打补丁时要注意自定义的验证方法,因为 Cake 期望返回字符串,否则会渲染默认消息。 例如,如果我们在打补丁时使用自定义验证函数
// in a Controller
$this->Users->patchEntity($user, $data, ['validate' => 'custom');
同样适用于关闭。
// in UserTable.php
public function validationCustom(Validator $validator) {
$validator = $this->validationDefault($validator);
$validator
->minLength('password',8,'At least 8 digits');
$validator->add('password',
'strength_light',[
'rule' => 'passwordCheck',
'provider' => 'table',
'message' => 'At least a number and a capital letter'
]
);
return $validator;
}
public function passwordCheck ($value = "") {
return preg_match("/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}/",$value);
}
这将返回默认消息而不是自定义消息(“至少..”),因为我们设置了一个可调用的 not-cakephp 函数作为自定义验证的规则,因此该消息应该由被调用的函数返回:
public function passwordCheck ($value = "") {
if (!preg_match("/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}/",$value))
return "At least a number and a capital letter";
}