Permalink
Browse files

Running client-side validations even if send_form_function is not def…

…ined
  • Loading branch information...
adamw committed Apr 18, 2015
1 parent c95bc7b commit 7032358dad71e42f137a9fe37fa5512945ed2876
@@ -45,7 +45,7 @@ object PersonForm {
val personForm = form[Person](f => List(
f.field(_.firstName).label("label_person_firstname") || f.field(_.lastName).label("label_person_lastname")
.validate(custom((v, e) => v.length > e.firstName.length, (v, e) => Message("error_custom_lastNameLongerThanFirstName"))),
f.field(_.age).label("Age") || f.field(_.birthday).label("Birthday").description("Please tell us, when where you born"),
f.field(_.age).label("Age").validate(ge(0), lt(160)) || f.field(_.birthday).label("Birthday").description("Please tell us, when where you born"),
f.field(_.likesBroccoli).label("Likes broccoli"),
f.field(_.address1).label("Address 1"),
f.field(_.address2).label("Address 2"),
@@ -14,24 +14,27 @@ module Supler {
}
attachRefreshListeners() {
this.ifEnabledForEachFormElement(htmlFormElement => {
this.forEachFormElement(htmlFormElement => {
if (htmlFormElement.nodeName != "FIELDSET") {
htmlFormElement.onchange = () => this.refreshListenerFor(htmlFormElement)
}
});
}
attachActionListeners() {
this.ifEnabledForEachFormElement(htmlFormElement => {
this.forEachFormElement(htmlFormElement => {
if (htmlFormElement.getAttribute(SuplerAttributes.FIELD_TYPE) === FieldTypes.ACTION) {
htmlFormElement.onclick = () => this.actionListenerFor(htmlFormElement)
}
});
}
private refreshListenerFor(htmlFormElement:HTMLElement) {
// always running validations
var validationResult = this.validation.processClientSingle(htmlFormElement.id);
// if an action is in progress, dropping the send
if (!this.actionInProgress && !this.validation.processClientSingle(htmlFormElement.id)) {
if (!this.actionInProgress && this.options.sendEnabled() && !validationResult) {
this.refreshCounter += 1;
var thisRefreshNumber = this.refreshCounter;
@@ -54,7 +57,7 @@ module Supler {
private actionListenerFor(htmlFormElement:HTMLElement) {
// allowing at most one action at a time.
if (!this.actionInProgress) {
if (!this.actionInProgress && this.options.sendEnabled()) {
this.actionInProgress = true;
var id = htmlFormElement.id;
@@ -80,15 +83,13 @@ module Supler {
this.actionInProgress = false;
}
private ifEnabledForEachFormElement(body:(htmlFormElement:HTMLElement) => void) {
if (this.options.sendEnabled()) {
this.formElementDictionary.foreach((elementId:string, formElement:FormElement) => {
var htmlFormElement = document.getElementById(elementId);
if (htmlFormElement) {
body(htmlFormElement);
}
});
}
private forEachFormElement(body:(htmlFormElement:HTMLElement) => void) {
this.formElementDictionary.foreach((elementId:string, formElement:FormElement) => {
var htmlFormElement = document.getElementById(elementId);
if (htmlFormElement) {
body(htmlFormElement);
}
});
}
private sendSuccessFn(applyResultsCondition:() => boolean, onComplete:() => void) {
@@ -226,5 +226,19 @@ describe('form validation', function() {
validationElementByName('f2').innerText.should.not.have.length(0);
validationElementByName('f3').innerText.should.not.have.length(0);
validationElementByName('f4').innerText.should.not.have.length(0);
})
});
it('should run client-side validations after a field is changed, even if send_form_function is not defined', function() {
// given
var sf = new Supler.Form(container);
sf.render(data.simple1.form1);
// when
byName('field1').val('');
byName('field1').change();
// then
var validationElement1 = validationElementByName('field1');
validationElement1.innerText.should.have.length.above(0);
});
});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Large diffs are not rendered by default.

Oops, something went wrong.

0 comments on commit 7032358

Please sign in to comment.