Hello Dennis,

The second did work after all. Appears I have to restart the CMS to get changes to be effective.

Your hint regarding the first try also lead me to the solution.
The following code snippet works:

>>     <rule for="/document/content/paragraphs/paragraph/image/@alt">
>>       <validation>
>>       <validator name="javascript">
>>         <![CDATA[
>>           var srcWidget = lookupWidget(widget,"../@src");

Thanks for your help,

Reinier


Dennis Dam wrote:
Reinier van den Born wrote:

Hello,

I have the following problem:

In XML I have an element "image" with two attributes @src and @alt.
If @src is filled in the CMS form, then @alt becomes a required field.

Looking at http://www.hippocms.org/display/CMS/Template+validation
I would say that this can be implemented with a bit of JavaScript
(flowscript). Section "More about javascript (flowscript) validation"
suggests there is a function lookupWidget( widget, xpath ) that
according to the description:

"Returns a widget object, relative to the widget passed as first
argument, using the relative/absolute xpath to that widget."

So I expected the following piece of code to work:

    <rule for="/document/content/paragraphs/paragraph/image">
      <validation>
      <validator name="javascript">
        <![CDATA[
          var srcWidget = lookupWidget(widget,"@src");
          var altWidget = lookupWidget(widget,"@alt");
          if ((srcWidget != null)){
            if (srcWidget.value != null && (widget.value == null ||
widget.value.length() <= 0)){
              setValidationError(widget,"Alt is required field.");
              return false;
            }
          }
clearValidationError(widget); // clear previous validation errors
          return true;
        ]]>
      </validator>
      </validation>
    </rule>

It doesn't. Tried all sorts of variations: "./@xxx" paths, rule on @alt
with ../@src path, etc.

I think doesn't work yes. Validation handlers only work on *widgets*, which means any field that is visible as an input field in the editor. The element "image" does not have any content field related to it. So the validation handler is not activated. This should documented on the wiki page you read. I will do that.

In the end I tried something that doesn't use
lookupWidget at all:

    <rule for="/document/content/paragraphs/paragraph/image/@alt">
      <validation>
      <validator name="javascript">
        <![CDATA[
            if ((widget.value == null || widget.value.length() <= 0)){
              setValidationError(widget,"Alt is required field.");
              return false;
          }
clearValidationError(widget); // clear previous validation errors
          return true;
        ]]>
      </validator>
      </validation>
    </rule>

This doesn't work either.


hmm this should work. Can you try to print something in the handler to see whether the validation handler is executed at all? E.g. :

print(widget);



Can anyone explain what I am doing wrong or where I can find more
information to solve this?

Thanks,

Reinier

********************************************
Hippocms-dev: Hippo CMS development public mailinglist


--

Reinier van den Born

HintTech B.V.
Rotterdamseweg 183c, 2629 HD Delft
T: +31(0)15 268 2573
F: +31(0)15 268 2567
M: +31(0)6 494 171 36
--
HintTech levert specialisten op het gebied van softwareontwikkeling (.NET en Java),
projectmanagement, informatiebeveiliging en business consulting.
KvK Den Haag nr. 27242282 | BTW nr. NL8062.16.396.B01
begin:vcard
fn:Reinier van den Born
n:van den Born;Reinier
org:HintTech B.V.
adr:;;Rotterdamseweg 183c;Delft;;2629HD;Netherlands
email;internet:[EMAIL PROTECTED]
tel;work:+31-15-268 2573
tel;fax:+31-15-268 2567
tel;cell:+31-6 494 171 36
note;quoted-printable:KvK Den Haag nr. 27242282 | BTW nr. NL8062.16.396.B01=0D=0A=
	=0D=0A=
	HintTech levert specialisten op het gebied van softwareontwikkeling (.NET=
	en Java), projectmanagement, informatiebeveiliging en business consulting=
	.=0D=0A=
	=0D=0A=
	
url:www.hinttech.com
version:2.1
end:vcard

********************************************
Hippocms-dev: Hippo CMS development public mailinglist

Reply via email to