Web2py字段值取决于另一个字段 - (在定义模型时验证)

时间:2016-10-13 12:15:53

标签: python web2py data-access-layer

请考虑下表:

\"

现在,'marks_obtained'字段的值不能大于'total_marks'。

我试过以下

<Grid x:Name="ImageGrid">
        <Grid.Projection>
            <PlaneProjection/>
        </Grid.Projection>
        <Grid x:Name="grid" Margin="10,-176,10,10" Height="37" Width="37">
            <Grid.Projection>
                <PlaneProjection/>
            </Grid.Projection>
            <Ellipse x:Name="grd" VerticalAlignment="Center"  Fill="#E84C3D" Height="37" Width="37" StrokeThickness="5" Stroke="#E84C3D"/>
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="VIII"/>           
              </Grid>        
        <Image  x:Name="ImageBlock" Source="Assets/ic_out_circle.png" Height="150" HorizontalAlignment="Center"
               Stretch="Uniform" Width="230">              
            <Image.Triggers>
                <EventTrigger RoutedEvent="Image.Loaded">
                    <BeginStoryboard>
                        <Storyboard x:Name="SpinAnimation">
                            <DoubleAnimation RepeatBehavior="Forever" To="0" From="360" Duration="0:0:7" Storyboard.TargetName="ImageGrid"
                Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationZ)"/>
                        </Storyboard>                            
                    </BeginStoryboard>

                    <BeginStoryboard>
                    <Storyboard x:Name="SpinAnimation1">
                            <DoubleAnimation RepeatBehavior="Forever" To="360" From="0" Duration="0:0:7" Storyboard.TargetName="grid"
                Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationZ)"/>
                    </Storyboard>
                    </BeginStoryboard>

                </EventTrigger>
            </Image.Triggers>
        </Image>
    </Grid>

但这不起作用。我收到以下错误:

db.define_table('bio_data',
            Field('name', 'string'),
            Field('total_mark', 'integer', requires=IS_EMPTY_OR(IS_INT_IN_RANGE(0, 1e100))),
            Field('marks_obtained', 'integer')
            )

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,我刚刚找到了一个优雅的解决方案。首先,您需要注意,在进行任何验证之前,字段的值会传递到模型,因此,在模型中,您可以通过request.vars操作它们。之后其他一切都很简单:只需创建自己的验证器,提取字段的值并根据需要使用它们。一个例子会做得更好:

让我们知道你有一个包含两个字段的表:

    dbOBJECT.define_table("example",
                          Field("high_number",
                                "integer",
                                label= T('High number'),
                                requires=[IS_NOT_EMPTY(T(stringBuilder.emptyField))]),
                          Field("other_number",
                                "integer",
                                label= T('Lower number'),
                                requires=IS_NOT_EMPTY(T(stringBuilder.emptyField))))

你需要第二个数字低于第一个数字。出于解释目的,我们创建一个自定义验证器。这可以在相同的db.py文件中完成

    class VALIDATE_NUMBERS(object):
        def __init__(self, error_message="Error, this value can't be greather than the above one"):
            self.error_message = error_message

        def __call__(self, value):
            error = None
            high_number=request.vars['high_number']

            if high_number < value:
                error = self.error_message
            return (value, error)

嗯,另一件事是在模型上包含这个验证:

    dbOBJECT.define_table("example",
                          Field("high_number",
                                "integer",
                                label= T('High number'),
                                requires=[IS_NOT_EMPTY(T(stringBuilder.emptyField))]),
                          Field("other_number",
                                "integer",
                                label= T('Lower number'),
                                requires=IS_NOT_EMPTY [(T(stringBuilder.emptyField)), VALIDATE_NUMBERS()]))

魔力完成!!!享受...

答案 1 :(得分:1)

您可以使用onvalidation回调函数轻松完成此操作。读这个 Form and validators - onvalidation

第二个解决方案是你需要将'验证器与依赖关系'和IS_EXPR验证器结合起来。读:

  1. Validators with dependencies

  2. IS_EXPR

  3. 控制器中添加验证器,如下所示,我没有对此进行过测试,但您可以从中获知。

    is_total_less = int(request.vars.marks_obtained) < int(request.vars.total_mark)
    
    db.bio_data.marks_obtained.requires = IS_EMPTY_OR(
    IS_EXPR('%s' % is_total_less,
            error_message='Marks Obtained should be smaller than Totak Marks'))
    

    确保request.vars可用。