0

Question 1

I'm trying to add a form validation using the following link.

https://github.com/gfranko/Backbone.validateAll/blob/master/demos/index.html

The sample code is working perfectly. But in my case I have a form with corresponding View, when I click on the submit button how can I validate?

In my View's initialize() I have added the following code to enable validation for the fields.

var user = new User;

$('input').each(function() {
  new Field({el: this, model: user});
});
this.model.set({route: formData.route}, {validate: true, validateAll: false});

But it is not validating.

My idea is validate a form with backbone and submit to PHP.

Using the code in the link how can I achieve it?

Question 2

I tried some other kind fo validations, Model save() error callback is not working for it.

this.model.save({route:formData.route}, {
  error: function(){
    console.log('error!!!'); // Not showing for failed validation
  },
  success: function(){
    console.log('success!!!!!');
  }
});

What could be the reason?

San
  • 666
  • 7
  • 27

1 Answers1

0

The main ideia is you have validators to perform when you set something on Model. To do that, you'll need a code like this:

For example inside the model, you can check if the attribute has some value.

validation:
    route : [
        required: true
    ]

setNewValue:->
    @set("route":"AnyValue")

The validation function will validate all the rules before the value be attached on model.

keep on mind that it's really important do this kind of thing inside your model. Avoiding your views to have theses rules.

To perform the validation before you save, try to add.

postData:->
    validateModel = true
    if @isValid(validateModel)
        @save()....
rcarvalho
  • 790
  • 1
  • 4
  • 14
  • Thank you for your quick reply! I could not understand to which question you answered?! can you give some link to a form validation then submit to PHP? – San Jul 08 '14 at 14:13
  • I'm using this plugin to perform validations using backbone, it works perfectly to me https://github.com/thedersen/backbone.validation. – rcarvalho Jul 08 '14 at 14:20
  • Thank you very much! I have successfully implemented backbone.validation!! – San Jul 08 '14 at 15:01
  • How to validate select box? – San Jul 15 '14 at 12:22
  • http://stackoverflow.com/questions/24533805/select-optionselected-text-and-attr-specified-is-deprecated-warning/24534042#24534042 – rcarvalho Jul 15 '14 at 12:24
  • Its modified as says in your link. but the validation is not happening only for select. HTML: Validaition COde: transportTypes: {required: true, msg: 'Please enter Type'} – San Jul 15 '14 at 13:26