PDA

View Full Version : [OPEN] emptyText is submitted with the form



quilleashm
21 Jun 2007, 2:21 AM
Hi all,

If I set emptyText on a text field, then if the form is submitted with the field 'empty' (but showing the emptyText) then the value of emptyText is submitted. I think this value should be display only and never submitted. Typically the value is some sort of prompt which isn't usually sensible to submit.

Cheers.

Mike.

violinista
21 Jun 2007, 6:52 AM
I noticed the same issue today. Is it bug or feature?

Regards,
Violinista

mdissel
21 Jun 2007, 11:13 AM
This is fixed in the SVN release

Field.js

/**
* Returns the raw data value which may or may not be a valid, defined value. To return a normalized value see {@link #getValue}.
* @return {Mixed} value The field value
*/
getRawValue : function(){
var v = this.el.getValue();
if(v === this.emptyText){
v = '';
}
return v;
},

/**
* Returns the normalized data value (undefined or emptyText will be returned as ''). To return the raw value see {@link #getRawValue}.
* @return {Mixed} value The field value
*/
getValue : function(){
var v = this.el.getValue();
if(v == this.emptyText || v === undefined){
v = '';
}
return v;
},


Thanks

Marco

prophet
2 Jul 2007, 3:15 PM
It looks like this problem still exists in Ext 1.1 Beta 2 (as of today, 7/2/2007).
Any hot fix suggestions?

trbs
2 Jul 2007, 4:27 PM
okey i wrote a small workaround for this.
i'll have to say that i think this patch is less then ideal and i do not recommend patching ext in this way... but if you really want this problem solved try this:

add the following lines of code to Action.js line 108:


if (o.clientValidation === false || this.form.isValid()){
this.form.items.each(function(f) {
if (f.el.getValue() == f.emptyText) {
f.el.dom.value = '';
}
});
Ext.lib.Ajax.formRequest(


The first and last line of the code block are the existing lines. You'll need to add the this.form.items.each block between them.

Explanation
The form.submit() action uses a form serializer instead of the getValue() field functions the raw dom value is send to the server. (this is the value including emptyText if the field is empty)

So the fix posted above does work, only not for a form.submit() action. Since i do not want to start editing in files other then those of ext themselfs. I build the above work around.

Just before the XHR submit-request to the server, this deletes the emptyText value from the dom. Then when the Ext.lib.Ajax.formRequest serializes the form there is no emptyText in there anymore.

The next time the form validates itself, the emptyText will reappear.

Related problems
You will also hit this problem if you use: form.getValues() as that method also uses the serializer.
This patch will not work for that, but i could use a similar workaround. (again remember, this is not very good
way to work around this... hopefully the ext team will come up with something better, like using getValue() methods or serializers that check for emptyText)

Attachment
The patched file attached here is based on the Ext 1.0.1 release.
Please apply the workaround yourself or do a diff; if you want to apply it on any other version.

Post Scriptum
If your client types the same value as emptyText in your Field the workaround will not do the desired thing. It will consider the typed value the same as emptyText, which it is clearly not. For 9 out of 10 people this will not be a problem, but if it is you should also check the css (aka the light grey text, instead of black typed text)

prophet
5 Jul 2007, 12:40 PM
trbs, you're a pro! thanks a lot!

cgi-bin
6 Jul 2007, 9:39 PM
There has got to be a more elegant way to handle this. Whats the point of having emptyText if it cant gracefully handle the case when the user may have not updated that field. trbs fix sorta works, it blanks out the field. If the submit doesn't work and returns a server side error that requires the form to remain for more changes, the field is not empty, but the emptyText is no longer available. Heck I guess something similar to ComboBox's hiddenName might work (don't get me started on the problems with ComboBox...)

I'm guessing most people use this feature for context hints for fields, or what is the intended purpose of emptyText? A default value? Maybe I'm just using it wrong.

Sorry for the semi-rant, 6 hours sleep in the last 3 days will do that to ya. (:|

prophet
7 Jul 2007, 12:40 PM
cgi-bin,

I understand your frustration.
I think that the nomenclature for Ext.form.Field's are a bit misleading...

I was confused with the differences between blankText/emptyText at first, and the bugs with emptyText did not help me much either.
However, I think trbs's fix works well though, and I'm sure we'll see a fix and (hopefully) a rename in the next version.

It may already be fixed in a SVN release. I'm not sure.

jack.slocum
7 Jul 2007, 1:43 PM
It is not a "bug" with emptyText. You have configured the field to have text in it when it goes empty, instead of being empty. So, when it is submittedto the server, it has the value you asked for it to have. How is this a bug?

Clearing out that value before submit is definitely a desirable option, but it isn't currently implemented or supported. trbs posted a nice workaround. I tried it and it seems to work well.

jay@moduscreate.com
8 Jul 2007, 11:12 AM
It is not a "bug" with emptyText. You have configured the field to have text in it when it goes empty, instead of being empty. So, when it is submittedto the server, it has the value you asked for it to have. How is this a bug?

Clearing out that value before submit is definitely a desirable option, but it isn't currently implemented or supported. trbs posted a nice workaround. I tried it and it seems to work well.

Jack,

Empty text is assumed to be used as a helper for customers. I realize what you're saying, but I also assumed it as well. I've since stopped using empty text where fields are not required to be filled in.


Eh, i can see both sides of the fence being right. Perhaps an option to not send the empty text value?

cgi-bin
8 Jul 2007, 12:46 PM
Maybe rename emptyText to defaultValue since thats what it seems its intended use is and would be less confusing. Then also add helpText parameter which would never be submitted?

kilrae
8 Jul 2007, 7:38 PM
Jack,

Empty text is assumed to be used as a helper for customers. I realize what you're saying, but I also assumed it as well. I've since stopped using empty text where fields are not required to be filled in.


Eh, i can see both sides of the fence being right. Perhaps an option to not send the empty text value?

Especially as the emptyText is displayed differently (greyed) and disappears when you click on the field unlike regular text.

sebsei
4 Oct 2007, 4:39 AM
I had the same problem as other users in this thread, and find it a little unlogical that the getValue() function of a field returns '' if the field is empty and emptyText is set, but when you submit the form with form.submit() the emptyText values are submitted.

I'm using the following solution, which I think is a little cleaner, as you don't have to put it directly in the Ext source, but can include from somewhere else. Another thing is that if errors are returned from server side, the emptyText values will be put back on the fields.



Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createInterceptor(function() {
this.form.items.each(function(item) {
if (item.el.getValue() == item.emptyText) {
item.el.dom.value = '';
}
});
});
Ext.form.Action.Submit.prototype.run = Ext.form.Action.Submit.prototype.run.createSequence(function() {
this.form.items.each(function(item) {
if (item.el.getValue() == '' && item.emptyText) {
item.el.dom.value = item.emptyText;
}
});
});

feishu
25 Feb 2008, 12:46 AM
THX, That's good:D

oxyum
16 Aug 2008, 9:10 AM
solution (http://extjs.com/forum/showthread.php?t=44295)

broady
20 Aug 2009, 10:17 PM
My solution:


Ext.onReady(function() {
Ext.select("form").on('submit', function() {
Ext.each(Ext.ComponentMgr.all.items, function() {
if (typeof this.getValue == "undefined" || typeof this.el == "undefined") return;

if (this.getRawValue() != this.el.getValue())
this.setRawValue("");
});
});
});

SabaSarwat
15 Dec 2010, 6:29 AM
Broady, this solution did not work for me.
and sebsei where to add lines if i don't have any html file, i have a .jsp

hitekshu
12 Jul 2011, 12:11 AM
I am using Extjs 3.4 and there is an error "f.el is undefined" which is thrown if the element is a radio button. Any fix for this one?



okey i wrote a small workaround for this.
i'll have to say that i think this patch is less then ideal and i do not recommend patching ext in this way... but if you really want this problem solved try this:

add the following lines of code to Action.js line 108:


if (o.clientValidation === false || this.form.isValid()){
this.form.items.each(function(f) {
if (f.el.getValue() == f.emptyText) {
f.el.dom.value = '';
}
});
Ext.lib.Ajax.formRequest(


The first and last line of the code block are the existing lines. You'll need to add the this.form.items.each block between them.

Explanation
The form.submit() action uses a form serializer instead of the getValue() field functions the raw dom value is send to the server. (this is the value including emptyText if the field is empty)

So the fix posted above does work, only not for a form.submit() action. Since i do not want to start editing in files other then those of ext themselfs. I build the above work around.

Just before the XHR submit-request to the server, this deletes the emptyText value from the dom. Then when the Ext.lib.Ajax.formRequest serializes the form there is no emptyText in there anymore.

The next time the form validates itself, the emptyText will reappear.

Related problems
You will also hit this problem if you use: form.getValues() as that method also uses the serializer.
This patch will not work for that, but i could use a similar workaround. (again remember, this is not very good
way to work around this... hopefully the ext team will come up with something better, like using getValue() methods or serializers that check for emptyText)

Attachment
The patched file attached here is based on the Ext 1.0.1 release.
Please apply the workaround yourself or do a diff; if you want to apply it on any other version.

Post Scriptum
If your client types the same value as emptyText in your Field the workaround will not do the desired thing. It will consider the typed value the same as emptyText, which it is clearly not. For 9 out of 10 people this will not be a problem, but if it is you should also check the css (aka the light grey text, instead of black typed text)

SabaSarwat
12 Jul 2011, 5:47 AM
see this
http://www.sencha.com/forum/showthread.php?119221-Ext-version-3.3.1-with-submitEmptyText-false-not-working&p=552959