You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Sebastien Arbogast <se...@gmail.com> on 2005/05/22 08:26:32 UTC

[CForms] Getting selectedValues from a multivaluefield ?

Hi,

How can I get the set of selected values of a multivaluefield widget ?

I tried :
form.getModel().myFieldId => "Cannot convert from..." exception
form.getModel().myFieldId.getValue() => "getValue is not a function" exception

And what will be the type of the retrieved value ? Object[] ? String[]
(as my datatype for this multivaluefield is string) ?

Thx in advance...

-- 
Sebastien ARBOGAST

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [CForms] Getting selectedValues from a multivaluefield ?

Posted by Sebastien Arbogast <se...@gmail.com>.
2005/5/23, Johannes Textor <jc...@gmx.de>:
> 
> >Please help ! I tried to find a solution in samples but the only
> >multivaluefield I found was not used from flowscript. The whole form
> >is just forwarded to some XSP logicsheet as a request parameter.
> >
> >
> >
> Maybe you cannot pass the model.userRoles directly. Keep in mind that
> the flowscript form binding is designed to work from JavaScript, not
> Java. I'm using a loop to feed the values to my Hibernate Object like this:
> 
> *if*(model.rubriken.length>0)
> {
> *       for*( *var* i = 0 ; i < model.rubriken.length; i++ )
>                 artikel.getRubriken().add(hs.load(Rubrik,*new* java.lang.Long(model.rubriken[i])));
> }
> 
> hs is the Hibernate Session which I'm using from flowscript, but never
> mind. The problem is that there is no way I'm aware of to create native
> Java Arrays in flowscript (because JavaScript Arrays are really a different
> type of object !) The only solution I can think of is to refactor your
> createUser method to accept a Vector of objects rather than a String[],
> and then do:
> 
>     model = form.getModel();
> 
>     var roles = new java.util.Vector();
> 
>     for( var i = 0 ; i < model.userRoles.length ; i ++ )
>         roles.add( new java.lang.String( model.userRoles[i] ) );
> 
>     user = schaman.createUser(
>         model.userName,
>         DigestUtils.md5Hex(model.password),
>         model.userActive,
>         model.userEmail,
>         roles
>     );
> 
> Yes I know it's ugly, can somebody come up with a better solution ? Because
> I've had exactly the same problem and could not come up with a better
> solution
> than the one described above. But maybe that is because CForms is designed
> to be smoothly used from JavaScript, and I found the conversion between
> JavaScript
> and Java painful at times.
> 

Thank you very much for your help Johannes. In a way this reassures me
because I'm not alone with that problem. Actually I already have a
addRole method and I'll use it as you suggest.
Otherwise I guess the problem doesn't occur in javaflow but I have
absolutely no idea how to use it so...

-- 
Sebastien ARBOGAST

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [CForms] Getting selectedValues from a multivaluefield ?

Posted by Johannes Textor <jc...@gmx.de>.
>Please help ! I tried to find a solution in samples but the only
>multivaluefield I found was not used from flowscript. The whole form
>is just forwarded to some XSP logicsheet as a request parameter.
>
>  
>
Maybe you cannot pass the model.userRoles directly. Keep in mind that
the flowscript form binding is designed to work from JavaScript, not
Java. I'm using a loop to feed the values to my Hibernate Object like this:

*if*(model.rubriken.length>0)
{
*	for*( *var* i = 0 ; i < model.rubriken.length; i++ )
		artikel.getRubriken().add(hs.load(Rubrik,*new* java.lang.Long(model.rubriken[i])));
}

hs is the Hibernate Session which I'm using from flowscript, but never
mind. The problem is that there is no way I'm aware of to create native
Java Arrays in flowscript (because JavaScript Arrays are really a different
type of object !) The only solution I can think of is to refactor your
createUser method to accept a Vector of objects rather than a String[],
and then do:

    model = form.getModel();
   
    var roles = new java.util.Vector();

    for( var i = 0 ; i < model.userRoles.length ; i ++ ) 
	roles.add( new java.lang.String( model.userRoles[i] ) ); 

    user = schaman.createUser(
        model.userName,
        DigestUtils.md5Hex(model.password),
        model.userActive,
        model.userEmail,
        roles
    );

Yes I know it's ugly, can somebody come up with a better solution ? Because
I've had exactly the same problem and could not come up with a better 
solution
than the one described above. But maybe that is because CForms is designed
to be smoothly used from JavaScript, and I found the conversion between 
JavaScript
and Java painful at times.




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [CForms] Getting selectedValues from a multivaluefield ?

Posted by Sebastien Arbogast <se...@gmail.com>.
2005/5/22, Sebastien Arbogast <se...@gmail.com>:
> I should... and I did but it doesn't work as expected.
> Here is my flowscript method :
> 
> function createUser(){
>     getSchaman();
>     var form = new Form("forms/create_user_d.xml");
>     var user;
>     var model;
> 
>     var roles = schaman.getAllRoles();
>     form.lookupWidget("userRoles").setSelectionList(roles,"name","name");
> 
>     form.showForm("internal/display-user-creation-form");
>     model = form.getModel();
>     user = schaman.createUser(
>         model.userName,
>         DigestUtils.md5Hex(model.password),
>         model.userActive,
>         model.userEmail,
>         model.userRoles
>     );
> 
>     cocoon.sendPage("users-list");
> }
> 
> And the corresponding form definition :
> 
> <fd:form id="data"
> xmlns:fd="http://apache.org/cocoon/forms/1.0#definition"
> xmlns:i18n="http://apache.org/cocoon/i18n/2.1">
>     <fd:widgets>
>         <fd:booleanfield id="userActive">
>             <fd:label>
>                 <i18n:text key="user-active"/>
>             </fd:label>
>         </fd:booleanfield>
>         <fd:field id="userName" required="true">
>             <fd:label>
>                 <i18n:text key="user-name"/>
>             </fd:label>
>             <fd:datatype base="string"/>
>         </fd:field>
>         <fd:field id="userEmail" required="true">
>             <fd:datatype base="string">
>                 <fd:validation>
>                     <fd:email/>
>                 </fd:validation>
>             </fd:datatype>
>             <fd:label>
>                 <i18n:text key="user-email"/>
>             </fd:label>
>         </fd:field>
>         <fd:field id="userPassword" required="true">
>             <fd:label>
>                 <i18n:text key="user-password"/>
>             </fd:label>
>             <fd:datatype base="string"/>
>             <fd:validation>
>                 <fd:length min="5" max="20"/>
>             </fd:validation>
>         </fd:field>
>         <fd:field id="confirmPassword" required="true">
>             <fd:label>
>                 <i18n:text key="confirm-password"/>
>             </fd:label>
>             <fd:datatype base="string"/>
>             <fd:validation>
>                 <fd:assert test="userPassword = confirmPassword">
>                     <fd:failmessage>
>                         <i18n:message key="different-passwords"/>
>                     </fd:failmessage>
>                 </fd:assert>
>             </fd:validation>
>         </fd:field>
>         <fd:multivaluefield id="userRoles">
>             <fd:label><i18n:text key="user-roles"/></fd:label>
>             <fd:datatype base="string"/>
>             <fd:selection-list/>
>         </fd:multivaluefield>
>     </fd:widgets>
> </fd:form>
> 
> But when I do this I get the following exception :
> 
> Cannot convert org.apache.cocoon.forms.flow.javascript.ScriptableWidget@1da77f5
> to java.lang.String[]
> 
> Any idea ?
> 
> --
> Sebastien ARBOGAST
> 

Please help ! I tried to find a solution in samples but the only
multivaluefield I found was not used from flowscript. The whole form
is just forwarded to some XSP logicsheet as a request parameter.

-- 
Sebastien ARBOGAST

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [CForms] Getting selectedValues from a multivaluefield ?

Posted by Sebastien Arbogast <se...@gmail.com>.
I should... and I did but it doesn't work as expected.
Here is my flowscript method :

function createUser(){
    getSchaman();
    var form = new Form("forms/create_user_d.xml");
    var user;
    var model;
    
    var roles = schaman.getAllRoles();
    form.lookupWidget("userRoles").setSelectionList(roles,"name","name");    
    
    form.showForm("internal/display-user-creation-form");
    model = form.getModel();
    user = schaman.createUser(
        model.userName,
        DigestUtils.md5Hex(model.password),
        model.userActive,
        model.userEmail,
        model.userRoles
    );
    
    cocoon.sendPage("users-list");
}

And the corresponding form definition :

<fd:form id="data"
xmlns:fd="http://apache.org/cocoon/forms/1.0#definition"
xmlns:i18n="http://apache.org/cocoon/i18n/2.1">
    <fd:widgets>
        <fd:booleanfield id="userActive">
            <fd:label>
                <i18n:text key="user-active"/>
            </fd:label>
        </fd:booleanfield>
        <fd:field id="userName" required="true">
            <fd:label>
                <i18n:text key="user-name"/>
            </fd:label>
            <fd:datatype base="string"/>
        </fd:field>
        <fd:field id="userEmail" required="true">
            <fd:datatype base="string">
                <fd:validation>
                    <fd:email/>
                </fd:validation>
            </fd:datatype>
            <fd:label>
                <i18n:text key="user-email"/>
            </fd:label>
        </fd:field>
        <fd:field id="userPassword" required="true">
            <fd:label>
                <i18n:text key="user-password"/>
            </fd:label>
            <fd:datatype base="string"/>
            <fd:validation>
                <fd:length min="5" max="20"/>
            </fd:validation>
        </fd:field>
        <fd:field id="confirmPassword" required="true">
            <fd:label>
                <i18n:text key="confirm-password"/>
            </fd:label>
            <fd:datatype base="string"/>
            <fd:validation>
                <fd:assert test="userPassword = confirmPassword">
                    <fd:failmessage>
                        <i18n:message key="different-passwords"/>
                    </fd:failmessage>
                </fd:assert>
            </fd:validation>
        </fd:field>
        <fd:multivaluefield id="userRoles">
            <fd:label><i18n:text key="user-roles"/></fd:label>
            <fd:datatype base="string"/>
            <fd:selection-list/>
        </fd:multivaluefield>
    </fd:widgets>
</fd:form>

But when I do this I get the following exception :

Cannot convert org.apache.cocoon.forms.flow.javascript.ScriptableWidget@1da77f5
to java.lang.String[]

Any idea ?

-- 
Sebastien ARBOGAST

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [CForms] Getting selectedValues from a multivaluefield ?

Posted by Johannes Textor <jc...@gmx.de>.
I tried :

>>form.getModel().myFieldId => "Cannot convert from..." exception
>>form.getModel().myFieldId.getValue() => "getValue is not a function" exception
>>    
>>
>
>The multivalue field has a getValue() function which returns an
>Object[] (see org.apache.cocoon.forms.formmodel.MultiValueFiled)
>

You should also be able to access forms.getModel().myFieldId, which is 
of type
Object[] (actually, JS Array) too.

forms.getModel().myFieldId.length

should  yield a positive value when the user has selected something.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Re: [CForms] Getting selectedValues from a multivaluefield ?

Posted by Jens Maukisch <co...@maukisch.net>.
Hi,

> How can I get the set of selected values of a multivaluefield widget ?

> I tried :
> form.getModel().myFieldId => "Cannot convert from..." exception
> form.getModel().myFieldId.getValue() => "getValue is not a function" exception

The multivalue field has a getValue() function which returns an
Object[] (see org.apache.cocoon.forms.formmodel.MultiValueFiled)



-- 
* best regards
* Jens Maukisch              


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org