Another way to handle this is to leverage the wicket validation process by extending FormComponentPanel.

Your userEditPanel would be composed of individual textfields whose models are not linked to your IModel<User>. Then implement the convertInput method that builds a new User object from the validated field values when the form is submitted.

For example:

public class UserEditPanel extends FormComponentPanel<User> {

    private TextField<String>emailField;
    /**
     * @param id
     */
    public UserEditPanel(String id, IModel<User>userModel) {
        super(id, userModel);

        emailField = new  TextField("emailField", new Model<String>(""));

        emailField.add(new EmailValidator());
    }

    @Override
    protected void convertInput() {

        /**
         * Build up a new User instance from the values in the fields.
         *
         */

        User u = new User(emailField.getModelObject(), ...);


        setConvertedInput(u);


    }

    /*
* Here we pull out each field from the User if it exists and put the contents into the fields.
     */
    @Override
    protected void onBeforeRender() {

        User u = this.getModelObject();

        if (u != null) {
            // copy the field values into the form fields.
            this.emailField.setModelObject(u.getEmailAddress());
        }
    }


}

I only implemented for one field but you can see that if you refactored the User.testSetEmail() into a static utility class then you could use it inside the EmailValidator(). This would allow you to show errors as required to ensure the fields of the User object were entered in correctly. Further you can add Validators directly to the UserEditPanel that can be used to validate the built User object.

I find this useful especially for cases like you describe with multiple layers of IModel's; it allows the complexity to be internalized and then users of the panel don't need to care about the internals only that when it validates properly you call .getModelObject() and get the valid User object back.

e.g.

UserEditPanel userEditPanel = new UserEditPanel ("userPanel", new Model<User>());

add (form = new Form("form") {

@Override
            protected void onSubmit() {

// this is only called if there are no validation errors in the form fields
// the validation logic built into the UserEditPanel is implicity used and will prevent this method from being called
// if an error is detected.

                User u = userEditPanel.getModelObject();

                userService.save(u);
            }
    });

form.add (userEditPanel);


Regards,

Mike






Thanks James I'll investigate on extending PropertyModel.

Currently I'm doing the following:

public class UserRegistrationPage extends WebPage {
        @SpringBean
        private UserService userService;

        private FeedbackPanel feedbackPanel;
        private UserDto userDto; // only has the User properties
        
        @SuppressWarnings("unchecked")
        public UserRegistrationPage() {
                feedbackPanel = new FeedbackPanel("feedback");
                userDto = new UserDto();
                CompoundPropertyModel userDtoModel = new 
CompoundPropertyModel(userDto);
// bind to the DTO
                
                Form registrarForm = new Form("registerForm", userDtoModel){
                        @Override
                        protected void onSubmit() {
                                try {
                                         // Create a real User and obtain the
data from the DTO
                                        User user = new User(userDto.getEmail(),
                                                                                
userDto.getName(),
                                                                                
userDto.getPassword(),
                                                                                
userDto.getBirth());
                                        userService.save(user); // service 
calls the dao which actually saves
to DB
                                } catch (Exception e) { // The Businness 
Exception has the message error
                                        feedbackPanel.warn(e.getMessage());
                                }
                        }
                };      

                registerForm.add(new TextField("email").setRequired(true)); // 
form binded
to the DTO properties
                ...


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to