Skip to content

Instantly share code, notes, and snippets.

@insin
Created March 1, 2011 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save insin/849904 to your computer and use it in GitHub Desktop.
Save insin/849904 to your computer and use it in GitHub Desktop.
Tonight's coding mission: implement deep copying to kill some rather ugly boilerplate
// From...
var TestForm = forms.formFactory({
fields: function() {
return {
username: new forms.CharField(),
password: new forms.CharField({widget: forms.PasswordInput})
};
},
clean: function() {
if (this.cleanedData.username && this.cleanedData.password &&
(this.cleanedData.username != 'admin' ||
this.cleanedData.password != 'secret')) {
throw new forms.ValidationError('Invalid credentials!');
}
return this.cleanedData;
}
});
// ...to...
var TestForm = forms.formFactory({
username: new forms.CharField(),
password: new forms.CharField({widget: forms.PasswordInput}),
clean: function() {
if (this.cleanedData.username && this.cleanedData.password &&
(this.cleanedData.username != 'admin' ||
this.cleanedData.password != 'secret')) {
throw new forms.ValidationError('Invalid credentials!');
}
return this.cleanedData;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment