Optional field


Definition

If the field is marked as optional, if no value is specified the validator didn't send an error with field is undefined.

Usage

addRule("field -> optional")
addRule("website, facebook -> optional: ([facebook])") // website is requiered but facebook isn't

Tests

@Test
public final void testRuleOptionalOk() throws Exception {
    FTest.getInstance().test("optional")
            .rule("test", "optional", true)
            .field("test", "1").build()
            .test().rule("test, facebook", "optional: ([facebook])", true)
            .field("test", "1").build()
            .test().rule("test", "optional", true)
            .build().test()
            .rule("test", "optional|email", true)
            .field("test", "[email protected]")
            .build();
}

@Test
public final void testRuleOptionalNotOk() throws Exception {
    FTest.getInstance().test("optional")
            .rule("test", "optional|higher:5", false)
            .field("test", "1").build()
            .test().rule("test, facebook", "optional: ([test])", false)
            .rule("test", "optional", true) // to detect that test is optional but not facebook
            .field("test", "1").build();
}

results matching ""

    No results matching ""