1

I am using two different messages for two different validation such as

validation: {
        email: [{
            required: true,
            msg: 'Please enter Email Id.'
        }, {
            pattern: 'email',
            msg: 'Please enter valid Email id.'
        }]
    }

But if i submit the form without entering the email value it triggers the first validation after that if give the wrong input the submits the form it triggers the second one.

Issue is that the first validation is not removed that is, it still shows as "Please enter Email Id." even though that validation is not triggered second time along with the second one by its side.

jerin
  • 326
  • 1
  • 3
  • 11

1 Answers1

0

That validation code looks perfectly fine, but that's only one piece of the puzzle. I would suggest looking at the Backbone.Validation examples (and the code behind them). The examples offer two different approaches for handling validation rules similar to yours.

Here are the pieces that get the email validation working in the "Basic" scenario. First, a snippet of the template:

  <div class="control-group">
    <label class="control-label" for="email">Email</label>
    <div class="controls">
      <input type="text" class="input-xlarge" id="email" name="email" data-error-style="inline">
    </div>
  </div>

Note that the email input specifies "inline" as the data-error-style. That plays into the default invalid callback from backbone.validation.bootstrap.js:

else if (control.data("error-style") === "inline"){
  if (group.find(".help-inline").length === 0){
    group.find(".controls").append("<span class=\"help-inline error-message\"></span>");
  }
  var target = group.find(".help-inline");
  target.text(error);
}

That code adds an error message to the email element, or replaces the error text if an inline error message already exists.

You can check through more of the code to get a handle on what's going on, but the key point is that the model validation parameters are only part of the story. The other pieces are the template setup and valid/invalid callbacks.

ajk
  • 4,473
  • 2
  • 19
  • 24