Form does not postback when submit is clicked, jade, node.js - node.js

I have a form created with jade, it does not postback when submit button is clicked.
I have looked at many similar problems, I tried the solutions, which include ensuring that the input fields all have a 'name', I have made sure all the input field have a name but the form still does not post back, here it is
//views/users/new.jade
h1 New User
form(action="/users", method="POST")
p
label(for="username") Username
input#username(name="username")
p
label(for="name") Name
input#name(name="name")
p
label(for="bio") Bio
textarea#bio(name="bio")
p
input(type="submit",value="Create")
The post handler is this
//routes/users
module.exports=function(app){
app.post('/users',function(req,res){
if(users[req.body.username]){ //Check if user exists
res.send('Confllict, 409')
}else
{
//add to users list
users[req.body.username]=req.body;
res.redirect('/users');
}
});
};

Jade uses indention to show what content goes between the tags
p
this is inside the p tags
also there is shorthand
p this is also in the tag
in your original code you are telling Jade that there is a p tag then the next line is outside the p tags.

This is an easy problem to run into if you aren't careful with indentation using Pug (previously Jade). I realize this is an old question, and it has several hits so perhaps this answer will shed some light for new Pug users.
The problem with the OP's code is the Form does not know how to post without a button, and the Submit button doesn't know where to post to without being associated with the Form. Why? Indentation.
Jade and Pug tips for success:
They produce html, so it helps to think in html layouts when developing the template (would you put text outside of your p element? Of course not).
Pug uses indentation to form layout and element hierarchy.
Html Forms must enclose the elements that are to be associated or bound to it.
A Submit Button does nothing unless you define an event handler.
Forms define a destination in their definition, which a button type element can leverage.
In the code listed by the OP, every Pug element is left-aligned. This puts all elements at the same level, so there is no hierarchy or nesting. Since there is no other event handler explicitly defined for the Submit button, nothing happens when it is clicked, despite the form having an action and a valid method.
To fix this, the Input (submit button) can be nested inside the Form.
Reworking the code to include indented hierarchy will enable the POST to function as expected (removed all the other elements for readability):
h1 New User
form(action="/users", method="POST")
label(for="username") Username
input#username(name="username")
label(for="name") Name
input#name(name="name")
label(for="bio") Bio
textarea#bio(name="bio")
input(type="submit",value="Create")
The resulting form won't be a pretty, but it will function:
The Form has an action and method.
All dependent elements are indented.
The input has a type of Submit and is child to the form.
Of course, a server must have a binding at the address to receive the data in order to do anything with it.

Related

pug nested content in input

Hi I'm trying to create nested mixins which can display bootstrap radio button in pug
mixin input(textinput)
label.btn.btn-secondary
input(type="radio" name="options" id="option1" autocomplete="off")= textinput
mixin btn-toggle-group(textinput)
.btn-group(data-toggle="button")
+btn-toggle-group
+input('coca')
+input('pepsi')
+input('orangina')
+input('lemonade')
but it gives me error with input is a self closing element but it contains nested content
The error is due to trying to make textinput a TextNode child of a radio button via = which obviously has to fail.
If one wants to render a next sibling text node, there is the | operator (pipe) and interpolation. But one also can go with just another html element like span. Bothe variants are provided with the example code.
One should keep in mind that a radio button (especially within a radio group) that does not feature a value is of almost no use. It might be useful, assigning textinput to the radio button's value attribute too.
One also should avoid passing id or class attributes directly into a tag, one instead should append each of it either by # for ids or by . for class names.
And last, for one wants to append all the labeled radio controls to the btn-group classified div element, one needs to mark this by a carefully indented block keyword.
mixin input(textinput)
label.btn.btn-secondary
input#option1(type='radio' name='options' autocomplete='off' value=textinput)
// span.label= textinput
// span= textinput
| #{textinput}
mixin btn-toggle-group(textinput)
.btn-group(data-toggle='button')
block
+ btn-toggle-group
+ input('coca')
+ input('pepsi')
+ input('orangina')
+ input('lemonade')
The above provided sanitized and tested code now does work as intended.
And reading the pug documentation (attributes, interpolation, mixins, inheritance/templates/blocks, ) does help too.

How can we use ONLY client side script for "hide/whens"?

I am working on a large, worldwide application, which includes access from areas of low bandwidth. As such, I want to use a minimum of SSJS or partial refreshes for all the complex hide/when calculations. Here is what I have so far for a simple "hide/when":
A Yes/No radio button, with CSJS to show a panel ("Yes") or hide the
panel ("No").
The panel has a formTable inside it, and the values are shown or hidden, as per #1.
In the XPage's onClientLoad, the following code is run:
// "getRadioValue" is a simple script to return the value of a radio button
var v_value = getRadioValue("#{id:radioButton}");
v_div = '#{javascript:getClientId("radioButtonPanel")}';
// show or hide div simply use dojo to change the display of the panel
if (v_value == 'Yes') {
showDiv(v_div);
} else {
hideDiv(v_div);
};
For a new document, the onClientLoad script will hide the "radioButtonPanel" successfully. Changing the radio button to "Yes" will show the radioButtonPanel, just as clicking "No" will hide it. It works great! :-)
Once the document is saved and reopened in read mode, though, the onClientLoad CSJS event should read the saved value in the document, and decide to show the panel or not. When the document is opened in edit mode, the onClientLoad fires, reads the radioButton value and successfully shows or hides the panel.
This is what I've tried so far, to get it to work in read mode:
In CSJS, using "#{javascript:currentDocument.getItemValueString('radioButton'}" to get the value,
Doing some calculations in the "rendered" or "visible" properties, but that's SSJS and, if hidden, prevents any of the "show/hideDiv" CSJS visibility style changes.
Adding an old fashioned "div" to compute the style (which is what I used to do before XPages), but since I can't do pass-thru html any more, I can't seem to get a CSJS calculation for the style. Ideally, I can do something like this:
<div id="radioButtonPanel" style="<ComputedValue>">
Where the ComputedValue would read the back end value of the document, and decide to add nothing or "display:none".
Note that I don't want to use viewScopes, since this long form would need many of them for all the other hide/when's.
Is there any way to make this 100% CSJS? I feel like I'm very close, but I wonder if there's something I'm just missing in this whole process.
First, rather than computing style, I'd recommend computing the CSS class instead -- just define a class called hidden that applies the display:none; rule. Then toggling visibility becomes as simple as a call to dojo.addClass or dojo.removeClass.
Second, I see that you're using the #{id:component} syntax to get the client ID of the radio button but using SSJS to get the client ID of the panel. Use the id: syntax for both; this is still just a server-side optimization, but if there are many instances of these calculations, it adds up. Similarly, replace #{javascript:currentDocument.getItemValueString('radioButton'} with #{currentDocument.radioButton}. Both will return the same value, but the latter will be faster.
Finally, any attribute of a pass-thru tag (any component with no namespace, like xp: or xc:) can still be computed, but you'll need to populate the expression by hand, since the editor doesn't know which attributes for valid for these tags, and therefore doesn't provide a graphical expression editor. So if the ideal way to evaluate the initial display is by wrapping the content in a div, the result might look something like this:
<div class="#{javascript:return (currentDocument.getValue('radioButton') == 'Yes' ? 'visible' : 'hidden');}">
<xp:panel>
...
</xp:panel>
</div>

CakePHP 2.1: Using CSRF protection on Elements

I have a somewhat special situation with one of my CommentsController actions, the "add" action.
Since I have an "Add Comment" form on my "view" View of PostsController, I have an "add_comment" Element instead of an "add" View for the Comments. This is that I can just insert the Element with the form inside the "view" View of Posts. I hope this does not sound too complicated.
Now, inside the "add_comment" Element, the form gets submitted to the actual "add" action of CommentsController. If the form has to be rendered again, because of invalid user input for exaxmple, the form gets rendered again by - this time - calling the "add" View of Comments. This "add" View only again includes the "add_comment" Element, which - as was said above - includes the actual form.
Now the thing is, I added the Security component to my CommentsController in order to get CSRF protection. All my admin actions of CommentsController get the protection i.e. e token is rendered inside the form and checked upon submitting the form.
Still, the add_comments Element form does NOT get a token. Probably because it is an Element and not the corresponding View.
How can I manually insert a token into the Element's form or how do I better solve this?
Thank you
EDIT: Se also my other question regarding the setup - it should explain it a little
EDIT2: This is what's inside my add_comment Element
EDIT3: This is the HTML output of the add_comment element. The nonce should be in the <div style="display:none;"> part.
EDIT4: I put Debugger::log($this->Form->request->params); inside the element (add_comment.ctp) and the view (add.ctp) and this shows the output.
I also uploaded the the whole CommentsController (here). Please note that I specified a blackhole function to catch errors of type 'auth'. This is because, every time I entered invalid data in the add_comments.ctp Element and - by submitting the form - the actual add Action of CommentsController was invoked (and thus the add View of comments), I got a blackhole error of type 'auth', which I could not explain so I caught it with this way - this might even be related to my problems.

Yesod form with multiple buttons

I have a Yesod form for editing the contents of some static pages which are written using markdown (processed using Pandoc). I want to have two buttons - a 'preview' button which processes the markup and displays the result underneath the form, and a 'submit' button which saves the contents to the database.
What is the simplest way to do this with Yesod? All the form examples in the Yesod book have exactly one button. I've looked at the exposed functions / api, but I even if I add more than one submit button with different names and/or values to the form I can't figure out how to get Yesod to tell me which one was pressed.
Can anyone give me a simple example of a form with more than one button in Yesod, which trigger different actions?
You can just use the Input form functions to get the raw values, and explicitly set a name attribute on the various buttons. Something like this in the HTML:
<input type="submit" name="preview" value="Preview">
And in the Haskell code:
res <- runFormPost ...
isPreview <- runInputPost $ iopt boolField "preview"
if isPreview then ... else ...
Sorry if this doesn't typecheck, I don't have my normal development system right now. But I think this is the right general approach.

Drupal 6, Create a form in a node which save data to another content type (cck)

I need to create a form in the view (not in the edit) of a content type A.
This form need to submit the data to a content type B.
I notice that node/<nodeID/edit is the "action" of the form which let you edit a node. But if I put the same action in my form on A it shows me the editing node page of B.
My form is as simple as:
<form action="xxx">
<input type="text" name="cck_field_to_be_added_in_B" value="foobar">
</form>
Setting the xxx action isn't enough, because the FAPI requires form_id and other stuff... So, How can I build a form which is "correct" and sends the data in the rigth way?
Any idea?
----EDIT----
Using the rimian solution it worked. Heres are the dettails:
I needed the form in the view of a node created with CCK. So I have my module with two functions:
function getForm(){
return drupal_get_form('buildForm');
}
function buildIngredientsForm(){
$form[]... //bla bla bla build the form
return $form;
}
Because I want this inside a cck content, than I can "hack" the theming system of CCK. Here explain how: http://drupal.org/node/206980
Now, whenever you wats to display your form just call print mymodule.getForm(); and the magic is done.
Regards,
Segolas
Add another callback to the array of functions that are called when the node's edit form is submitted. You'll need to understand the basics of the form API. It's not too hard.
It goes something like this:
$form['#submit'][] = 'my_function';
Then..
function my_function() {
//do stuff to the other node
}
See:
http://api.drupal.org/api/drupal/includes--form.inc/group/form_api
http://drupal.org/project/examples

Resources