我在登录页面上有一个包含用户名和密码文本字段的表单。现在用户注册一个帐户,我想将他添加到用户模型。当他登录时,我需要检查用户名/密码组合是否准确,以允许进入Sencha应用程序。
我不知道如何验证登录/通过组合。有人帮忙!!!这是我的代码。
模特:
App.models.Users = Ext.regModel('Expense',{
fields : [
{ name : 'id', type : 'integer'},
{ name : 'email', type : 'string'},
{ name : 'pass', type : 'string'}
],
validations : [
{ name : 'email', type : 'presence', message : 'cannot leave field blank'},
{ name : 'pass', type : 'presence', message : 'cannot leave field blank'}
],
proxy : {
type : 'localstorage',
id : 'user-creds'
}
});
商店:
App.stores.User = new Ext.data.Store ({
model : 'Users',
autoLoad : true,
autoSave : true
});
表单是基本表单,电子邮件的xtype为“textfield”,密码为“passwordfield”。
如何验证表单中输入的详细信息是否与localstorage ???
中存储的详细信息相匹配答案 0 :(得分:4)
点击按钮进行登录时,您需要尝试查找在商店中输入的电子邮件地址。
App.stores.User.findRecord('email', < the value entered on the form >) ;
如果找到匹配项,此方法将返回记录。获得记录后,您可以检查记录中的密码是否与表单上输入的密码相匹配。
Refer to Store in the Sencha Touch docs
更新
假设您正在关注MVC模式...
你需要:
在视图中,您的登录按钮将分派到控制器:
this.loginButton = new Ext.Button({
text: 'Login',
ui: 'action',
handler: function() {
Ext.dispatch({
controller: App.controllers.loginController,
action: 'login'
});
},
scope: this
});
在您的控制器中,代码检索表单值:
'login': function (options) {
var formDetails = App.views.loginView.getRecord();
var store = Ext.StoreMgr.get('users');
var record = store.findRecord('username', formDetails.get('username'), 0, false, true);
if (record != null) {
if (formDetails.get('password') == record.get('password')) {
// valid login
} else {
// wrong password
}
} else {
// username doesn't exist
}
}
我希望这会有所帮助。