You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The only thing you need to do is use viewName in your fields so you can ref them in your validators that are in the controller. In your controller you have something like that:
validators: [{field: 'name',validator: 'required',message: "Name is mandatory"},{field: 'name',validator: function(field){returnfield.get('value')!="123"},message: "Your name can't be 123"}],save: function(){varform=this.view.getPath("form");if(form.validate()){// do its thing}}
And fields would show errors messages and add css classes to DOM elements and such.
How so?
TC.Form=Ember.View.extend({classNames: "ember-form",formFields: Ember.computed(function(){varchildViews=this.get('_childViews');varret=Ember.A();Ember.ArrayUtils.forEach(childViews,function(view){if(view.isFormField){ret.push(view);}});returnret;}).property().cacheable(),clearErrors: function(){varform_items=this.get('formFields');this.$(".error .help-inline").remove();for(vari=0;i<form_items.length;i++){form_items[i].$().parents(".control-group").removeClass("error");}},validate: function(){varerrors=0;varformFields=this.get('formFields');this.clearErrors();for(vari=0;i<this.validators.length;i++){varvalidator=this.validators.objectAt(i);varfield=this.getPath(validator.field);varfunc=Ember.typeOf(validator.validator)=='string'? TC.Form.defaultValidators[validator.validator] : validator.validator;Ember.assert("Validator for: "+validator.field+", is not a valid function",!func);varmessage=validator.message;varfield_valid=func(field,this,validator);if(!field_valid){this.error(field,message);errors++;}else{this.valid(field)}}returnerrors==0;},valid: function(field){if(field){field.$().parents(".control-group").removeClass("error");field.$().next(".help-inline").remove();}},error: function(field,message){if(field){field.$().parents(".control-group").addClass("error");if(field.$().next(".help-inline").length===0)field.$().parents(".controls").append("<span class=\"help-inline\">"+message+"</span>");}}});TC.Form.reopenClass({defaultValidators: {required: function(field){returnfield.get('value')!='';},number: function(field,form,params){varmax=params.max;varmin=params.min;varvalue=field.get('value');if(parseInt(value)!=value||value<min||value>max)returnfalse;returntrue;}}});
This code would show errors for a Bootstrap based layout. But I've done a version showing all errors on the top of the form. It's pretty flexible and shouldn't be a problem create one that shows errors like Flame.FormView does.
The text was updated successfully, but these errors were encountered:
I'm not using flamejs FormView because I like to have control over the layout and put the fields where I want.
So I tried something today and I would like your opinion on this.
So basically you can create your form just like you're used to in HTML:
The only thing you need to do is use viewName in your fields so you can ref them in your validators that are in the controller. In your controller you have something like that:
And fields would show errors messages and add css classes to DOM elements and such.
How so?
This code would show errors for a Bootstrap based layout. But I've done a version showing all errors on the top of the form. It's pretty flexible and shouldn't be a problem create one that shows errors like Flame.FormView does.
The text was updated successfully, but these errors were encountered: