Forums / Setup & design / how to use hidden form fields to specify values of attributes?

how to use hidden form fields to specify values of attributes?

Author Message

nigel dodd

Wednesday 12 January 2005 12:52:20 pm

In a form that uses /content/edit to edit the attributes of an object, I cannot figure out how to use the hidden form fields to specify the values of attributes that are determined programatically rather than by user input.

The attributes to be edited by the user (the first two in the class) are made editable in the template file by

{let my_name=$content_attributes[0]
     my_entry=$content_attributes[1]
     my_extrafield=$content_attributes[2]
}
<form enctype="multipart/form-data" method="post" action={concat("/content/edit/",$object.id,"/",$edit_version)|ezurl}>
{$my_name.contentclass_attribute.name} {attribute_edit_gui attribute=$my_name}
{$my_entry.contentclass_attribute.name} {attribute_edit_gui attribute=$my_entry}

This works. But I also want to specify the value to be given to the third attribute. I have tried

<input type="hidden" name="extrafield" value="the new value for extrafield" />

where extrafield is the name (and identifier) of the third attribute but this does not work. Neither does putting in the id number of the attribute, nor the variable {$my_extrafield} etc., etc.. What needs to go in the various fields of the hidden input type to insert values into the object?

Joanie Chembars

Thursday 13 January 2005 6:13:42 am

I am trying to figure out the same thing - but using a hidden attribute value in a class --
I found this documentation that might do what we need -
http://ez.no/ez_publish/documentation/reference/template_functions/variables/let
I will let you know if I get mine to work. Please post here if you figure yours out.......
Thanks..
By the way, are you working on a class or a form?

nigel dodd

Thursday 13 January 2005 6:47:54 am

Good to hear from you.

The documentation you refer to describes setting the value of a variable. The variable is a template variable and exists in the namespace of the template where it is defined. Unfortunately it does not apply to setting the value of an attribute in a class derived object (instance of an object), so far as I understand.

Still hoping for a solution.

Joanie Chembars

Thursday 13 January 2005 10:49:16 am

Believe it or not - I think I have figured this out.........
start with this post that I stumbled across......
http://ez.no/community/forum/developer/overriding_view_templates_for_datatypes
and it will show you how (AND WHERE) to include overrides for datatype views.
In my override I created a hidden input field taking the value of the current users id number.
I am still chugging on how to make it into a conditional so that this hidden field only shows up when a certain class is called...........
Good Luck.......

nigel dodd

Friday 14 January 2005 4:08:33 am

The "solution" you posed (and which I hope will work for you) is for a different problem: how to conditionally display attribute values.

My problem is different. It is how to insert values into attributes of a newly created object. The normal way to do this is by the use of a form where the user inputs values by means of attribute_edit_gui. It is also possible, I believe, to do this without the user having to input anything but instead by means of a hidden input to the form as suggested in

  <input type="hidden" name="extrafield" value="the new value for extrafield" />

but I cannot determine what to use in the <i>name</i> field.

Please, if anybody has used hidden input fields in a form for this purpose, share your wisdom with me.

Joanie Chembars

Friday 14 January 2005 4:39:56 am

Hi again Nigel --
I didn't give you enough explanation before........
Here's what I did --
The hidden field that I needed was an objectrelation datatype - so I entered that into the class using the regular add attribute interface.
Then, I used that forum comment I linked to earlier to do the datatype override for objectrelation datatype. Here's the code that I put in the override file.......


{let user=fetch('user','current_user')}

   <input type="hidden"
                   name="{$attribute_base}_data_object_relation_id_{$attribute.id}"
                   value="{$user.contentobject_id}" />

{/let}

This set my hidden objectrelation attribute to the value of the current user's id number -- and it works like a charm. For the name, I just copied what I found in the standard dataype template into the name field of hidden value. Make sense so far.......

What I still need to figure out is how to do a conditional loop that gets the class id of the current object and only uses this special hidden item code for certain classes.

Also, because of some coding somewhere, I couldn't get my object name to stop appearing in the input layout - so I just deleted the text from the name field of the attribute and put in some dashes so it won't confuse my users.

This workaround keeps me from having to create a custom datatype - which I have no desire to tackle. A hidden value datatype would be nice to have wouldn't it??

nigel dodd

Friday 14 January 2005 5:59:10 am

Pursuing your idea I have put the following code in the form template

<input type="hidden" name="{$attribute_base}_data_object_relation_id_{$content_attributes[2].id}" value="the new value for extrafield" />

where $content_attributes[2] is the third (they start from zero) attribute of the object i.e the one I wish to set programatically. The other two are exposed for the user to edit using attribute_edit_gui.

Still there is no change of the third attribute value from the default value given in object construction.

If I expand the <i>name</i> parameter I get
ContentObjectAttribute_data_object_relation_id_569

So evidently I am not getting the <i>name</i> parameter right here, or the whole notion that I can set object attribute values using a hidden input field is wrong.

Joanie Chembars

Friday 14 January 2005 9:33:00 am

A couple of things (and with disclaimer -- I am NOT a programmer, this is just how I got it to work)....
I actually modified the code in the template for the display of the attribute type, like it said in the linked forum page I listed above.
And, the name code I have in my example is what I copied from the standard template for the objectrealtion datatype.
Did you create the datatype that you needed in the class first?
Just trying to help.......

nigel dodd

Friday 14 January 2005 2:26:12 pm

I appreciate your efforts to help.

It is a simple requirement to programatically set an attribute of an object to a value rather than have the user type it in. It has taken several days so far and I don't feel I am any nearer. The only documentation seems to be the php code itself.

To answer your question, the attribute I want to set is a simple text field not an objectrelation datatype. What were you using for the <i>action</i> part of your form?

I shall be away for a week or so.

nigel dodd

Wednesday 26 January 2005 3:26:09 am

Final solution:

{let my_name=$content_attributes[0]
     my_entry=$content_attributes[1]
     my_extrafield=$content_attributes[2]
}
<form enctype="multipart/form-data"
      method="post"
      action={concat("/content/edit/",
                     $object.id,
                     "/",
                     $edit_version)|ezurl}>
{* Allow the user to edit name and entry *}
{$my_name.contentclass_attribute.name} {attribute_edit_gui attribute=$my_name}
{$my_entry.contentclass_attribute.name} {attribute_edit_gui attribute=$my_entry}
{* Hide the third attribute and insert some value into it *)
<input type="hidden" 
       name="{$attribute_base}_ezstring_data_text_{$my_extrafield.id}"
       value="the new value for extrafield" 
/>
{* Now do the normal stuff to create the buttons *}
<table class="layout">
    <tr><td>
        <div class="buttonblock">
            <input class="defaultbutton"
                   type="submit"
                   name="PublishButton"
                   value="Submit" />

            <input class="button"
                   type="submit" 
                   name="DiscardButton" 
                   value="Cancel" />

            <input type="hidden" 
                   name="MainNodeID" 
                   value="{$main_node_id}" />
        </div>
    </td></tr>
</table>
</form>
{/let}

My hidden attribute was a Text Line which seems to be also called an ezstring. The trick was to look in design\standard\templates\content\datatype\edit\ezstring.tpl to get the magic incantation:

name="{$attribute_base}_ezstring_data_text_{$content_attributes[2].id}"

The middle bit is different for different data types and you need to look in the relevant file for the exact wording for different data types. Incidentally this compound string is picked up by ezstringtype.php (or the equivalent for other data types) for actual processing.

So that is the way to insert your own programatically determined values into attributes of newly created objects. The solution that has eluded me for quite some time. Thanks to Joanie for her contributions.