对初始表单加载执行验证

发布于 2024-10-26 18:56:58 字数 1864 浏览 0 评论 0原文

我有一个用户可以通过邀请访问的注册表单。例如,受邀用户可以访问 /users/register/[invite_code] 并获取部分填充的注册表单。我看到了一些我没有想到的东西:

如果邀请代码有效,我们将返回用户结构并将其设置在 $this->data 中,以便自动预填充表单。预填充有效,但是当加载表单时,会显示数据验证错误,就像我提交了表单一样。当预先填充值时,我看到很多“[字段名称]不能为空”。

为了回答问题,调用注册操作来显示和提交表单。代码如下所示:

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;
      }
    }
  }
}

谢谢。

更新

回到这里后,当我将用户分配给 $this->data 时,问题就出现了。当我删除它时,没有验证错误。不幸的是,也没有预填充。我需要后者,所以我需要找到一种设置数据点而不触发错误消息的方法。

更新2

如果我转储$this->User->invalidFields(),甚至早在UsersController::beforeFilter()<的第一行/code>,数组中填充了无效值。这怎么可能?我在请求的早期可能做了什么导致了这种情况?

更新3

我还注意到,当我输入UsersController::beforeFilter()时,$this->User->id的值代码> 已设置。我不知道在哪里或为什么,但这似乎很重要。据我所知,进入实际应用程序代码的第一步是相关控制器的 beforeFilter 方法,并且用户 id 是在其中设置的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-11-02 18:56:58

不幸的是,控制器 loadModel() 函数根据操作的第一个参数设置模型中的 id 属性。这是一个很大的烦恼,在 CakePHP 2.0 中被删除了。然而,没有代码可以在设置 Id 后自动验证您的字段,所以我的猜测是您有一个处理自动验证的组件,或者您的模式中有一些代码可以在创建模型时执行此操作。

如果您找不到自动验证数据的代码,或者不想更改它,我建议您在控制器中重写 loadModel() 以删除 id 分配,或者仅使用命名参数来执行操作。

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.

回忆追雨的时光 2024-11-02 18:56:58

1)您最初是否在 $this->Model->save 之前设置 $this->data ?

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

花想c 2024-11-02 18:56:58

奇怪的是,我已经这样做了,没有任何问题。如果我理解正确的话,表单是在最后一个 else 之后显示的。您可以检查控制器,如果在输入块时验证已经失败

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

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() );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文