New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to validate form element names grouped as array? #5
Comments
What framework are you using to create the forms? Can you provide a more complete example? |
I am not using any framework to create forms. Simply using the jade. If I put 'name' as the name of the textfield I can access the field and validate it But for 'company[name]' as the name I couldn't validate |
express-validator uses the |
I'm experiencing this issue too... What is the workaround? e.g. you can't do req.sanitize('object.property') ... and you can't do req.sanitize(req.query.object.property) |
Manually doing this is simple enough... but we should support the above. var myProperty = req.query.myObject.myProperty;
if(myProperty !== 'undefined' && myProperty === 'true') {
myProperty = true;
} else {
myProperty = false;
} |
I'm also having this problem and looking for a solution. Dumping
However, there is no way to actually access the It would be great to have query array support! |
in meantime you can use var Validator = require('validator').Validator;
var v = new Validator();
v.error = function(msg) {
console.log('Fail');
}
v.check('abc').isInt(); //'Fail' per examples on https://github.com/chriso/node-validator |
e.g. edit: you might need to do |
see edit to previous comment btw |
Ok, I see. Express is using node-querystring instead of the node-internal var querystring = require('querystring');
var qs = require('qs');
var string = 'foo[bar]=baz'
querystring.parse(string);
// -> { 'foo[bar]': 'baz' }
qs.parse(string);
// -> { foo: { bar: 'baz' } } Seems like we have to put some effort into express-validator to support nested bodies.... We should probably write a I'd be more than happy to apply a corresponding patch if anybody is willing to write one. I won't be able to do that during the next two weeks however myself... Apologies! |
@ctavan Did you fixed this issue? Thank you so much! |
Sorry, I'm quite busy these days. Anyone willing to write a patch? I'm happy to accept pull requests. |
I've just committed a solution on my pull-request |
req.check = function(param, fail_msg) {
if(this.param(param) === undefined) {
return validator.check(param, fail_msg);
} else {
return validator.check(this.param(param), fail_msg);
}
}; with this small hack you can validate arays: for input: name[first] req.assert(req.body.name.first, 'No first name.').notEmpty(); for input: email req.assert(req.body.email, 'No email.').notEmpty(); or req.assert('email', 'No email.').notEmpty(); |
@turbyho I'm going to check that asap |
Your hack would work in a percfect world ;) // req.check(req.body.name.first).notEmpty(); TypeError: Cannot read property 'first' of undefined Also, since you don't send the parameter(s) name, but the value directly, this enhancement wouldn't work anymore |
when using property, you have the "problem" anywhere. the node-validator can use property and have the same "problem". var check = require('validator').check
check(xxx.yyy.zzz).notEmpty(); yes, here is classic solution for perfect world :-) var check = require('validator').check
try {
check(xxx.yyy.zzz).notEmpty();
check(yyy.zzz.aaa).notEmpty();
}
catch(err) {
console.log(err);
} |
Is this still an issue now? |
Yeah, that is what I thought. I added dot notation support as well. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
form(action='/', method='POST')
input(name='company[name]', type='text')
input(name='company[description]', type='text')
input(name='company[address]', type='text')
input(type='submit')
For the above form how can I use the express-validator?
req.assert('company[name]', 'Company Name is required').notEmpty(); doesn't seems to work
The text was updated successfully, but these errors were encountered: