开发者

Validation performed on initial form load

I have a registration form that a user can access via invitation. For example, an invited user can go to /users/register/[invite_code] and get a registration form that is partially populated. I'm seeing something I didn't expect:

If the invite code is valid, we return the user structure and set that in $this->data so that the form is automagically pre-populated. The pre-population works, but when the form loads, the data validation errors are displayed as though I'd submitted the form. I see a lot of "[Field name] cannot be empty" when a value has been pre-populated.

In response to questions, the register action is called to both display and submit the form. The code looks like this:

public function register( $invite = null ) {
  # Handle a submitted registration
  if( !empty( $this->data ) ) {
    # Do stuff...

    if( $this->User->save( $this->data ) ) {
      $this->Session->setFlash( 'Welcome. Thanks for registering.', null, null, 'success' );
      $this->redirect( $this->Auth->redirect() );
    }
    else {
      $this->Session->setFlash( 'Oh noz. There\'s a problem with your registration.', null, null, 'validation' );
    }
  }

  # If an invite is passed, pull the user attached to that invite.
  if( !empty( $invite ) ) {
    # Attempt to pull the user that owns the invite

    if( empty( $user ) ) { # Unrecognized invite code
      # Do stuff...
    }
    else { # Invited user found
      if( !empty( $user['User']['password'] ) ) { # Invited user has already registered
        # Do stuff...
      }
      else { # This is the invited user
        $this->data = $user;
      }
    }
  }
}

Thanks.

UPDATE

After coming back to this, the problem comes when I assign the user to $this->data. When I remove that, no validation errors. Unfortunately, no pre-population either. I need the latter, so I need to find a way of setting the data points without triggering the error messages.

UPDATE 2

If I dump $this->User->invalidFields开发者_开发百科(), even as early as the first line of UsersController::beforeFilter(), the array is populated with invalids. How is this possible? What could I possibly be doing that early in the request to cause this?

UPDATE 3

I've also noticed that as I enter UsersController::beforeFilter(), the value of $this->User->id is already set. I have no idea where or why, but this seems significant. As far as I know, the first step into the actual application code is the relevant controller's beforeFilter method and the user id is set on the way in.


The controller loadModel() function, unfortunately, sets the id property in the model based on the first parameter on your action. That is a big annoyance that was removed in CakePHP 2.0. Nevertheless there is no code that will auto validate your fields after setting an Id, so my guess is that you have a component handling the auto validation or you have some code in your mode that will do it when the model is created.

If you cannot find the code that is auto validating your data, or do not want to change it, I suggest you override loadModel() in your controller to remove the id assigment, or just use a named parameter for your action.


1) Are you initially setting $this->data before $this->Model->save ?


It's weird, I already did that without any problem. If I understand correctly, it's after the last else that the form is displayed. You can check in the controller, if validation already failed when entering the block with this

echo debug($this->User->invalidFields() );
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜