Custom validator that avoids saving duplicate / similar values on a database with custom forms

38 views
Skip to first unread message

mostwanted

unread,
May 20, 2019, 3:07:16 AM5/20/19
to web2py-users
I have a custom form that allows users to save information on what they are selling but the problem is that at times customers save similar items names and during a search by buyers the buyer gets a duplicate name results, what i want is to disable registering clients from saving the same item names, if the name they want to save is already in the database they should get a message that says, "that name is already used, please choose a different name for your items" , what would a custom validator that does this look like? Below is my APP code.

MODEL
db.define_table('black_market2',
               
Field('regCode', requires=IS_NOT_EMPTY()),
               
Field('fullNames', requires=IS_NOT_EMPTY()),
               
Field('id_number', 'integer', requires=IS_NOT_EMPTY()),
               
Field('contactNumber', requires=IS_NOT_EMPTY()),
               
#Field('place', 'reference locations'),
               
Field('geographical_place', requires=IS_NOT_EMPTY()),
               
Field('located_at', requires=IS_NOT_EMPTY()),
               
Field('product', requires=IS_NOT_EMPTY()),
               
Field('model', requires=IS_NOT_EMPTY()),
               
Field('productionYear', requires=IS_NOT_EMPTY()),
               
Field('productSerialNumber', requires=IS_NOT_EMPTY()),
               
Field('offerPrice', requires=IS_NOT_EMPTY()),
                format
='%(product)s')



VIEW
        <div id="itemDetails">
 
<input type="text" name="fullNames" class="field" id="fullName" placeholder="Full Names"/><br />
   
<div class="target" id="target"></div>

   
<input type="text" name="id_number" class="field" id="id_number" placeholder="ID Number"/><br />
   
<div class="target" id="target"></div>

   
<input type="text" name="contactNumber" class="field" id="contactNumber" placeholder="Contact Number"/><br />
   
<div class="target" id="target"></div>
           
   
<input type="text" name="geographical_place" class="field" id="geographical_place" placeholder="Geographical Place"/><br />
   
<div class="target" id="target"></div>

   
<input type="text" name="located_at" class="field" id="located_at" placeholder="Location"/><br />
   
<div class="target" id="target"></div>

   
<input type="text" name="product" class="field" id="product" placeholder="What is your product?"/><br />
   
<div class="target" id="target"></div>

 
<input type="text" name="model" class="field" id="model" placeholder="What is the Model?"/><br />
   
<div class="target" id="target"></div>

 
<input type="text" name="productionYear" class="field" id="productionYear" placeholder="Production Year?"/><br />
   
<div class="target" id="target"></div>

 
<input type="text" name="productSerialNumber" class="field" id="productSerialNumber" placeholder="Product Serial Number"/><br />
   
<div class="target" id="target"></div>

 
<input type="text" name="offerPrice" class="field" class="field" id="offerPrice" placeholder="Offer Price in Pulas"/><br />
   
<div class="target" id="target"></div>
           
<hr />
 
<input type="submit"  class="btn btn-default" value="Register"/>
 
<input type="reset" class="btn btn-default" value="Cancel"/>
                   
</div>
           
</div>
   
<br />



       
</div>
</form>

<script>
jQuery
('#myform').submit(function() {
  ajax
('{{=URL('blackMarket')}}', '[name=regCode], [name=fullNames],[name=id_number], [name=contactNumber],[name=geographical_place], [name=located_at], [name=product], [name=model],[name=productionYear], [name=productSerialNumber], [name=offerPrice]', 'target, target, target, target, target, target, target, target, target, target, target');
 
return false;
});
</script>


CONTROLLER
def blackMarket():
    form
= SQLFORM(db.black_market2)
   
if form.accepts(request, formname=None):
       
return DIV("Message posted",_class="target", _style="color: white; border: solid 1px green; width: 160px; background-color: green; font-weight: bold; padding: 3px; border-radius:5px;")


Lovedie JC

unread,
May 20, 2019, 3:36:32 AM5/20/19
to web...@googlegroups.com
IS_NOT_IN_DB() 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/d1607588-1a8c-48ca-963e-01060b218d89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mostwanted

unread,
May 20, 2019, 6:28:58 AM5/20/19
to web2py-users
I wanna use a custom validator with a more relevant message, the message with IS_NOT_IN_DB() reads "Value already in database or empty", how can i change the message to what i want it to be?

Mostwanted
IS_NOT_IN_DB() 

To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.
Message has been deleted

Leonel Câmara

unread,
May 20, 2019, 7:50:18 AM5/20/19
to web2py-users
Valitadors have an error_message __init__ argument. So you can just do IS_IN_DB(..., error_message=T('that name is already used, please choose a different name for your items'))

mostwanted

unread,
May 22, 2019, 5:58:10 AM5/22/19
to web2py-users
Nice one, Thanks alot
Reply all
Reply to author
Forward
0 new messages