简体   繁体   中英

Play framework 2.4 form fill does not work

In Play Framework 2.4 Java, I need to fill a form using the following code, but it does not work. The output value of usereditform.field("email").value() is null. Anyone know why?

SignupClz signupobj = new SignupClz();
signupobj.email="abcd@efgh.com";
signupobj.name="abcd";
signupobj.password = "eoijf";

Form<SignupClz> usereditform = Form.form(SignupClz.class).fill(signupobj);
System.out.println(usereditform.field("email").value());

This code is correct. Please look at other points - maybe you did not recognize "abcd@efgh.com" output in the console.

For example I have the similar code and it works:

EditForm rawEditForm = new EditForm();
rawEditForm.title = boxModel.title;
rawEditForm.body = boxModel.body;
rawEditForm.id = id;

Form<EditForm>  editForm = Form.form(EditForm.class).fill(rawEditForm);
Logger.info("Title: " + editForm.field("title").value());

I am learning PlayFramework and stumbled upon this question.

For PlayFramework 2.8 form field values cannot be directly accessed, be it reading or filling(writing) information. You have to explicity tell it when building the form, like this :

 @Inject
 FormFactory formFactory;

 Form<SignupClz> usereditform = FormFactory.form(SignupClz.class) // 
                                .withDirectFieldAccess(true)
                                .fill(signupobj);

 System.out.println(usereditform.field("email").value());

Note that in this version forms are built using FormFactory , make sure you inject it or add it to your code otherwise.

Details in their official documentation https://www.playframework.com/documentation/2.8.x/JavaForms#Defining-a-form:~:text=Instead%20of%20enabling%20%E2%80%9Cdirect%20field%20access%E2%80%9D%20for%20all%20forms%2C%20you%20can%20enable%20it%20only%20for%20specific%20ones%3A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM