Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Cannot check if native.newTextField is empty or not
Started by info452 Feb 20 2014 11:43 PM

- - - - -
18 replies to this topic

Best Answer primoz.cerar , 21 February 2014 - 05:45 AM

Ah yes the infamous reverse if bug :).

You're welcome.

[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

So, i'm trying to validate a form, in teory when I hit the validar button, the widget calls checkcampos function, but always return is filled, even when it empty. If i change the if condition to == it return empty when is filled.
 
¿There is any other way to compare if nativetextfield is or not empty?
 
Thanks
 
Here's the code:
 
 
 
  1. local nombreinput = native.newTextField( centerX-131, _H-40, 250, 24 )
  2. nombreinput.align = "center"
  3. nombreinput.size = 18
  4. nombreinput:setTextColor( 1, 0.5, 0 )
  5.  
  6.  
  7. function checkcampos()
  8. if nombreinput ~= "" then
  9.                 --print("Ready for scene change")
  10.                 --storyboard.gotoScene( "scene1", "slideUp", 800  )
  11.                 native.showAlert("test", "the nombreinput field is filled.")
  12.         else
  13.                 native.showAlert("test", "the nombreinput field is empty.")
  14.         end
  15. end    
  16.  
  17. local validar = widget.newButton
  18. {       width = 95,
  19.     height = 65,
  20.         y = _H + 137,
  21.         x = centerX+205,  
  22.         id = "button6",
  23.         labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0, 0.5 } },
  24.     label = "Validar Respuesta", "Sansation",
  25.         onPress = checkcampos,
  26.         fontSize = 12,
  27.         textOnly=true
  28. }
  29.  group:insert( validar )


[TOPIC: post.html]
#2

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

if nombreinput.text ~= "" then



[TOPIC: post.html]
#3

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

Your line

if nombreinput ~= "" then

 

correct line

if nombreinput.text ~= "" then



[TOPIC: post.html]
#4

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

Your line

if nombreinput ~= "" then

 

correct line

if nombreinput.text ~= "" then

 

Nope :( I already try that too, even creating a local var and asign the variable to the nombreinput.text but the same. Even debugging with adb it detect only the empty, in both cases.

 

I read other post and in a reply from corona staff, they said the workaround of the local var but i test that too and is wrong.

 

I'm working on Android btw.



[TOPIC: post.html]
#5

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

Well I use text objects all the time on Android and I have not seen this issue before. The fact is that in your code nombreinput is a text field object so you can not check it as a string. The text field object has a property text that holds the current text. Before you check the text property print it out so you can see in the terminal windows what is returning.

print(nombreinput.text)



[TOPIC: post.html]
#6

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

Well I use text objects all the time on Android and I have not seen this issue before. The fact is that in your code nombreinput is a text field object so you can not check it as a string. The text field object has a property text that holds the current text. Before you check the text property print it out so you can see in the terminal windows what is returning.

print(nombreinput.text)

 

Do you have some working example? because after an update on my code still not working:

 

-- Create our Text Field
local nombreinput = native.newTextField(centerX-131, _H-40, 250, 24 )
nombreinput.align = "center"
nombreinput.size = 18
nombreinput:setTextColor( 1, 0.5, 0 )
nombreinput.text = "Introducir Nombre..."
nombreinput.inputType = "default"
 
function checkcampos()
if nombreinput.text ~= "" then
		print ("the nombreinput field is ", nombreinput.text)
		
	else
                print ("the nombreinput field is ", nombreinput.text)
	end
end	


[TOPIC: post.html]
#7

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

What does it print in the terminal window?

Also try adding an input event listener.

 

function inputEvent(event)

  print(event.text)

  print(event.target.text)

end

 

nombreinput:addEventListener("userInput", inputEvent)



[TOPIC: post.html]
#8

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

What does it print in the terminal window?

 

The terminal window don't get any value because in the newtextfield is not accesible on the simulator (stay gray):

 

the nombreinput field is
the nombreinput field is
 
And when i write something (fffffrff) on the field (on the phone) the adb log says:
 
I/Corona  (25944): the nombreinput field is     fffffrff
 
I/Corona  (25944): the nombreinput field is     fffffrff
 
I/Corona  (25944): the nombreinput field is     fffffrff
 
I/Corona  (25944): the nombreinput field is     fffffrff
 
But my idea is that when a user leave the field empty a popup shows saying (you cannot send because field X is empty) and to do that i have to get the current value of the field, the problem is that there's no difference once you are on android (in this case), the newtextfield always return empty.
 


[TOPIC: post.html]
#9

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

Ah right you are on windows. Well that would be the reason why it doesn't work. Run it on device and check in logcat what gets printed (filter: corona).



[TOPIC: post.html]
#10

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

Ah right you are on windows. Well that would be the reason why it doesn't work. Run it on device and check in logcat what gets printed (filter: corona).

 

Validate button without text on nombreinput:

 

I/Corona  (25944): the nombreinput field is     
 
I/Corona  (25944): the nombreinput field is     
 
I/Corona  (25944): the nombreinput field is     
 
I/Corona  (25944): the nombreinput field is     
 
I/Corona  (25944): the nombreinput field is     
 
I/Corona  (25944): the nombreinput field is     
 
 
Validate button with text ( lllll ) on nombreinput:
 
I/Corona  (25944): the nombreinput field is     lllll
 
I/Corona  (25944): the nombreinput field is     lllll
 
I/Corona  (25944): the nombreinput field is     lllll
 
I/Corona  (25944): the nombreinput field is     lllll
 
Till then all is ok (in theory) but know... wait emmm I see what you trying to tellme with the (The text field object has a property text that holds the current text.) i have to use another if inside right?


[TOPIC: post.html]
#11

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

I'm not sure what you are trying to do. textField.text is the text that is currently displayed in it. Is that not what you want?



[TOPIC: post.html]
#12

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

I'm not sure what you are trying to do. textField.text is the text that is currently displayed in it. Is that not what you want?

 

It's very ease, im trying to show a native message (hey! the nombrefield is empty!),  when the user leave the nombreinput field empty. When is not, shows another native message (hey! you have completed the nombrefield!)   The problem is that when the field is empty the app shows "hey! you have completed the nombrefield!" and there is no characters inside. Is like he have a blank space but is not.

 

:(



[TOPIC: post.html]
#13

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

Well your logcat output looks right so I don't know what the issue is. Are you saying that when you have this in logcat:

I/Corona  (25944): the nombreinput field is

 

Your app still says that it's not empty?

 

Try outputting this to see if there are spaces in the text box:

print ("the nombreinput field is '"..nombreinput.text.."'")

 

now if it's empty you should get

the nombreinput field is ''

if it's not then it will be

the nombreinput field is ' '



[TOPIC: post.html]
#14

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

Well your logcat output looks right so I don't know what the issue is. Are you saying that when you have this in logcat:

I/Corona  (25944): the nombreinput field is

 

Your app still says that it's not empty?

 

Try outputting this to see if there are spaces in the text box:

print ("the nombreinput field is '"..nombreinput.text.."'")

 

now if it's empty you should get

the nombreinput field is ''

if it's not then it will be

the nombreinput field is ' '

Yes! that's exactly my problem! 

 

Gonna try what you say.



[TOPIC: post.html]
#15

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

actualy try this and post adb log results:

 

function checkcampos()
print ("the nombreinput field is '"..nombreinput.text.."'")
if nombreinput.text ~= "" then
		print("not empty")
	else
		print("empty")
	end
end	


[TOPIC: post.html]
#16

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

actualy try this and post adb log results:

 

function checkcampos()
print ("the nombreinput field is '"..nombreinput.text.."'")
if nombreinput.text ~= "" then
		print("not empty")
	else
		print("empty")
	end
end	

 

adb log

 

I/Corona  (12251): the nombreinput field is ''
 
I/Corona  (12251): empty
 
I/Corona  (12251): the nombreinput field is ''
 
I/Corona  (12251): empty
 
I/Corona  (12251): the nombreinput field is ''
 
I/Corona  (12251): empty
 
after insert some text:
 
I/Corona  (12251): the nombreinput field is 'llllll'
 
I/Corona  (12251): not empty
 
I/Corona  (12251): the nombreinput field is 'llllll'
 
I/Corona  (12251): not empty
 
I/Corona  (12251): the nombreinput field is 'llllll'
 
I/Corona  (12251): not empty


[TOPIC: post.html]
#17

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

I don't see the problem. It's working as it should. Now where the print("empty") is put your warning.



[TOPIC: post.html]
#18

info452

[GLOBAL: userInfoPane.html]
info452
  • Observer

  • 28 posts
  • Corona SDK

I don't see the problem. It's working as it should. Now where the print("empty") is put your warning.

 

oh boy... i was putting the message in the wrong place, when was not empty i put empty and vice versa :P 

 

Thanks for all

 

Kind regards.



[TOPIC: post.html]
#19

primoz.cerar

[GLOBAL: userInfoPane.html]
primoz.cerar
  • Contributor

  • 455 posts
  • Corona SDK

  Best Answer

Ah yes the infamous reverse if bug :).

You're welcome.




[topic_controls]
[/topic_controls]