Note that there are some explanatory texts on larger screens.

plurals
  1. POCakePHP requires two page loads to validate form
    primarykey
    data
    text
    <p>I'm writing a page where my users can change their account email and password. Here's the controller action and the view:</p> <pre><code># UsersController.php public function edit() { if($this-&gt;request-&gt;is('post')) { if($this-&gt;User-&gt;save($this-&gt;request-&gt;data)) { $this-&gt;Session-&gt;setFlash('Your account has been updated.'); $this-&gt;redirect(array('action' =&gt; 'edit')); } $this-&gt;Session-&gt;setFlash('There was a problem saving your account settings. Please try again.'); } // Auto populate form fields if(!$this-&gt;request-&gt;data) { $this-&gt;request-&gt;data = $this-&gt;User-&gt;find('first', array( 'conditions' =&gt; array('User.id' =&gt; $this-&gt;Auth-&gt;user('id')) )); } } # edit.ctp &lt;?php echo $this-&gt;Form-&gt;create('User'); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('currentPassword', array('between' =&gt; 'You must enter your password in order to make changes', 'type' =&gt; 'password', 'value' =&gt; '', 'autocomplete' =&gt; 'off')); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('email'); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('password', array('type' =&gt; 'password', 'between' =&gt; 'Must be atleast 6 characters', 'value' =&gt; '', 'autocomplete' =&gt; 'off')); ?&gt; &lt;?php echo $this-&gt;Form-&gt;input('confirmPassword', array('type' =&gt; 'password', 'value' =&gt; '', 'autocomplete' =&gt; 'off')); ?&gt; &lt;?php echo $this-&gt;Form-&gt;end('Save changes'); ?&gt; </code></pre> <p>Now, I would like to make the user enter their current password in order to make changes. In order for that to work, I need to run a validation check to make sure the password they entered in <code>currentPassword</code> matches what I have in the database. One of the validation rules in my <code>User</code> model is this:</p> <pre><code>'currentPassword' =&gt; array( 'custom' =&gt; array( 'rule' =&gt; 'validateCurrentPassword', 'message' =&gt; 'Incorrect password. Make sure you\'re using your current password.' ) ), </code></pre> <p>and the relevant function that gets called:</p> <pre><code>public function validateCurrentPassword($data) { debug($data); return false; } </code></pre> <p>So far so good, but there's some really weird behaviour. Cake only seems to validate this field after two page loads. For example, if I enter a wrong value and press "Save changes" the page refreshes, but no validation errors pop up. If I enter another wrong value, I get the validation errors. For some reason I need to submit the form twice for validation to occur.</p> <p>Can anyone work out why this is?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload