在Vaadin 8中调用没有粘合剂和bean的验证用户反馈

时间:2018-09-18 23:07:05

标签: validation vaadin vaadin8

在Vaadin 8中,内置的验证功能是通过将Binderdiscussed in the Manualshown in this Answer挂接到bean来建立的。我了解这是如何工作的,并且运作良好,我感谢Vaadin团队在那里的思想和努力。

但是……我只想要一个简单的快速脏数据输入字段,仅接受-1、0和1作为值。我喜欢Vaadin验证的功能,可以通过用红色和红色等标记字段/标题来拒绝错误的输入。

➥有什么方法可以调用对用户的验证样式的反馈,而无需费心定义bean和建立绑定器的所有麻烦?

作为一种解决方法,我可以将我的数据输入字段替换为弹出菜单(又称下拉列表)。但是,当我们需要用户快速简单的输入(例如在对话框中,而无需所有输入数据形式的仪式)时,我的问题仍然有用。

2 个答案:

答案 0 :(得分:1)

Binder不适用于单个字段。

Vaadin 8

使用Vaadin 8进行单字段验证的最佳选择是仅与字段ValueChangeListener挂钩,并执行值更改事件中所需的任何操作。

AbstractComponent::setComponentError(ErrorMessage componentError)

请注意,但是在Vaadin 8字段components中确实有setComponentError(..)方法。通过调用此方法,您用ValueChangeListenersetComponentError(…)进行的单字段验证具有与表单中的装订器类似的外观。这适用于文本字段,按钮等。

请参见手册的Handling Errors页。

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

Vaadin 7

Vaadin 7Vaadin 8之间存在区别。使用Vaadin 7,可以将验证器直接分配给字段。

答案 1 :(得分:0)

示例应用

Answer by Tatu Lund是正确的,可以解决问题。解决方案是致电AbstractComponent::setComponentError

这是一个完整的Vaadin 8.5.2小应用程序,可以将其显示出来。

此应用程序有两个小部件,一个TextField和一个Button,可以将其标记为正常状态…

enter image description here

...或者处于错误状态。

enter image description here

这是应用程序的整个.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 {
    }
}

顺便说一下,这是我针对Java 10和各种库的最新版本更新的POM(pom.xml)文件。

<?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>