liveBook · Manning

Business rule

As explained in Spring in Action, Sixth Edition

A validation rule is a constraint that determines whether submitted data satisfies a required condition.

A validation rule is a constraint that determines whether submitted data satisfies a required condition. In a web form, rules can reject missing or incorrectly formatted values before the application accepts them. section 2.3

Spring supports declaring rules with the JavaBean Validation API instead of filling controller methods with repetitive field-by-field checks. Add the Spring Validation starter, annotate the domain-object properties, trigger validation in controller methods, and display any errors in the form. section 2.3

A taco-order form can use rules such as:

  • Require a taco name of at least five characters and at least one selected ingredient. section 2.3.1
  • Require delivery name and address fields, and check payment values for an acceptable card number, expiration format, and CVV length. section 2.3.1
Listing 2.11 Adding validation to the Taco domain class
package tacos;
import java.util.List;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import lombok.Data;
 
@Data
public class Taco {
 
  @NotNull
  @Size(min=5, message="Name must be at least 5 characters long")
  private String name;
  @NotNull
  @Size(min=1, message="You must choose at least 1 ingredient")
  private List<Ingredient> ingredients;
 
}

See the Taco class with property-level annotations for required values and minimum lengths. section 2.3.1

Rules can check format without confirming real-world validity: the card-number check can catch mistakes or deliberately bad data, but does not establish that the number belongs to an account or can be charged. Forms can display field-specific messages when validation fails. section 2.3.1 section 2.3.3

How Spring in Action, Sixth Edition covers it

How deep it goes
Declares property constraints and connects controller validation to displayed errors, but stops short of verifying payment-account validity.
Examples
Uses taco names and ingredients, delivery addresses, and credit-card number, expiration, and CVV fields.
What you can do after
Apply JavaBean Validation API annotations and Spring MVC validation to check submitted form fields and show errors.
How it connects
Connects JavaBean Validation API annotations, Spring MVC controllers, Hibernate Validator constraints, and form error display.

Also explained in

Compare how 8 books explain business rule →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.