0

When I call isValid() on my backbone model it always returns undefined even when the validation rules have been met or not. What could be going wrong here?

What could be the problem here and how can I fix it?

My Model:

Backbone.Model.extend({

    validation: {

            firstname: { required: true, msg: _('First Name is required').translate() }
        ,   lastname: { required: true, msg: _('Last Name is required').translate() }
    }
});

My view:

Backbone.View.extend({

        events: {
            'submit form[data-action="specify-pbt"]': 'specifyPBT'
        }

    ,   bindings: {
            '[name="firstname"]': 'firstname'
        ,   '[name="lastname"]': 'lastname'
        }

    ,   initialize: function(options) {
            this.model = options.model || new PBTSpecifyModel(); // The latter condition is always met

            BackboneCompositeView.add(this);
            BackboneFormView.add(this);
            Backbone.Validation.bind(this);
        }

    ,   specifyPBT: function (e)
        {
            var options = jQuery(e.target).serializeObject();

            console.log("model: ");
            console.log(this.model);  // Outputs the model: looks all correct
            console.log("isValid: ");
            console.log(this.model.isValid()); // Outputs undefined
            console.log("validationError: ");
            console.log(this.model.validationError); // Outputs null
        }

    ,   getContext: function()
        {
            return {

            }
        }
    })
});
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
sazr
  • 24,984
  • 66
  • 194
  • 362
  • Use method `isValid(true)` – Przemek eS Apr 13 '17 at 07:13
  • I see you've asked other questions already without giving feedback here. If any of the answers have helped you, you could upvote and/or accept one. If no answer was enough, it's encouraged to write and accept your own answer. – Emile Bergeron May 18 '17 at 12:48

2 Answers2

0

Please read the documentation before asking questions.

isValid model.isValid()
Run validate to check the model state.

validate model.validate(attributes, options)
This method is left undefined and you're encouraged to override it with any custom validation logic

You're using a plugin, not validation methods of backbone itself. Follow the plugins instructions on how to check validation state.

T J
  • 42,762
  • 13
  • 83
  • 138
0

Backbone.Validation has a documentation entry on its version of isValid which differ from the default Backbone one.

Check to see if an attribute, an array of attributes or the entire model is valid.

isValid returns undefined when no validation has occurred and the model has validation (except with Backbone v0.9.9 where validation is called from the constructor), otherwise, true or false.

If you don't pass an argument, the properties defined by the attributes bind option will be validated. If no attributes option is used there will be no validation.

var isValid = model.isValid();

If you pass true as an argument, this will force a validation before the result is returned:

var isValid = model.isValid(true);

If you pass the name of an attribute or an array of names, you can check whether or not the attributes are valid:

// Check if name is valid
var isValid = model.isValid('name');

// Check if name and age are valid
var isValid = model.isValid(['name', 'age']);

The default isValid of a Backbone Model will always return a boolean.

Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129