2013-07-09 2 views
1

안녕하세요, 백본의 유효성 검사가 바뀌 었음을 알고 작업을 수행하려고합니다. 누군가는 무엇이 잘못되었는지 알아 차립니다. 유효성을 검사하는 JSfiddle 링크도 있습니다. 유효성을 검사하지 않는 이유를 이해하지 못합니다. http://jsfiddle.net/NcHTb/백본에서 유효성 검사가 유효하지 않습니다?

은 코드 :

(function() { 
    window.App = { 
     Models: {}, 
     Collections: {}, 
     Views: {}, 
     Templates: {}, 
     Router: {} 

    }; 


    App.Models.User = Backbone.Model.extend({ 
     defaults: { 
      firstName: 'first', 
      lastName: 'last', 
      email: 'Email', 
      phone: '222', 
      birthday: 'date' 
     }, 

     validate: function (attrs) { 
      if (!attrs.firstName) { 
       return 'You must enter a real first name.'; 
      } 
      if (!attrs.lastName) { 
       return 'You must enter a real last name.'; 
      } 
      if (attrs.email.length < 5) { 
       return 'You must enter a real email.'; 
      } 
      if (attrs.phone.length < 10 && attrs.phone === int) { 
       return 'You must enter a real phone number, if you did please remove the dash and spaces.'; 
      } 
      if (attrs.city.length < 2) { 
       return 'You must enter a real city.'; 
      } 
     } 

    }); 


    var user = new App.Models.User(); 
    //user.on('change', function() { 
    // console.log('something changed'); - used for change anything 
    //}); 
    user.on('invalid', function (model, invalid) { 
     console.log(invalid); 
    }); 
    user.on('change:firstName', function() { 
     console.log('something changed'); 
    }); 

    user.set('firstName', ''); 
    console.log(JSON.stringify(user)); 
    console.log(user.get('birthday')); 






})(); 
+0

뭔가 : 결국
, 당신의 코드는 다음과 것인가? http://stackoverflow.com/questions/15614826/validate-method-inside-backbone-model-not-being-called/15615027#15615027 – nikoshr

답변

5

이미 @nikoshr 응답, 문제는 this question에서 해결되었습니다.

기본적으로 전화 번호는 set()입니다. 전화 번호는 { validate: true }입니다. save() 메서드는 항상 유효성 검사를 트리거합니다. 이 같은

user.set('firstName', '', { validate: true }); // Run validations 
user.set('firstName', '');      // Doesn't run validations 
user.save();         // Run validations 
+1

오, 확인해 보았습니다. 유효성 검사를 한 것 같았습니다. 감사! – Lion789

+0

감사합니다. 튜토리얼에 따라 질문을 게시하려했는데 작성자는 'validate : true'라고 쓰지 않았고 백본이나 크롬에 대한 새로운 업데이트 여야합니까? –

+0

새로운 것은 아니지만, 내 대답은 7 월에 게시되었음을 참조하십시오 :) – gustavohenke

관련 문제