Example usage for org.springframework.validation BindingResult hasFieldErrors

List of usage examples for org.springframework.validation BindingResult hasFieldErrors

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult hasFieldErrors.

Prototype

boolean hasFieldErrors();

Source Link

Document

Are there any field errors?

Usage

From source file:org.duracloud.account.app.controller.AccountGroupsController.java

@RequestMapping(value = GROUPS_PATH, method = RequestMethod.POST)
@Transactional/*from  w  ww . j av  a2s  . com*/
public String modifyGroups(@PathVariable Long accountId, Model model,
        @ModelAttribute(GROUPS_FORM_KEY) @Valid GroupsForm form, BindingResult result) throws Exception {

    AccountService as = this.accountManagerService.getAccount(accountId);
    GroupsForm.Action action = form.getAction();

    if (action == Action.ADD) {
        String name = form.getGroupName();
        String groupName = DuracloudGroup.PREFIX + name;
        try {
            duracloudGroupService.createGroup(groupName, accountId);

        } catch (InvalidGroupNameException e) {
            if (groupName.equalsIgnoreCase(DuracloudGroup.PUBLIC_GROUP_NAME)) {
                result.rejectValue(GROUP_NAME_KEY, GROUP_NAME_RESERVED_ERROR_CODE, GROUP_NAME_RESERVED_MESSAGE);

            } else {
                result.rejectValue(GROUP_NAME_KEY, GROUP_NAME_INVALID_ERROR_CODE, GROUP_NAME_INVALID_MESSAGE);
            }

        } catch (DuracloudGroupAlreadyExistsException e) {
            result.rejectValue(GROUP_NAME_KEY, GROUP_NAME_EXISTS_ERROR_CODE, GROUP_NAME_EXISTS_MESSAGE);
        }

        if (!result.hasFieldErrors()) {
            return formatGroupRedirect(accountId, groupName, "/edit");
        }
    } else {
        String[] groups = form.getGroupNames();
        if (groups != null) {
            for (String name : groups) {
                DuracloudGroup group = duracloudGroupService.getGroup(name, accountId);
                removeGroup(group, accountId);
            }
        }
    }

    addGroupsObjectsToModel(as, model);

    return GROUPS_VIEW_ID;
}

From source file:fragment.web.RegistrationControllerTest.java

@Test
public void testRegisterNotAcceptedTerms() throws Exception {
    MockHttpServletRequest mockRequest = getRequestTemplate(HttpMethod.GET, "/portal/register");
    UserRegistration registration = new UserRegistration();
    registration.setCountryList(countryService.getCountries(null, null, null, null, null, null, null));
    AccountType disposition = accountTypeDAO.getOnDemandPostPaidAccountType();
    BindingResult result = setupRegistration(disposition, registration);
    beforeRegisterCall(mockRequest, registration);
    String view = controller.register(registration, result, "abc", "abc", map, null, status, mockRequest);
    Assert.assertEquals("register.moreuserinfo", view);
    Assert.assertFalse(status.isComplete());
    Assert.assertTrue(result.hasFieldErrors());
    Assert.assertTrue(result.getFieldErrorCount() == 1);
    Assert.assertEquals("AssertTrue", result.getFieldError("acceptedTerms").getCode());
}

From source file:edu.zipcloud.cloudstreetmarket.core.util.ValidatorUtil.java

public static void raiseFirstError(BindingResult result) {
    if (result.hasFieldErrors()) {
        throw new IllegalArgumentException(result.getFieldError().getDefaultMessage());
    } else if (result.hasGlobalErrors()) {
        throw new IllegalArgumentException(result.getGlobalError().getDefaultMessage());
    } else if (result.hasErrors()) {
        throw new IllegalArgumentException(result.getAllErrors().get(0).getCode());
    }//from  www  .  j  a  va  2  s .  c  o m
}