First I had it like the way you say, but then changed it, because it hat
not been working.
Looks like if validators.Int() itself is working
>>> from formencode import validators
>>> i = validators.Int()
>>> i.to_python(3)
3
>>> i.to_python('as')
Traceback (most recent call last):
File
"/home/user/tg2env/lib/python3.2/site-packages/formencode/validators.py",
line 957, in _convert_to_python
return int(value)
ValueError: invalid literal for int() with base 10: 'as'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/tg2env/lib/python3.2/site-packages/formencode/api.py",
line 476, in to_python
value = tp(value, state)
File
"/home/user/tg2env/lib/python3.2/site-packages/formencode/validators.py",
line 959, in _convert_to_python
raise Invalid(self.message('integer', state), value, state)
formencode.api.Invalid: Bitte eine ganze Zahl eingeben
So I created an FakeValidator, just to take a look if he is called anyhow:
class FakeValidator(formencode.Validator):
def to_python(self, value, state=None):
print("\n\nabout to get hit...\n\n")
raise TGValidationError('you hit me')
But he is not fired at any time. So somehow
So the validation is not done at all.
But
str(tg.request.validation['values'])
prints out all the values entered into the form.
So at the moment I got no clue, how to tell TG2 to do validation with those
parameters...
Am Mittwoch, 15. Oktober 2014 01:22:29 UTC+2 schrieb Alessandro Molina:
>
> If execution continues in post it means it didn't detect any validation
> error, so that is the reason why request.validation is actually empty.
>
> Are you sure the price.validator and amount.validator actually work? The
> expected syntax to specify validators in sprox is to actually
> use __field_validators__ and if your issue is that nor the error_handler is
> called nor request.validation contains errors is because validation is not
> happening at all.
>
> On Wed, Oct 15, 2014 at 12:03 AM, Johannes Sauer <
> [email protected] <javascript:>> wrote:
>
>> The model:
>>
>> class DepotItem(Base):
>> __tablename__ = 'depot_item'
>>
>> id = Column(Integer, autoincrement=True, primary_key=True)
>> depot_id = Column(Integer, ForeignKey('depot.id'), nullable=False)
>> price = Column(Float)
>> amount = Column(Integer)
>> stock_id = Column(Integer, ForeignKey('stock.id'), nullable=False)
>>
>> The form:
>>
>> class NewDepotItemForm(AddRecordForm):
>> __model__ = DepotItem
>> __omit_fields__ = ['depot']
>> __require_fields__ = ['price', 'amount']
>> __dropdown_field_names__ = {'stock':'symbol'}
>> stock = StockField
>> price = TextField
>> price.validator = validators.Number(min=0.01)
>> amount = TextField
>> amount.validator = validators.Int(min=1)
>>
>> new_depotitem_form = NewDepotItemForm(DBSession)
>>
>> The POST method:
>> @validate(new_depotitem_form, error_handler=new)
>> @expose()
>> def post(self, kw):
>> print("\n\n\n"+str(tg.request.validation['errors']))
>>
>> del kw['sprox_id']
>> depot_item = DepotItem()
>> depot_item.price = kw.get('price')
>> depot_item.amount = kw.get('amount')
>> depot_item.depot = self.entry
>> depot_item.stock = DBSession.query(Stock).filter_by(symbol=kw.get
>> ('stock')).one()
>> DBSession.add(depot_item)
>> flash('Aktie gekauft')
>> redirect('/depot/'+str(self.entry.id))
>>
>> When I enter something else than a number the application crashes with a
>> StatementError, because those Strings could not be converted to Float or
>> Integer. But according to my @validat decorater the new procedure should be
>> called again. But poorly there where no validation errors detected (as the
>> print in the POST method only shows an empty dict).
>>
>> Has anybody an idea, why this is not working?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "TurboGears" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]
>> <javascript:>.
>> Visit this group at http://groups.google.com/group/turbogears.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
--
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/turbogears.
For more options, visit https://groups.google.com/d/optout.