Validation in spring

I am using spring mvc and hibernate.I have a form which has three fields id,sum and sum_amount .I have a table in database which has 2 column "id" and "Amount".Now I want to persist amount field in that table if sum(Amount) of a id attribute less than sum_amount field specified in form otherwise it will show an error.How can i do that validation?

   <form action="join" method="POST"  ajaxForm="ContentReplace" style="margin: 133px;">

        <label>Id</label>
        <input type="text" name="id" value="" /><br>  
        <label>Amount</label>
        <input type="text" name="amount" value="" /><br>  
        <label>Sum of Amount</label>  
        <input type="text" name="sum_amount" value="" /><br>    
        <input type="Submit" value="Search">

    </form> 

My model class is

@Entity
public class Amount {

@Id
Integer id;

Integer amount;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Integer getAmount() {
    return amount;
}

public void setAmount(Integer amount) {
    this.amount = amount;
}


}

Now i know about JSR validation and Spring custom validation but they validate model data.You can see that i need a validation which query database and show error in form field.How can i do that?

Upvotes: 0

Views: 45

Answers (1)

dimitrisli
dimitrisli

Reputation: 21391

It's perfectly reasonable to have injected a Service bean (that has an injected DAO bean) in your custom Validator to perform your lookup and based on that validation success or failure.

Upvotes: 1

Related Questions