Git Product home page Git Product logo

Comments (11)

ljharb avatar ljharb commented on August 20, 2024
forms.create({
    csrf: forms.widgets.hidden({
        required: true,
        validators: [
            function (form, field, callback) {
                if (form.fields.csrf !== getExpectedCSRFTokenValue()) {
                    callback('bad csrf token');
                } else {
                    callback();
                }
            }
        ]
    })
}).bind({ csrf: 'a token' });

? I haven't tried this yet, but something like that should work?

from forms.

ljharb avatar ljharb commented on August 20, 2024

If you get a working solution, I think I've changed my mind - a generic "must match value" widget, that takes a "getValue" function and an "initialValue", might serve your purposes quite well.

from forms.

d4goxn avatar d4goxn commented on August 20, 2024

This is just weird:

> var form = forms.create({ csrf: forms.widgets.hidden() });
> form.bind({ csrf: 'token' });

Throws an error, TypeError: Object #<Object> has no method 'bind', but form certainly does have a bind method:

> form.bind
[Function]

This is with 0.3.0, installed using npm. I would like to clone the repo and start poking around, but right now I have a late project and so I think I should just to do the hidden input manually in the templates for now. Thank you for all your work on this much needed module, I hope I can sort this out after I catch up to my deadline.

from forms.

ljharb avatar ljharb commented on August 20, 2024

Sounds good, I'd love to make forms more useful. I think the argument to form.bind might need to be different - maybe { fields: { csrf: 'token' } }? i'll take a look at the code later

from forms.

ljharb avatar ljharb commented on August 20, 2024

I've added a matchValue validator that takes a getter function.

from forms.

elantion avatar elantion commented on August 20, 2024

My situation is quite similar to d4goxn. And I just need to have a value attribute with the csrfToken in it.But wired, I try this:

    var loginForm = forms.create({
         username: fields.string({ required: validators.required('xxxx') }),
         password: fields.password({ required: validators.required('xxxx') }),
        csrfToken: widgets.hidden({
            required: true
        })
    }).bind({ fields:{ csrfToken: req.csrfToken() }});

"no bind function error"

and this:

    var loginForm = forms.create({
         username: fields.string({ required: validators.required('xxxx') }),
         password: fields.password({ required: validators.required('xxxx') }),
        csrfToken: widgets.hidden({
            required: true
                 value: req.csrfToken()
        })
    });

I dump the csrf value, it's there. But at the end, the form didn't have a value attribute.

And I try 'value':req.csrfToken() too.
it dosen't work too.

pls help, thnks.

from forms.

ljharb avatar ljharb commented on August 20, 2024

@elantion In your second snippet you're missing a comma - is that just a copy/paste error, or is that in your original code?

As for "no bind function error" can you gist the actual output you're getting?

from forms.

elantion avatar elantion commented on August 20, 2024

Thanks your reply.
The second snippet syntax error just a copy/paste mistake. The code is find.
'no bind function error' detail :

TypeError: undefined is not a function
   at /Users/jamesying/Desktop/lc/node_modules/forms/lib/forms.js:36:47 
   at Array.forEach (native)
   at Object.f.bind (/Users/jamesying/Desktop/lc/node_modules/forms/lib/forms.js:34:35)
   at /Users/jamesying/Desktop/lc/routes/articles.js:25:8
   at Layer.handle [as handle_request] (/Users/jamesying/Desktop/lc/node_modules/express/lib/router/layer.js:82:5)
   at next (/Users/jamesying/Desktop/lc/node_modules/express/lib/router/route.js:110:13)
   at csrf (/Users/jamesying/Desktop/lc/node_modules/csurf/index.js:97:5)
   at Layer.handle [as handle_request] (/Users/jamesying/Desktop/lc/node_modules/express/lib/router/layer.js:82:5)
   at next (/Users/jamesying/Desktop/lc/node_modules/express/lib/router/route.js:110:13)
   at Route.dispatch (/Users/jamesying/Desktop/lc/node_modules/express/lib/router/route.js:91:3)

from forms.

elantion avatar elantion commented on August 20, 2024

Yes, I am making another form. But I use the same way to create the form.

/* add article */
router.get('/add', csrfProtection, function(req, res) {
    var add_form = forms.create({
        title: fields.string({ required: true }),
        content: fields.string({ required: validators.required('请输入内容') }),
        csrfToken:forms.widgets.hidden({required:true})
    }).bind({ fields:{ csrfToken: req.csrfToken() }});
    add_form.toHTML();
    res.render('article/add',{
        add_form:add_form
    });
});

from forms.

ljharb avatar ljharb commented on August 20, 2024

@elantion This discussion should really go in a brand new issue. Can you file one?

from forms.

elantion avatar elantion commented on August 20, 2024

sure, I will do it.

from forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.