/** * Collect all messages associated with components identified by <code>for</code> attribute and * return it. An empty list will be returned when there are no messages. * * @param context The involved faces context. * @param component The messages component. * @return All messages associated with components identified by <code>for</code> attribute. */ protected List<FacesMessage> getMessages(FacesContext context, OmniMessages component) { String forClientIds = component.getFor(); if (forClientIds == null) { return component.isGlobalOnly() ? context.getMessageList(null) : context.getMessageList(); } List<FacesMessage> messages = new ArrayList<>(); for (String forClientId : forClientIds.split("\\s+")) { UIComponent forComponent = component.findComponent(forClientId); if (forComponent == null) { continue; } messages.addAll(context.getMessageList(forComponent.getClientId(context))); if (!(forComponent instanceof UIInput)) { for (UIInput child : findComponentsInChildren(forComponent, UIInput.class)) { messages.addAll(context.getMessageList(child.getClientId(context))); } } } return messages; }
@Override protected void writeFormRow( FacesContext context, ResponseWriter w, FormLayout c, ComputedFormData formData, UIFormLayoutRow row) throws IOException { ComputedRowData rowData = createRowData(context, c, formData, row); UIInput edit = row.getForComponent(); if (edit != null) { // Write the error messages, if any if (!formData.isDisableRowError()) { Iterator<FacesMessage> msg = ((DominoFacesContext) context).getMessages(edit.getClientId(context)); if (msg.hasNext()) { while (msg.hasNext()) { FacesMessage m = msg.next(); writeFormRowError(context, w, c, row, edit, m, rowData); } } } } // The write the children writeFormRowData(context, w, c, formData, row, edit, rowData); }
public static Object getConvertedValue( final FacesContext context, final UIInput component, final Object submittedValue) { try { final Renderer renderer = getRenderer(context, component); if (renderer != null) { return renderer.getConvertedValue(context, component, submittedValue); } else if (submittedValue instanceof String) { return getConvertedUIOutputValue(context, component, submittedValue); } } catch (final ConverterException e) { final FacesMessage facesMessage = e.getFacesMessage(); if (facesMessage != null) { context.addMessage(component.getClientId(context), facesMessage); } else { final Object[] args = new Object[] {UIComponentUtil.getLabel(component)}; context.addMessage( component.getClientId(context), FacesMessageUtil.getMessage(context, UIInput.CONVERSION_MESSAGE_ID, args)); } component.setValid(false); } return submittedValue; }
private boolean verifyNumberIsAvailable() { Magazzino existing = em.find(Magazzino.class, newJar.getCodeJar()); if (existing != null) { messages .warn(new BundleKey("messages", "account_numberTaken")) .defaults("The number '{0}' is already taken. Please choose another number.") .targets(numberInput.getClientId()) .params(newJar.getCodeJar()); log.jarAvailable(existing.getCodeJar() + "", existing != null); return false; } return true; }
// This function is being overridden to provide style/class to tds...probably // should be bubbled up to the actual general FormTableRenderer @Override protected void writeFormRowData( FacesContext context, ResponseWriter w, FormLayout c, ComputedFormData formData, UIFormLayoutRow row, UIInput edit, ComputedRowData rowData) throws IOException { w.startElement("div", c); // $NON-NLS-1$ String fieldStyle = row.getStyle(); if (StringUtil.isEmpty(fieldStyle)) { fieldStyle = (String) getProperty(PROP_FIELDROWSTYLE); } if (StringUtil.isNotEmpty(fieldStyle)) { w.writeAttribute("style", fieldStyle, null); // $NON-NLS-1$ } String fieldClass = row.getStyleClass(); if (StringUtil.isEmpty(fieldClass)) { fieldClass = (String) getProperty(PROP_FIELDROWCLASS); } if (StringUtil.isNotEmpty(fieldClass)) { w.writeAttribute("class", fieldClass, null); // $NON-NLS-1$ } // TODO: I think this section is dead code. Look into that. boolean rowError = false; if (edit != null) { // Write the error messages, if any if (!formData.isDisableRowError()) { Iterator<FacesMessage> msg = ((DominoFacesContext) context).getMessages(edit.getClientId(context)); if (msg.hasNext()) { rowError = true; } } } boolean hasLabel = rowData.hasLabel(); boolean labelAbove = rowData.isLabelAbove(); // Write the label w.startElement("div", c); // $NON-NLS-1$ String tdStyle = (String) getProperty(PROP_FIELDCOLUMNSTYLE); if (StringUtil.isNotEmpty(tdStyle)) { w.writeAttribute("style", tdStyle, null); // $NON-NLS-1$ } // if we have an error, assign the appropriate classes String tdClass = hasLabel ? (String) getProperty(PROP_FIELDCOLUMNSTYLECLASS) : (String) getProperty(PROP_FIELDEDITNOLABELCLASS); tdClass = (rowError) ? ExtLibUtil.concatStyleClasses( (String) getProperty(PROP_ERRORROWTITLESTYLECLASS), tdClass) : tdClass; if ((TypedUtil.getChildren(row).get(0) instanceof XspInputTextarea || TypedUtil.getChildren(row).get(0) instanceof UIDojoTextarea) && getProperty(PROP_TEXTAREALABELCLASS) != null) { // if our row contains a textarea component, we need a special label class tdClass = ExtLibUtil.concatStyleClasses(tdClass, (String) getProperty(PROP_TEXTAREALABELCLASS)); } if (StringUtil.isNotEmpty(tdClass)) { w.writeAttribute("class", tdClass, null); // $NON-NLS-1$ } if (hasLabel) { String lblStyle = (String) getProperty(PROP_FIELDLABELSTYLE); String lblClass = (String) getProperty(PROP_FIELDLABELCLASS); if (labelAbove) { w.startElement("div", c); // $NON-NLS-1$ } else { String width = row.getLabelWidth(); if (StringUtil.isEmpty(width)) { width = formData.getLabelWidth(); } if (StringUtil.isEmpty(width)) { width = (String) getProperty(PROP_FIELDLABELWIDTH); } if (StringUtil.isNotEmpty(width)) { lblStyle = ExtLibUtil.concatStyles( "width:" + width, (String) getProperty(PROP_FIELDLABELSTYLE)); // $NON-NLS-1$ } } if (StringUtil.isNotEmpty(lblStyle)) { w.writeAttribute("style", lblStyle, null); // $NON-NLS-1$ } if (StringUtil.isNotEmpty(lblClass)) { w.writeAttribute("class", lblClass, null); // $NON-NLS-1$ } String label = row.getLabel(); writeFormRowLabel(context, w, c, formData, row, edit, label); if (labelAbove) { if (c.isFieldHelp()) { writeFormRowHelp(context, w, c, row, edit); } w.endElement("div"); // $NON-NLS-1$ w.startElement("div", c); // $NON-NLS-1$ } else { w.endElement("div"); // $NON-NLS-1$ w.startElement("div", c); // $NON-NLS-1$ } } String editStyle = (hasLabel && !labelAbove) ? ExtLibUtil.concatStyles( (String) getProperty(PROP_FORMROWEDITSTYLE), (String) getProperty(PROP_FIELDEDITSTYLE)) : (String) getProperty(PROP_FIELDEDITSTYLE); if (StringUtil.isNotEmpty(editStyle)) { w.writeAttribute("style", editStyle, null); // $NON-NLS-1$ } String editClass = (String) getProperty(PROP_FIELDEDITCLASS); if (TypedUtil.getChildren(row).get(0) instanceof XspInputTextarea || TypedUtil.getChildren(row).get(0) instanceof UIDojoTextarea) { editClass = ExtLibUtil.concatStyleClasses(editClass, (String) getProperty(PROP_TEXTAREASTYLECLASS)); } if (StringUtil.isNotEmpty(editClass)) { w.writeAttribute("class", editClass, null); // $NON-NLS-1$ } writeFormRowDataField(context, w, c, row, edit); if (hasLabel) { if (labelAbove) { w.endElement("div"); // $NON-NLS-1$ } // Write the help if (hasLabel) { if (!labelAbove) { if (c.isFieldHelp()) { w.startElement("div", c); // $NON-NLS-1$ writeFormRowHelp(context, w, c, row, edit); w.endElement("div"); // $NON-NLS-1$ } } } } w.endElement("div"); // $NON-NLS-1$ w.endElement("div"); // $NON-NLS-1$ }
// === continue code from InputRendererUtil === // ClientSide validation // com.ibm.xsp.renderkit.html_basic.InputRendererUtil.generateClientSideValidation(FacesContext, // UIInput, ResponseWriter) private void generateClientSideValidation( FacesContext context, UIInput uiInput, ResponseWriter writer) throws IOException { // Check if the input field is required boolean required = uiInput.isRequired(); Converter c = InputRendererUtil.findConverter(context, uiInput); Validator[] v = uiInput.getValidators(); // Check if it might make sense to generate a function boolean validate = required; if (!validate) { validate = c instanceof ClientSideConverter; } if (!validate) { for (int i = 0; i < v.length; i++) { validate = v[i] instanceof ClientSideValidator; if (validate) { break; } } } if (validate) { // This flag is maintained if we actually need to generate the function // Some converter/validator may not actually generate any client code, depending on their // parameters validate = false; StringBuilder b = new StringBuilder(128); b.append("XSP.attachValidator("); // $NON-NLS-1$ JavaScriptUtil.addString(b, uiInput.getClientId(context)); // Add the required flag if (required) { b.append(",new XSP.RequiredValidator("); // $NON-NLS-1$ JavaScriptUtil.addMessage(b, InputRendererUtil.getRequiredMessage(context, uiInput)); b.append(")"); validate = true; } else { b.append(",null"); // $NON-NLS-1$ } // Add the converter if (c instanceof ClientSideConverter) { ClientSideConverter clientSideConverter = (ClientSideConverter) c; // TODO this is handling of converterRenderer // differs from the original implementation in InputRendererUtil. String s; Renderer renderer = FacesUtil.getRenderer(context, uiInput.getFamily(), uiInput.getRendererType()); ClientSideConverter converterRenderer = (ClientSideConverter) FacesUtil.getRendererAs(renderer, ClientSideConverter.class); if (null != converterRenderer) { // if the control renderer implements ClientSideConverter // delegate to the renderer instead of to the converter. s = converterRenderer.generateClientSideConverter(context, uiInput); } else { s = clientSideConverter.generateClientSideConverter(context, uiInput); } if (StringUtil.isNotEmpty(s)) { b.append(","); b.append(s); // not JSUtil because contains client script. validate = true; } else { b.append(",null"); // $NON-NLS-1$ } } else { b.append(",null"); // $NON-NLS-1$ } // And add the validator for (int i = 0; i < v.length; i++) { if (v[i] instanceof ClientSideValidator) { String s = ((ClientSideValidator) v[i]).generateClientSideValidation(context, uiInput); if (StringUtil.isNotEmpty(s)) { b.append(","); b.append(s); // not JSUtil because contains client script. validate = true; } } } // Finally, check for multiple values String multiSep = null; if (uiInput instanceof UIInputEx) { multiSep = ((UIInputEx) uiInput).getMultipleSeparator(); } if (c instanceof ListConverter) { multiSep = ((ListConverter) c).getDelimiter(); if (StringUtil.isEmpty(multiSep)) { multiSep = ","; // $NON-NLS-1$ } } if (StringUtil.isNotEmpty(multiSep)) { b.append(","); JSUtil.addString(b, multiSep); } b.append(");"); // get the scriptcollector component (needed to add script blocks the the rendered output). if (validate) { JavaScriptUtil.addScriptOnLoad(b.toString()); } } }
private void writeTag( FacesContext context, UIInput component, ResponseWriter writer, String currentValue) throws IOException { writer.startElement("input", component); // $NON-NLS-1$ boolean isUsingInputPlainText = false; int typeInt = getValueType(component); String type; if (typeInt == TYPE_DATE) { type = "date"; // $NON-NLS-1$ } else if (typeInt == TYPE_TIME) { type = "time"; // $NON-NLS-1$ } else { isUsingInputPlainText = isUsingInputPlainText(context, component); if (isUsingInputPlainText) { type = "text"; // $NON-NLS-1$ } else { type = "datetime-local"; // $NON-NLS-1$ // type = "datetime"; //$NON-NLS-1$ } } writer.writeAttribute("type", type, null); // $NON-NLS-1$ // Write the actual value // For an input tag, it is passed as a parameter if (StringUtil.isNotEmpty(currentValue)) { writer.writeAttribute("value", currentValue, "value"); // $NON-NLS-1$ //$NON-NLS-2$ } // id & name use clientID String clientId = component.getClientId(context); // id // only write valid user defined ids String id = component.getId(); if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)) { writer.writeAttribute("id", clientId, "id"); // $NON-NLS-1$ //$NON-NLS-2$ } // name writer.writeAttribute("name", clientId, "clientId"); // $NON-NLS-1$ //$NON-NLS-2$ writeTagHtmlAttributes(context, component, writer, currentValue); writeTagEventAttributes(context, component, writer, currentValue); if (component instanceof FacesAttrsObject) { FacesAttrsObject attrsHolder = (FacesAttrsObject) component; AttrsUtil.encodeAttrs(context, writer, attrsHolder); } if (isUsingInputPlainText) { boolean placeholderAlreadyDecided = false; if (component instanceof FacesAttrsObject) { FacesAttrsObject attrsHolder = (FacesAttrsObject) component; List<Attr> attrs = attrsHolder.getAttrs(); if (null != attrs) { for (Attr attr : attrs) { if ("placeholder".equals(attr.getName())) { // $NON-NLS-1$ placeholderAlreadyDecided = true; } } } } if (!placeholderAlreadyDecided) { // not using the native dateTime picker, so need some hint to the user // as to the date format they should use, so providing the current date in the correct // format // in a placeholder (value that disappears onfocus). // placeholder="2014-01-13T15:35" Date currentDateTime = new Date(); String placeholderValue = getAsString( context, component, (DateTimeConverter) component.getConverter(), currentDateTime); writer.writeAttribute("placeholder", placeholderValue, null); // $NON-NLS-1$ } } writer.endElement("input"); // $NON-NLS-1$ }
// validates the form to find any errors regarding the user input // "never trust any user" public void validate(ComponentSystemEvent event) { UIForm form = (UIForm) event.getComponent(); UIInput pw1 = (UIInput) form.findComponent("password1"); UIInput pw2 = (UIInput) form.findComponent("password2"); UIInput zipCode = (UIInput) form.findComponent("zip"); UIInput accountNo = (UIInput) form.findComponent("accountNo"); UIInput bankCode = (UIInput) form.findComponent("bankCode"); // check if a password has been entered if (pw1 == null || pw1.getValue() == null) { FacesContext fc = FacesContext.getCurrentInstance(); fc.renderResponse(); return; } // check further that both passwords are equal // if not, a message should be shown to the user telling him // that both passwords do not match if (!(pw1.getValue().equals(pw2.getValue()))) { pw1.setValue(""); pw2.setValue(""); FacesContext fc = FacesContext.getCurrentInstance(); ResourceBundle bundle = fc.getApplication().getResourceBundle(fc, "m"); message.setText( bundle.getString("register_passwordDifferent"), bundle.getString("register_passwordDifferentDetail"), FacesMessage.SEVERITY_ERROR, pw1.getClientId()); fc.renderResponse(); } String zip = (String) zipCode.getValue(); if (zip.length() > 8 || zip.length() < 4) { zipCode.setValue(""); FacesContext fc = FacesContext.getCurrentInstance(); ResourceBundle bundle = fc.getApplication().getResourceBundle(fc, "m"); message.setText( bundle.getString("register_zipError"), null, FacesMessage.SEVERITY_ERROR, zipCode.getClientId()); fc.renderResponse(); } String accountNumber = (String) accountNo.getValue(); if (accountNumber.length() > 12 || accountNumber.length() < 5) { accountNo.setValue(""); FacesContext fc = FacesContext.getCurrentInstance(); ResourceBundle bundle = fc.getApplication().getResourceBundle(fc, "m"); message.setText( bundle.getString("register_accountNoError"), null, FacesMessage.SEVERITY_ERROR, accountNo.getClientId()); fc.renderResponse(); } String bankcode = (String) bankCode.getValue(); if (bankcode.length() > 7 || bankcode.length() < 5) { bankCode.setValue(""); FacesContext fc = FacesContext.getCurrentInstance(); ResourceBundle bundle = fc.getApplication().getResourceBundle(fc, "m"); message.setText( bundle.getString("register_bankCodeError"), null, FacesMessage.SEVERITY_ERROR, bankCode.getClientId()); fc.renderResponse(); } }