Zope form : adding a button to automatically fill some fields - forms

under Plone4/Zope3, I have a form to add a new object.
I'd like to add a button after the first field, to do the following :
- the user enters the value for the first field
- he presses this new button
- and the server will try to guess the remaining field based on the first field and an external database.
- the user will then have to check if all fields are OK and then submit the form.
I am not a zope expert, and spent some time trying to figure out how to do this.
Is creating a subform a good idea ?
If not, I could add a new button to the form : I tried something like
#button.buttonAndHandler(_(u'Essai'))
def essai(self, action):
print "button essai"
but then I have the following issues :
- how to render the button after the first field and not at the bottom ?
- how to update the remaining fields without submitting the form ?
- how to keep the "add" and "cancel" button that disappeared when I added this essai button.
any hints ?
Thx

I suggest to use javascript to solve this. With jquery you can include an extra button directly behind your first field (http://api.jquery.com/after). In the button action ask a browser view for the search results in json (http://docs.python.org/library/json.html) an fill up the other form fields, like:
jq('my-button').click(function(event) {
event.preventDefault();
var first_field= jq('#first-field-id').val();
jq.getJSON('/##my-browser-view?value=' + value, function(result) {
jq('#other-field-id').val(result.val_for_other_field);
...
});
});

Related

How to have a button change the form within the subform "window" in Ms Access

I need to create my own "navigation" form in Ms Access. Basically, I have a MAIN form that has one field named LotNumSelect
Then, buttons representing each form the user would like to view.
Button1 - Customer
Button2 - Job Data
etc.
Once the customer clicks a button, the procedure would open the corresponding form in the subform window to the record matching the LotNumSelect field.
So far, I have this.
Private Sub ContactInfo_Click()
Main_Customer_Sbfm.SourceObject = "Main_Customer_Sbfm"
Forms!Form1!Main_Customer_Sbfm.Form!Lot_Number.SetFocus
This nicely switches the form within the subform. the problem is how do I get the form within the subform find the record associated with the data in the LotNumSelect Field?
I tried this..
DoCmd.FindRecord LotNumSelect.Value, acEntire, , acSearchAll
but it's giving me a runtime error of "2162"
"A macro set to one of the current field's properties failed because of an error in a FindRecord action argument.

How to know if jQuery validator already exists for a form?

Imagine I have an unordered list with 2 cols, col 1 is the name of a file and col 2 is a pencil icon which has tooltip enabled. When the user clicks on this pencil icon, it opens a tooltip which contains a form. In this form the user will enter a new file and the value will be validated.
When the tooltip is shown, I attach jQuery validator to the form inside the tooltip. The problem is that each time the tooltip is shown, a new validator is attached. I would like to only attach one validator to a form. How can I successfully check if validator already exists for a form?
var validator = forms.validate();
console.log(validator);
// always evaluate to true
if (validator) {
}
This doesn't seem to work. Does anyone have any ideas?
You can use the jQuery data to check if the validator exists on a form element like this:
var form = $('#myform');
if(form.data('validator')){
// validator exists
}
I realized that I could just check to see if a form has the class "novalidate" to determine if the form has a validator attached or not.
var forms = jQuery("form.edit");
if (forms.attr("novalidate")) {
return;
}

In-line item editing in Lift / handling 2 different form submit needs on one page

OK, so first off, let's start with me acknowledging that the bind( ... ) way of binding Lift forms is so last week! :) I do know that, and I just haven't gone back to update this code yet. Also, I trust now that there's some really slick Lifty way to do this. That's what I seek. I'm stumped as to even how to hack something together. That said...
I have a list of Items that I initially display non-editable, and the title of each Item is an ajax-enabled link that calls to the server and replaces that line-item with an editable form of the Item (I use SetHtml to swap the form in at the < li> that listed that Item).
"parent" Items List view looks something like this
< form data-lift="form.ajax">
< div data-lift="creategamewizard?multipart=true" id="wizardform">
< ul>
< li>Item 1< /li>
< li>Item 2< /li>
< /ul>
some more form elements
< button>Submit< /button>
< input type="hidden" id='298356928734' />
< /div>
< form>
This ajax submit (via the hidden field) calls processSubmit().
The SetHtml that swaps in the editableItem form looks something like this.
NOTE: At the end of the following listing, the "save" binding has no server-side code tied to it because the "parent" submit button is already on the page, and when I put another hidden field in this binding or tried to tie any code directly to the Edit Item Save button, that code and the "parent" submit got triggered. So the approach below was to try to use the "parent" submit for both the parent submit as well as the Edit Item submit.
<a href="javascript://" onclick={ajaxOnClickHandler(editItemClickHandler(item.id.get))}>{item.title.get}</a>
def ajaxOnClickHandler(jsHandler: ()=>JsCmd) =
{
SHtml.onEvent( e => jsHandler()).toJsCmd+";return false;"
}
def editItemClickHandler(itemId: String): ()=>JsCmd = ()=>
{
trace.logAjaxComm("ExistingItem.Edit()")
JsCmds.SetHtml("LiId"+itemId, getEditableItem(promo) )
}
def getEditableItem(itemId) =
{
bind( ...
"promotitle" -> SHtml.text(editablePromo.get.promotitle.is,
(s:String) => {
trace.logUIDataManipulation("Saving new promo Title["+s+"]");
editablePromo.get.promotitle(s)
}, "id" -> "promotitle"),
"save" -> SHtml.button("Save", ()=> {})
)
}
Then when the user selects an Item, and the editable Item form is plugged in, there's "another" submit button that should ajax submit the form data for that item, and then swap back in the (now updated) nonEditable version of the data.
The problem for me is the submission. In addition to the Edit Item form above, I've got a ajaxified submit button on the "parent" non-editable list page to handle submitting some fields below the list. The Edit Item "save"-> binding adds a button, which should do (and in fact does) nothing for itself, but it does trigger the "Parent" submit button. And I route that submit to do the save of the Edit Item form.
The non-editable Item and the editable item code swaps fine, but changes made in the editable Item form is not saved, and I figured out that that was happening because the elements in the editable Item form are not being submitted at all, following is an example of a log message I don't see at all...
bind( ... "promotitle" -> SHtml.text(editablePromo.get.promotitle.is,
(s:String) => {
trace.logUIDataManipulation("Saving new promo Title["+s+"]");
editablePromo.get.promotitle(s)
}, "id" -> "promotitle")
)
In a normal ajaxified form, all element handlers are called (if there are changes to the field, I guess...) in order of rendering, with the submit/hidden elements' handlers being called last (if they're last in the bind list.
so finally, let's get around to my question:
if you're doing in-place editing like this, how do I manage 2 submit buttons (the one for the non-editable list page plus the additional one that gets added when editing an item)?
I'm sure I don't need to refresh the page, but I can't figure out how you'd do this with Ajax.
Maybe alternatively, the in-place editable form can be submitted as a non-submit ajax action, ie. somehow that doesn't trigger the parent submit?
For anyone tripping over this question, I figured I'd share the solution I eventually found...
1)The problem was that the submit (for AJAX this is the hidden html tag) happened before the editable Item's field handlers were called. So when the AJAX update that collapsed the editable Item back into just a non-Editable list item, the data hadn't yet been updated. So what was displayed in non-editable form didn't show the update, yet if I refreshed the page in the browser, the update had been saved to the database and now showed properly.
2)The reason for the mal-ordering is that Lift assigns each form tag's server-side handler an id (which are "monotonically increasing" with an additional string added to the end). That's fine until you do an ajax live-update of a form and add fields (as I did when I inserted the Editable Item fields). These newly-added fields were assigned server-side ids that came after the hidden field that got generated as part of the initial page rendering.
3)The solution was to explicitly shove the hidden field into a much higher id using S.formGroup. See here for more details...
The example from the last link below is as follows (and differs from mine in that it uses SHtml.submit, whereas I use SHtml.hidden). It adds the constant 1,000 to the submit button's server-side handler id:
"type=submit" #> S.formGroup(1000) {
SHtml.submit("Submit", process)
}
Discussion of a problem that is essentially the same as mine: https://groups.google.com/forum/#!topic/liftweb/MYJQeVlOYFM
Description of id assignment and S.formGroup under heading "Server side function order.":
https://www.assembla.com/spaces/liftweb/wiki/cool_tips
And lastly, linked to from the last link is some example code:
https://groups.google.com/forum/#!topic/liftweb/E9z7PVhogQw

Ext JS 4 - how to add a time stamp to a text area form field?

I have an form with a text area field. I want to add some text (a date/time stamp) when the user clicks in to the form. I am assuming I need to catch a click type event when the user clicks in to the field and then edit the field value and insert the text I want. Is this the best approach?
Looking for suggestions based on using Ext JS 4 / MVC.
As mentioned by you - "I need to catch a click type event when the user clicks in to the field" - One way to achieve this and appending of timestamp to the value could be by using the focus event in following manner:
focus:function(){
var value = this.getValue();
value += new Date().getTime();
this.setValue(value);
}
Just that the date used here is the client's system date.
Hope this helps.

Zend_Form : Adding fields in sub-forms on user's click

I'm having a zend form - comprised of a number of zend - sub forms, where the user is creating a new question (its a content management system).
In one of the subforms, the user can click on a button to add more textfields, like this:
[----------]
[----------]
[click to add more]
which should give
[----------]
[----------]
[----------]
[click to add more]
I'm trying to set a flag in the sub form in question - or set a count on how many times the button has been clicked, to add that many total fields to the subform - but its simply not working.
I tried using a static count variable - but the value doesnt get incremented at all.
Any thoughts on how to do this in a Zend-subform within a zend form?
I'll definitely update if I hit a solution.
Thanks!
I used Sessions to store the click.
i tried doing this with javascript but within the subform it was not working.
if i simply have such a situation where there is just 1 form (no subforms), the javascript solution works fine.
effectively, just increment the counter by one onclick.
with sessions, or some other global variable, simply do the same - increment the counter, and unset that var when the form is submitted.
so - when u come back to the form, the previous session var value is not retained.