问题通过URL变量后表单提交与CakePHP的表单助手
-
20-09-2019 - |
题
我在写我的第一个CakePHP程序和我只是写在用户已经接收到包含链接到该网站的电子邮件密码重置形式的第二部分,当他们点击他们被要求输入并确认新的密码。
的页面的URL是这样的:
/users/reset_password_confirm/23f9a5d7d1a2c952c01afacbefaba41a26062b17
的视图是这样的:
<?php echo $form->create('User', array('action' => 'reset_password_confirm')); ?>
<?php
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->hidden('static_hash');
?>
<?php echo $form->end('Reset password'); ?>
然而,这产生一个形式,如:
<form id="UserResetPasswordConfirmForm" method="post" action="/users/reset_password_confirm/8">
问题是被添加到形式操作(在这种情况下8)的用户ID。这不是一个真正的问题在这里,但我想通过哈希传递给我的控制器:
function reset_password_confirm($static_hash=null) {
// function body
}
$static_hash
现在填充有8而不是从URL的哈希。
我知道我可以通过创建表单标签自己,而不是使用$form->create
排序了这一点,但有这样做的更cakey路?
解决方案
$form->create('User', array('action' => '…', 'id' => false));
就明确设置PARAMS你不希望传递给null
或false
。这是不幸的是,其中蛋糕试图为自己的好一点太聪明了的情况下。 ; O)
您可能还可以做这样的事情再次发布同一网址:
$form->create('User', $this->here);
其他提示
如何将它作为一个参数,而不是形式的数据:
<?php
echo $form->create('User', array('action' => 'reset_password_confirm', $static_hash));
echo $form->input('password', array('label' => 'Password'));
echo $form->input('confirm_password', array('type' => 'password', 'label' => 'Confirm password'));
echo $form->end('Reset password');
?>
和在控制器:
function reset_password_confirm($static_hash = null) {
// Check if form is submitted
if (!empty($this->data)) {
// if it submitted then do your logic
} else {
$this->set('static_hash', $static_hash); // Else, pass the hash to the view, so it can be passed again when form is submitted
}
}
希望这有助于:)
不隶属于 StackOverflow