Detect Empty Fields

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Detect Empty Fields

DeHaynes
I am trying to use Velocity to dynamically create the output for my form.  If a field doesn't have any value, then I want the code to not display.  So I came up with the following:

    #if ($doc.getValue('processOverview')!='')
    = PROCESS OVERVIEW =
    <div style="padding-left: 25px;">
      $doc.display('processOverview')
    </div>
    #end

However, this is not working.  Velocity is ALWAYS finding something in the field even though when I go to edit it, there is nothing there.  Am I missing something?

As a work around, I have tried to find a Trim function in velocity and also a Lenth function.  I haven't been able to find either of those.  Anyone have any suggestions?
Reply | Threaded
Open this post in threaded view
|

Re: Detect Empty Fields

jerem
Hello,

You could do:
#if ( "$!doc.getValue('processOverview')" != "" )

You can call methods of a Java String from Velocity (if it's a String of
course):
${propValue.trim()}
${propValue.length()}

BR,
Jeremie


2014-03-24 17:49 GMT+01:00 DeHaynes <[hidden email]>:

> I am trying to use Velocity to dynamically create the output for my form.
>  If
> a field doesn't have any value, then I want the code to not display.  So I
> came up with the following:
>
>     #if ($doc.getValue('processOverview')!='')
>     = PROCESS OVERVIEW =
>     <div style="padding-left: 25px;">
>       $doc.display('processOverview')
>     </div>
>     #end
>
> However, this is not working.  Velocity is ALWAYS finding something in the
> field even though when I go to edit it, there is nothing there.  Am I
> missing something?
>
> As a work around, I have tried to find a Trim function in velocity and also
> a Lenth function.  I haven't been able to find either of those.  Anyone
> have
> any suggestions?
>
>
>
> --
> View this message in context:
> http://xwiki.475771.n2.nabble.com/Detect-Empty-Fields-tp7589773.html
> Sent from the XWiki- Dev mailing list archive at Nabble.com.
> _______________________________________________
> devs mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/devs
>
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs
Reply | Threaded
Open this post in threaded view
|

Re: Detect Empty Fields

DeHaynes
That Length formula doesn't work.

I put the following code in the top of my page.

ProcessOverview Value = "$doc.getValue('processOverview')"
ProcessOverview Length = $doc.getValue('processOverview').length()

I then put a value of "a" in the processOverview textArea and saved it.  The result is below:



I then emptied the processOverview textArea of anything and saved it.  The result is below:



I was wondering if the system is inserting a carriage return or something in the empty field.  So I looked at the HTML code and it only has a <p> node above the empty text node.  
Reply | Threaded
Open this post in threaded view
|

Re: Detect Empty Fields

jerem
The length seems ok : from your screenshot, it seems in second case your
string contains a blank space " " and not an empty string.
So you could first trim() it then compute length() (though I admit I don't
know from were comes the blank space in second case).


2014-03-24 18:15 GMT+01:00 DeHaynes <[hidden email]>:

> That Length formula doesn't work.
>
> I put the following code in the top of my page.
>
> ProcessOverview Value = "$doc.getValue('processOverview')"
> ProcessOverview Length = $doc.getValue('processOverview').length()
>
> I then put a value of "a" in the processOverview textArea and saved it.
>  The
> result is below:
>
> <
> http://xwiki.475771.n2.nabble.com/file/n7589776/ProcessOverview_With_Value.png
> >
>
> I then emptied the processOverview textArea of anything and saved it.  The
> result is below:
>
> <http://xwiki.475771.n2.nabble.com/file/n7589776/LengthStill1.png>
>
> I was wondering if the system is inserting a carriage return or something
> in
> the empty field.  So I looked at the HTML code and it only has a <p> node
> above the empty text node.
>
>
>
> --
> View this message in context:
> http://xwiki.475771.n2.nabble.com/Detect-Empty-Fields-tp7589773p7589776.html
> Sent from the XWiki- Dev mailing list archive at Nabble.com.
> _______________________________________________
> devs mailing list
> [hidden email]
> http://lists.xwiki.org/mailman/listinfo/devs
>
_______________________________________________
devs mailing list
[hidden email]
http://lists.xwiki.org/mailman/listinfo/devs
Reply | Threaded
Open this post in threaded view
|

Re: Detect Empty Fields

DeHaynes
Yea, the following code works.  It just seems overly complex to check if a field is empty.

    #if ($doc.getValue('processOverview').trim().length() != 0)
      = PROCESS OVERVIEW =
      <div style="padding-left: 25px;">
        $doc.display('processOverview')
      </div>
    #end

I think the issue is related to the fact that the field is a textArea and not a string.  I have used simpler checks on string fields and they work just fine.

Thanks for your help  :)