开发者

Problem passing URL variables in post form submission with CakePHP FormHelper

开发者 https://www.devze.com 2022-12-20 02:48 出处:网络
I\'m writing my first CakePHP application and am just writing the second part of a password reset form where a user has received an email containing a link to the site and when they click it they\'re

I'm writing my first CakePHP application and am just writing the second part of a password reset form where a user has received an email containing a link to the site and when they click it they're asked to enter and confirm a new password.

The url of the page is like this:

/users/reset_password_confirm/23f9a5d7d1a2c952c01afacbefaba41a26062b17

The view is like:

<?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'); ?>

However this produces a form like:

<form id="UserResetPasswordConfirmForm" method="post" action="/users/reset_password_confirm/8">

The problem is the user id (8 in this case) is being added to the form action. It's not really a problem here, but when I want to pass through the hash to my controller:

function reset_password_confirm($static_hash=null) {
    // function body
}

$static_hash is now populated wit开发者_Python百科h 8 rather than the hash from the URL.

I know I could sort this out by creating the form tag myself rather than using $form->create but is there a more cakey way of doing this?


$form->create('User', array('action' => '…', 'id' => false));

Just explicitly set params you don't want passed to null or false. This is unfortunately a case where Cake tries to be a little too intelligent for its own good. ;o)

You could probably also do something like this to POST to the same URL again:

$form->create('User', $this->here);


How about passing it as a parameter instead of form data :

<?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');
?>

and in the controller :

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
}

}

Hope this help :)

0

精彩评论

暂无评论...
验证码 换一张
取 消