Beispiel #1
0
  @Test
  public void listValidation() {
    Form<UserForm> userForm = Form.form(UserForm.class).bind(ImmutableMap.of("email", "e"));
    assertThat(userForm.errors().get("email"), notNullValue());
    assertThat(
        userForm.errors().get("email").get(0).message(),
        equalTo("This e-mail is already registered."));

    // Run it through the template
    assertThat(
        javaguide.forms.html.view.render(userForm).toString(),
        containsString("<p>This e-mail is already registered.</p>"));
  }
Beispiel #2
0
  /**
   * {@code posting}에 {@code original} 정보를 채우고 갱신한다.
   *
   * <p>when: 게시물이나 이슈를 수정할 떄 사용한다.
   *
   * @param original
   * @param posting
   * @param postingForm
   * @param redirectTo
   * @param updatePosting
   * @return
   */
  protected static Result editPosting(
      AbstractPosting original,
      AbstractPosting posting,
      Form<? extends AbstractPosting> postingForm,
      Call redirectTo,
      Callback updatePosting) {
    if (postingForm.hasErrors()) {
      return badRequest(postingForm.errors().toString());
    }

    if (!AccessControl.isAllowed(UserApp.currentUser(), original.asResource(), Operation.UPDATE)) {
      return forbidden(views.html.error.forbidden.render(original.project));
    }

    posting.id = original.id;
    posting.createdDate = original.createdDate;
    posting.authorId = original.authorId;
    posting.authorLoginId = original.authorLoginId;
    posting.authorName = original.authorName;
    posting.project = original.project;
    updatePosting.run();
    posting.update();

    // Attach the files in the current user's temporary storage.
    Attachment.moveAll(UserApp.currentUser().asResource(), original.asResource());

    return redirect(redirectTo);
  }
  // TODO change to use Contact.create() method
  @Security.Authenticated(Secured.class)
  public static Result newContact() {

    Form<Contact> filledForm = contactForm.bindFromRequest();

    String name = filledForm.data().get("name");
    String firstName = filledForm.data().get("firstName");
    String title = filledForm.data().get("title");
    String email = filledForm.data().get("email");
    String street = filledForm.data().get("street");
    String appendix1 = filledForm.data().get("appendix1");
    String appendix2 = filledForm.data().get("appendix2");
    String zipcode = filledForm.data().get("zipcode");
    String country = filledForm.data().get("country");
    String city = filledForm.data().get("city");
    String phone = filledForm.data().get("phone");
    String yearbook = filledForm.data().get("yearbookSubscription");
    String memberCategory = filledForm.data().get("memberCategory");
    String membershipSince = filledForm.data().get("membershipSince");

    Contact newContact = new Contact();
    newContact.name = name;
    newContact.firstName = firstName;
    newContact.title = title;
    newContact.email = email;
    newContact.street = street;
    newContact.appendix1 = appendix1;
    newContact.appendix2 = appendix2;
    newContact.zipcode = zipcode;
    newContact.city = city;
    newContact.country = country;
    newContact.phone = phone;

    if (yearbook.equals("true")) newContact.yearbookSubscription = true;
    newContact.memberCategory = memberCategory;

    for (int j = 0; j < ContactGroup.options().size(); j++) {
      String item = "belongsTo[" + j + "]";
      if (filledForm.data().get(item) != null) {
        ContactGroup cg =
            ContactGroup.find.byId((long) Integer.parseInt(filledForm.data().get(item)));
        newContact.belongsTo.add(cg);
      }
    }

    if (newContact.belongsTo.isEmpty())
      filledForm.reject("belongsTo[]", "Keine Sektion ausgewählt");

    // TODO Check fields for errors

    if (filledForm.hasErrors()) System.out.println(filledForm.errors().toString());

    newContact.membershipSince = membershipSince;
    newContact.createdAt = new Timestamp(new Date().getTime());
    newContact.lastEditedAt = newContact.createdAt;
    newContact.save();
    flash("success", "Kontakt " + newContact + " erstellt und gespeichert.");
    return redirect(routes.Application.contacts());
  }
  public static Result doRegistrieren() {

    Map<String, String[]> parameters = request().body().asFormUrlEncoded();

    Form<User> form = Form.form(User.class);

    form = form.bindFromRequest();

    if (form.hasErrors()) {
      return badRequest(views.html.registrieren.render("war nix. Fehler: " + form.errors()));
    } else {
      User user = form.get();

      if (!user.password.equals(parameters.get("password2")[0])) {
        return badRequest(views.html.registrieren.render("Passwörter stimmen nicht überein"));
      }
      // UserDB.init();
      //
      DBUser users = DBUser.get();
      // TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
      Date date = new Date();
      Date d = new Date();
      try {
        //  30-07-1987
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+1:00"));
        Logger.info("date from js " + parameters.get("datepicker")[0]);
        String datefromForm = parameters.get("datepicker")[0];
        // String datefromForm = "2013-05-04";
        date = sdf.parse(datefromForm);

        Logger.info(date.toString());
      } catch (Exception e) {
        Logger.info("Fehler beim Date \n" + e);
      }

      if (date == null) {
        return badRequest(views.html.registrieren.render("Date geht noch nicht...oder war leer!"));
      }

      // Date date = sdf.parse(user.date);

      User newUser =
          users.create(new User(user.email, user.password, user.nickname, user.fahrer, date));

      // +" remember: " +user.remember);
      // UserDB userDB = UserDB.get();
      // User userDB = UserDB.get().validateUser(user.email,
      // user.password);
      if (newUser != null) {

        return ok(views.html.loginform.render(""));
      } else {
        // return badRequest("falsche Angaben");
        return badRequest(views.html.registrieren.render("E-Mail existiert bereits"));
      }
    }
  }
Beispiel #5
0
  // Makes a nice flat list of all the errors in a filled form. The way errors are stored in Form<?>
  // is sort of redundant; it's a map of "field" with a value of List<ValidationError>. But each
  // ValidationError
  // object has its field in it, making the whole map redundant. So we just want a list of those
  // ValidationError objects.
  protected static List<ValidationError> getErrorsFromForm(Form<?> form) {
    List<ValidationError> errors = new ArrayList<ValidationError>();

    for (List<ValidationError> fieldErrors : form.errors().values()) {
      for (ValidationError error : fieldErrors) {
        errors.add(error);
      }
    }

    return errors;
  }
  public static Result signUp() {
    Form<UserAccount> filledSignUpForm = signUpForm.bindFromRequest();
    if (filledSignUpForm.hasErrors()) {
      return ok(filledSignUpForm.errors().toString());
    }

    UserAccount userAccount = filledSignUpForm.get();

    userAccount.save();

    session().clear();
    session("userId", String.valueOf(userAccount.id));
    session("name", userAccount.name);

    return redirect("/selectApartment");
  }
Beispiel #7
0
 public static Result newTeam() {
   Form<Team> filledForm = teamForm.bindFromRequest();
   if (filledForm.hasErrors()) {
     System.out.println("Errors: " + filledForm.errors());
     return badRequest(
         views.html.index.render(
             Team.all(),
             teamForm,
             Category.all(),
             categoryForm,
             Interview.all(),
             interviewForm,
             Application.getLoggedinUser()));
   } else {
     filledForm.get().save();
     return redirect(routes.Application.index());
   }
 }
Beispiel #8
0
  @Security.Authenticated(Secured.class)
  public static Result addNewStudAdvertisementForm(Long src) {
    Form<StudentAdvertisementForm> adForm =
        Form.form(StudentAdvertisementForm.class).bindFromRequest();

    if (adForm.hasErrors()) {

      System.out.println("ERRORS");
      System.out.println(adForm.errorsAsJson());
      System.out.println(adForm.errors().entrySet());

      return badRequest(
          postNewStudentAdvertisement.render(
              Student.find.byId(request().username()), adForm, null, src));
    } else {

      String description = adForm.get().description;
      String studies = adForm.get().studies;
      boolean testAd = adForm.get().test;

      if (testAd) {
        System.out.println("CREATED TESTAD");
      }

      StudentAdvertisement.create(
          Student.find.byId(request().username()), studies, description, testAd);
      if (src == 1) {
        return ok(
            viewAdvertisements.render(
                Student.find.byId(request().username()),
                StudentAdvertisement.find.all(),
                TutorAdvertisement.find.all()));
      } else {
        return ok(
            viewOwnAdvertisements.render(
                Student.find.byId(request().username()),
                StudentAdvertisement.find.all(),
                TutorAdvertisement.find.all()));
      }
    }
  }
  // TODO Automatic binding of owner is still missing at the moment
  @Security.Authenticated(Secured.class)
  public static Result newContactGroup() {
    User user = getCurrentUser();
    if (!user.isAdmin) return redirect(routes.Application.contacts());

    Form<ContactGroup> filledForm = contactGroupForm.bindFromRequest();

    if (filledForm.hasErrors()) {
      System.out.println(filledForm.errors().toString());
      flash("error", "Bitte korrigieren sie ihre Eingaben!");
      return badRequest(
          views.html.addContactGroup.render(filledForm, getCurrentUser(), ContactGroup.find.all()));
    } else {

      ContactGroup.create(filledForm.get());
      flash("success", "Kontaktgruppe " + filledForm.get().name + " erstellt.");
      if (User.findByEmail(request().username()).isAdmin) {
        // manual binding of owner
        ContactGroup.find.ref(filledForm.get().id).addOwner(User.findByEmail(request().username()));
      }
      return redirect(routes.Application.contacts());
    }
  }