使用Play!2将信息从一页转移到另一页 [英] Transferring information from one page to another using Play!2

查看:59
本文介绍了使用Play!2将信息从一页转移到另一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为网站创建注册页面.注册过程分为两个阶段:

I'm trying to create a signup page for a website. The signup process has 2 stages:

  1. 用户在页面上的表单上输入他们的姓名,电子邮件和密码.他们单击下一步,然后

  1. The user enters their name, email, password on a form on a page. They click next, then

用户在新页面上输入其他信息(也许上传照片),然后单击提交".

The user enters additional information on a new page (maybe upload a photo, whatever) and clicks submit.

所有这些信息(来自第1页和第2页)随后用于创建新用户.一个如何将信息从一页存储到另一页?

All this information (from page 1 and 2) is then used to create a new user. How can one store information from one page to another?

一种可行的方法可能是在第二页的表单中创建额外的字段,并用第一页中的信息填充它们,但这似乎有些不客气.有推荐的解决方法吗?

One way that could work might be to create extra fields in the form on the second page and fill them with info from the first one, but it seems a bit hacky. Is there a recommended way to go about it?

推荐答案

与用户的每次交互都不需要使用Form.class,它是很好的帮助者,但是...只是帮助者.

You don't need to use Form.class for each interaction with user, it's nice helper, but... just helper.

将注册过程划分为几个动作,每个动作仅负责编辑模型的一部分

Divide your registration process to few actions, each will be responsible for editing only part of the model

  1. 您的第一个-默认视图应为最大,并且应使用Form.classConstraints批注来验证数据.当然,您需要设置其他操作,例如new/createedit/update等.发送该表单后,该帐户将保存在数据库中,因此您可以通过其他操作对其进行更改,以通过唯一ID标识该帐户.
  2. 您的下一个视图(假设仅是change password表单)可以使用其他Form.class BUT ,如果它很容易并且具有特殊的验证规则(例如密码奇怪等),则可以可以只使用ie. editPassword/updatePassword一组操作并手动更新您的用户对象:

  1. Your first - default view should be largest and should use Form.class and Constraints annotations for validating data. Of course you'll need additional actions' set like new/create or edit/update etc. After sending that form, the account is saved in DataBase, so you can just make changes to it in other actions identifying it by unique ID.
  2. Your next view (let's say that's only change password form) can use other Form.class BUT if it's quite easy and has special validation rules (like password strange etc.) you can just use ie. editPassword/updatePasswordset of actions and update your user object manually:

public static Result updatePassword(Integer user_id){
    User user = User.find.byId(user_id);
    DynamicForm formData = form().bindFromRequest();

    if (!customValidatorMethod(formData.password)){
         return badRequest("Password was not changed");
    }

    user.password = formData.password;
    user.update(user_id);
    return ok("Password changed);
}

接下来,您可以根据需要创建任意多个步骤.

Next you can create as many steps as you want.

注意:不要在会话数据中保存用户名和密码,因为某些可以对他的老板使用它....而是使用第一步中创建的用户的id,其他您可以添加诸如registrationStepregistrationId(随机哈希)之类的简单字段,以在所有必需的注册步骤之间标识新用户.完成全部注册过程后,您将删除该会话数据,同时您可以通过简单地检查步骤号来检查用户是否完成注册.

Note: Don't save username and password in session data, cause some smartie can use it against his boss.... Instead use id of the user created in first step, additional you can add simple fields like registrationStep or registrationId (random hash) for identifying new user between all required registration steps. You'll remove that session data after full registration process, meanwhile you can check if user finished registration by simple checking the step number.

这篇关于使用Play!2将信息从一页转移到另一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆