Validation 在没有活页夹的情况下调用验证用户反馈&;瓦丁豆8

Validation 在没有活页夹的情况下调用验证用户反馈&;瓦丁豆8,validation,vaadin,vaadin8,Validation,Vaadin,Vaadin8,在vaadin8中,内置的验证特性是通过连接到bean的as和as来建立的。我理解这是如何运作的,而且运作良好,我感谢瓦丁团队在那里的思考和努力 但是…我只需要一个简单的快速脏数据输入字段,只接受-1、0和1作为值。我喜欢Vaadin validation的特性,即通过将字段/标题标记为“砰”和红色等来拒绝错误输入 ➥ 有没有一种方法可以调用验证风格的反馈给用户,而不必费心去定义bean和建立绑定器 作为一种解决方法,我可以用一个弹出菜单(也称为下拉列表)替换我的数据输入字段。但我的问题在需要用

在vaadin8中,内置的验证特性是通过连接到bean的as和as来建立的。我理解这是如何运作的,而且运作良好,我感谢瓦丁团队在那里的思考和努力

但是…我只需要一个简单的快速脏数据输入字段,只接受-1、0和1作为值。我喜欢Vaadin validation的特性,即通过将字段/标题标记为“砰”和红色等来拒绝错误输入

➥ 有没有一种方法可以调用验证风格的反馈给用户,而不必费心去定义bean和建立绑定器


作为一种解决方法,我可以用一个弹出菜单(也称为下拉列表)替换我的数据输入字段。但我的问题在需要用户快速简单输入时仍然很有用,例如在对话框中,不需要所有的数据输入表单。

Binder
不是为单个字段使用而设计的

瓦丁8 使用Vaadin 8进行单字段验证的最佳替代方法是使用字段的
ValueChangeListener
进行挂钩,并在值更改事件中执行所需的操作

AbstractComponent::setComponentError(ErrorMessage componentError)
注意,然而,在瓦丁8区,确实有方法。通过调用此方法,您可以使用
ValueChangeListener
By
setComponentError(…)
对单个字段进行验证,从而获得与表单中的活页夹类似的外观和感觉。这适用于文本字段、按钮等

请参阅手册的第页

button.setComponentError( new UserError( "Bad click" ) ) ;

瓦丁7 和之间有区别。使用Vaadin 7,可以将验证器直接分配给字段。

示例应用程序 该方法是正确的,解决了问题。解决方案是调用
AbstractComponent::setComponentError

下面是一个完整的小型Vaadin 8.5.2应用程序,用于展示这一点

此应用程序有两个小部件,一个
文本字段
和一个
按钮
,可以标记为处于正常状态

…或处于错误的状态

这是应用程序的整个
.java
文件

package com.basilbourque.example;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.UserError;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;

import java.util.List;
import java.util.Set;

/**
 * This UI is the application entry point. A UI may either represent a browser window
 * (or tab) or some part of an HTML page where a Vaadin application is embedded.
 * <p>
 * The UI is initialized using {@link #init(VaadinRequest)}. This method is intended to be
 * overridden to add component to the user interface and initialize non-component functionality.
 */
@Theme ( "mytheme" )
public class MyUI extends UI {

    @Override
    protected void init ( VaadinRequest vaadinRequest ) {

        // TextField allows only 'dog' or 'cat' as values.
        Set< String > animals = Set.of( "dog" , "cat" );
        TextField dogOrCat = new TextField();
        dogOrCat.setCaption( "Type dog or cat:" );
        dogOrCat.addValueChangeListener( valueChangeEvent -> {
            if ( animals.contains( dogOrCat.getValue() ) ) {
                dogOrCat.setComponentError( null );
            } else {
                dogOrCat.setComponentError( new UserError( "Oops! You typed something other than 'dog' or 'cat'." ) );
            }
        } );

        // Button which is deemed to be in good condition or erroneous condition by a radio buttons pair.
        Button button = new Button( "Example" );
        button.addClickListener( e -> {
            Notification.show( "This button does nothing." , Notification.Type.HUMANIZED_MESSAGE );
        } );

        // Radio-buttons, to control the good or error condition of button above.
        List< String > radioItems = List.of( "No error" , "Error" );
        RadioButtonGroup< String > radios =
        new RadioButtonGroup<>( "Make button:" );
        radios.setItems( radioItems );
        radios.setValue( radioItems.get( 0 ) ); // Set 1st item by default (index counting = 0).
        radios.addValueChangeListener( valueChangeEvent -> {
            if ( radios.getValue().equals( radioItems.get( 1 ) ) ) {  // Index-counting, so `1` = 2nd list item "Error".
                button.setComponentError( new UserError( "Bad button" ) );
            } else {
                button.setComponentError( null );
            }
        } );

        // Arrange
        final VerticalLayout layout = new VerticalLayout();
        layout.addComponents( dogOrCat , button , radios );
        setContent( layout );
    }

    @WebServlet ( urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true )
    @VaadinServletConfiguration ( ui = MyUI.class, productionMode = false )
    public static class MyUIServlet extends VaadinServlet {
    }
}

package com.basilbourque.example;
导入javax.servlet.annotation.WebServlet;
导入com.vaadin.annotations.Theme;
导入com.vaadin.annotations.VaadinServletConfiguration;
导入com.vaadin.server.UserError;
导入com.vaadin.server.vaadin请求;
导入com.vaadin.server.VaadinServlet;
导入com.vaadin.ui.*;
导入java.util.List;
导入java.util.Set;
/**
*此UI是应用程序入口点。UI可以表示浏览器窗口
*(或选项卡)或嵌入Vaadin应用程序的HTML页面的某些部分。
*
*UI是使用{@link#init(VaadinRequest)}初始化的。该方法旨在:
*重写以将组件添加到用户界面并初始化非组件功能。
*/
@主题(“神话主题”)
公共类MyUI扩展了MyUI{
@凌驾
受保护的void init(VaadinRequest VaadinRequest){
//TextField仅允许“dog”或“cat”作为值。
集合动物=集合(“狗”、“猫”);
TextField dogOrCat=新TextField();
设置标题(“键入狗或猫:”);
dogOrCat.addValueChangeListener(valueChangeEvent->{
if(anists.contains(dogOrCat.getValue())){
dogOrCat.setComponentError(null);
}否则{
setComponentError(新用户错误(“哎呀!您键入的不是‘dog’或‘cat’”));
}
} );
//单选按钮对认为处于良好状态或错误状态的按钮。
按钮按钮=新按钮(“示例”);
按钮。添加ClickListener(e->{
Notification.show(“此按钮不起任何作用”,Notification.Type.HUMANIZED_MESSAGE);
} );
//单选按钮,用于控制上述按钮的良好或错误状态。
Listradiotems=List.of(“无错误”、“错误”);
RadioButtonGroup收音机=
新的RadioButtonGroup(“生成按钮:”);
无线电.设置项(放射性项);
radios.setValue(radiotems.get(0));//默认设置第一项(索引计数=0)。
radios.addValueChangeListener(valueChangeEvent->{
如果(radios.getValue().equals(radiotems.get(1)){//索引计数,那么`1`=第二个列表项“错误”。
setComponentError(新用户错误(“坏按钮”));
}否则{
button.setComponentError(null);
}
} );
//安排
最终垂直布局=新建垂直布局();
布局。添加组件(dogOrCat、按钮、收音机);
设置内容(布局);
}
@WebServlet(urlPatterns=“/*”,name=“MyUIServlet”,asyncSupported=true)
@VaadinServletConfiguration(ui=MyUI.class,productionMode=false)
公共静态类MyUIServlet扩展了VaadinServlet{
}
}
顺便说一下,这里是我的POM(POM.xml)文件,它是为Java10和各种库的最新版本更新的

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.basilbourque.example</groupId>
    <artifactId>set-comp-error</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>set-comp-error</name>

    <prerequisites>
        <maven>3</maven>
    </prerequisites>

    <properties>
        <vaadin.version>8.5.2</vaadin.version>
        <vaadin.plugin.version>8.5.2</vaadin.plugin.version>
        <jetty.plugin.version>9.4.12.v20180830</jetty.plugin.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <!-- If there are no local customizations, this can also be "fetch" or "cdn" -->
        <vaadin.widgetset.mode>local</vaadin.widgetset.mode>
    </properties>

    <repositories>
        <repository>
            <id>vaadin-addons</id>
            <url>http://maven.vaadin.com/vaadin-addons</url>
        </repository>
    </repositories>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-server</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-push</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-client-compiled</artifactId>
        </dependency>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-themes</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                    <!-- Exclude an unnecessary file generated by the GWT compiler. -->
                    <packagingExcludes>WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-maven-plugin</artifactId>
                <version>${vaadin.plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>update-theme</goal>
                            <goal>update-widgetset</goal>
                            <goal>compile</goal>
                            <!-- Comment out compile-theme goal to use on-the-fly theme compilation -->
                            <goal>compile-theme</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
                <!-- Clean up also any pre-compiled themes -->
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>src/main/webapp/VAADIN/themes</directory>
                            <includes>
                                <include>**/styles.css</include>
                                <include>**/styles.scss.cache</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>

            <!-- The Jetty plugin allows us to easily test the development build by
                running jetty:run on the command line. -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.plugin.version}</version>
                <configuration>
                    <scanIntervalSeconds>2</scanIntervalSeconds>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <!-- Vaadin pre-release repositories -->
            <id>vaadin-prerelease</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>

            <repositories>
                <repository>
                    <id>vaadin-prereleases</id>
                    <url>http://maven.vaadin.com/vaadin-prereleases</url>
                </repository>
                <repository>
                    <id>vaadin-snapshots</id>
                    <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <pluginRepository>
                    <id>vaadin-prereleases</id>
                    <url>http://maven.vaadin.com/vaadin-prereleases</url>
                </pluginRepository>
                <pluginRepository>
                    <id>vaadin-snapshots</id>
                    <url>https://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
                    <releases>
                        <enabled>false</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>

</project>


4.0.0
com.basilbourque.example
设置补偿误差
战争
1.0-快照
设置补偿误差
3.
8.5.2
8.5.2
9.4.12.v20180830
UTF-8
10
10
地方的
瓦丁插件
http://maven.vaadin.com/vaadin-addons
com.vaadin
瓦丁波姆
${vaadin.version}
聚甲醛
进口
javax.servlet
javax.servlet-api
3.1.0
假如
com.vaadin
瓦丁服务器
com.vaadin
瓦德