Skip to content

Commit

Permalink
Added Easter egg ajax validation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
rafabene authored and sgilda committed Jan 16, 2013
1 parent b839488 commit 5c1ea6e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 19 deletions.
3 changes: 2 additions & 1 deletion kitchensink-deltaspike/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Changes compared to the original `kitchensink` quickstart
* The transaction type in the persistence unit configuration file (`src/main/resources/META-INF/persistence.xml`) has been changed to `RESOURCE_LOCAL`.
* The DeltaSpike `TransactionalInterceptor` has been added to the beans.xml CDI configuration file (`src/main/webapp/WEB-INF/beans.xml`).
* The DeltaSpike dependencies have been added to the project POM.
* The ShrinkWrap `shrinkwrap-resolver-bom` dependency has been added to the project POM, to be able to build the archive for the Arquillian test.
* The ShrinkWrap `shrinkwrap-resolver-bom` dependency has been added to the project POM, to be able to build the archive for the Arquillian test.
* The JSF components has an Ajax Validation feature that can be enabled/disabled on DeltaSpike configuration file (`src/main/resources/META-INF/apache-deltaspike.properties`).

System requirements
-------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public class MemberResourceRESTService {
private MemberRepository repository;

@Inject
MemberRegistration registration;

private MemberRegistration registration;
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Member> listAllMembers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

import org.apache.deltaspike.core.api.config.annotation.ConfigProperty;

/**
* This class uses CDI to alias Java EE resources, such as the persistence
* context, to CDI beans
*
* This class uses CDI to alias Java EE resources, such as the persistence context, to CDI beans
*
* <p>
* Example injection on a managed bean field:
* </p>
*
*
* <pre>
* &#064;Inject
* private EntityManager em;
Expand All @@ -43,13 +46,26 @@
public class Resources {

/**
* We are using a non JTA managed persistence context, so we cannot inject
* an EntityManager with &#064;PersistenceContext, as this would try to
* inject a container managed EntityManager.
* We are using a non JTA managed persistence context, so we cannot inject an EntityManager with &#064;PersistenceContext,
* as this would try to inject a container managed EntityManager.
*/
@PersistenceUnit
private EntityManagerFactory entityManagerFactory;

/**
* Inject a DeltaSpike configuration that can be configured using:
*
* <ul>
* <li>System properties</li>
* <li>Environment properties</li>
* <li>JNDI values</li>
* <li>Properties file values (apache-deltaspike.properties)</li>
* </ul>
*/
@Inject
@ConfigProperty(name = "jsf.ajax.validation.enabled", defaultValue = "true")
private Boolean ajaxValidationEnabled;

@Produces
@RequestScoped
protected EntityManager createEntityManager() {
Expand All @@ -64,8 +80,7 @@ protected void closeEntityManager(@Disposes EntityManager entityManager) {

@Produces
public Logger produceLog(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass()
.getName());
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}

@Produces
Expand All @@ -74,4 +89,11 @@ public FacesContext produceFacesContext() {
return FacesContext.getCurrentInstance();
}

@Produces
@Named
public Boolean getAjaxValidationDisabled() {
// Here we invert the value since <f:ajax/> tag queries for 'disabled'.
return !ajaxValidationEnabled;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# JBoss, Home of Professional Open Source
# Copyright 2012, Red Hat, Inc. and/or its affiliates, and individual
# contributors by the @authors tag. See the copyright.txt in the
# distribution for a full listing of individual contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

jsf.ajax.validation.enabled=true
19 changes: 12 additions & 7 deletions kitchensink-deltaspike/src/main/webapp/index.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,22 @@
model class.</p>
<h:panelGrid columns="3" columnClasses="titleCell">
<h:outputLabel for="name" value="Name:" />
<h:inputText id="name" value="#{newMember.name}" />
<h:message for="name" errorClass="invalid" />
<h:inputText id="name" value="#{newMember.name}" >
<f:ajax event="blur" render="nameMessage" disabled="#{ajaxValidationDisabled}"/>
</h:inputText>
<h:message id="nameMessage" for="name" errorClass="invalid" />

<h:outputLabel for="email" value="Email:" />
<h:inputText id="email" value="#{newMember.email}" />
<h:message for="email" errorClass="invalid" />
<h:inputText id="email" value="#{newMember.email}" >
<f:ajax event="blur" render="emailMessage" disabled="#{ajaxValidationDisabled}"/>
</h:inputText>
<h:message id="emailMessage" for="email" errorClass="invalid" />

<h:outputLabel for="phoneNumber" value="Phone #:" />
<h:inputText id="phoneNumber"
value="#{newMember.phoneNumber}" />
<h:message for="phoneNumber" errorClass="invalid" />
<h:inputText id="phoneNumber" value="#{newMember.phoneNumber}" >
<f:ajax event="blur" render="phoneMessage" disabled="#{ajaxValidationDisabled}"/>
</h:inputText>
<h:message id="phoneMessage" for="phoneNumber" errorClass="invalid" />
</h:panelGrid>

<p>
Expand Down

0 comments on commit 5c1ea6e

Please sign in to comment.