On Mar 15, 5:28 am, Mengu <[email protected]> wrote: > > First of all, I am not sure what is the problem you are trying to > > solve. Even if you have custom forms and you build all the form html > > manually you can still use the existing validation mechanism. > > I don't want to use crud or sqlform or any html helper. i build my > forms manually. and actually if you show me how to make validation > with manual forms, it would be good. > > > Second, there is a logical problem. Validators are designed to take > > the input as submitted by a web form and not as passed to the input > > function. For example for a IS_DATE a validator expects a "string" in > > the user locale, while input expects a datetime.date object or an ISO > > serialized date. > > I didn't get this one. how input expects a datetime object? > > > Fourth, this breaks backward compatibility because insert must be > > return a Reference object and 0 if the record was not inserted. If you > > return a non-empty list on error this will may break some of the > > existing apps. > > This is correct so I think I can add a validate method before calling > insert in my controller to see if the data is valid. and then i can > call insert.
See how sql.py:Field.validate() works - you can add validation of your own (in any place) and _still_, and in an order you choose, run thru all the "requires" ... that is, build a stack of validation. It's not too terribly difficult. - Yarko > > On Mar 15, 12:11 pm, mdipierro <[email protected]> wrote: > > > No way. ;-) > > > First of all, I am not sure what is the problem you are trying to > > solve. Even if you have custom forms and you build all the form html > > manually you can still use the existing validation mechanism. > > > Second, there is a logical problem. Validators are designed to take > > the input as submitted by a web form and not as passed to the input > > function. For example for a IS_DATE a validator expects a "string" in > > the user locale, while input expects a datetime.date object or an ISO > > serialized date. > > > Third, this will cause a slow down because some validators require > > database IO to work, therefore this cannot be the default behaviour. > > > Fourth, this breaks backward compatibility because insert must be > > return a Reference object and 0 if the record was not inserted. If you > > return a non-empty list on error this will may break some of the > > existing apps. > > > You can do, much simply: > > > def validate_and_insert(table,**vars): > > form=SQLFORM(table) > > form.accepts(vars, formname=None) > > if not form.errors: return table.insert(**form.vars) > > return form.errors > > > errors = > > validate_and_insert(db.tablename,field1='value1',field2='value2', > > etc.) > > if not errors: .... record was inserted > > > Note that table.insert(...) takes form.vars not vars since they are > > not the same. > > > Massimo > > > On Mar 15, 4:52 am, Mengu <[email protected]> wrote: > > > > Hi all, > > > > Currently web2py validation works on CRUD and SQLFORM. But some of us > > > don't use those and build their forms by hand. But when this is the > > > issue we have to validate every input ourselves in the controller > > > which is not nice so I have came up with an idea. I can set an > > > error_message per field object as an addition to requires attribute. > > > Let's say this is our table: > > > > db.define_table( > > > 'users', > > > Field('name'), > > > Field('email') > > > ) > > > > And this is how we can define our requires attribute: > > > db.users.name.requires = IS_NOT_EMPTY() > > > db.users.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db, > > > 'users.email')] > > > > Now, we can add our error messages like this: > > > db.users.name.requires = IS_NOT_EMPTY() > > > db.users.name.error_message = "Please enter your name" > > > db.users.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db, > > > 'users.email')] > > > db.users.email.error_message = ["Given e-mail is not a valid one.", > > > "Sorry, this e-mail already exists in our database"] > > > > My patch for object validation is working when the insert method is > > > called. So the following example will return a list of errors: > > > > print db.users.insert(name="", email="b...@email") > > > > and the return is: ['Please enter your name', 'Given e-mail is not a > > > valid one.'] > > > > so we can iterate over the errors and show them. > > > > you can see my addition > > > here:http://github.com/mengu/web2py-patches/blob/master/gluon/sql.py#L1888... > > > > let me know what you think or if i should follow any other way for > > > this. > > -- You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/web2py?hl=en.

