Documentation

Report Edit

Validator

import { Validator } from 'cx/widgets';

The Validator widget is used for additional validation on calculated values.

Please enter X and Y so that X + Y = 20.

+
=
0
  • Please enter X.
  • Please enter Y.
  • X + Y != 20
Validator
<ValidationGroup layout={LabelsTopLayout}
    valid-bind="$page.valid"
    errors-bind="$page.errors"
>
    <NumberField label="X" value-bind="$page.x" required requiredText="Please enter X." style="width: 50px"/>
    +
    <NumberField label="Y" value-bind="$page.y" required requiredText="Please enter Y."  style="width: 50px"/>
    =
    <LabeledContainer label="X + Y">
        <span text-expr="{$page.x} + {$page.y}" />
    </LabeledContainer>
    <Validator
        value-expr="{$page.x} + {$page.y}"
        onValidate={(value) => value != 20 && 'X + Y != 20'}
    />
</ValidationGroup>
<ul>
    <Repeater records-bind="$page.errors">
        <li text-bind="$record.message" style="color: red;" />
    </Repeater>
</ul>
Copied!Cx Fiddle
import { ValidationError } from 'cx/widgets';

Besides validation, the Validator widget can render error messages with the support of the ValidationError component. You can apply different styling for each ValidationError instance.

ValidationError
<ValidationGroup
    valid-bind="valid"
    errors-bind="errors"
    visited-bind="visited"
>
    <LabelsLeftLayout>
        <TextField
            value-bind="username"
            required
            label="Username"
            asterisk
        />
        <TextField
            inputType="password"
            value-bind="password"
            required
            label="Password"
            asterisk
        />
        <TextField
            inputType="password"
            value-bind="repeatPassword"
            required
            label="Repeat password"
            asterisk
        />
        <Validator
            value-expr="{password} != {repeatPassword}"
            onValidate={(value) => value && "Passwords don't match."}
        >
            <div style="margin: 10px 0">
                <ValidationError
                    style={{
                        padding: "10px",
                        background: "rgba(255, 0, 0, 0.3)",
                        borderRadius: "5px"
                    }}
                />
            </div>
        </Validator>
        <Button submit>Sign Up</Button>
    </LabelsLeftLayout>
</ValidationGroup>
Copied!Cx Fiddle
PropertyDescriptionType
value

Value to be validated. This can a simple binding, an expression or a structured selector containing multiple values.

string/number/object
Methods
onValidate(value, instance)

A callback function used for validation. Function should return an error message string or false, if value is valid. Alternatively, a Promise should be returned for server-side validation scenarios.

onValidationException(error, instance)

A callback function that is called if validation fails.