[ 
http://jira.nuxeo.org/browse/WEB-61?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Bogdan Stefanescu updated WEB-61:
---------------------------------

    Attachment: test-forms.xml

A generic form validation mechanism was implemented.

You can see the test 
org.nuxeo.ecm.webengine.tests.forms.TestForms 
as an example on how to use it.

Roughly to validate a form you need to register a form validation template 
which is an extension point 
that looks like the one attached to this issue.

A form is uniquely identified by a string ID and may contains several fields 
definitions.
Each field definition may specify common constraints like isRequired, maxLength 
etc or custom ones
that can be constructed using complex logical expressions based on  AND, OR, 
NOT logical operators.

Apart OR, AND and NOT the following simple constraints are supported:
1. like  - match a regex pattern 
2. eq    - equals to a given value
3. lt       - less than a given value
5. lteq  - less than or equal a given value
6. gt     - greater than a given value
7. gteq - greater than or equal a given value
8. enum - equals to a  value from a given set of values

A field is identified by its ID used in the html form. It may also specify a 
label. The label is not yet  used but may be used in future (to generate simple 
forms for example)

If a field is not explicitly defined in a form then it will not have any 
constraint.

Simple constraints may also specify an error message to be used  in the case 
they doesn't validate a value.
The message is a template string that can take one argument -> the value that 
was tested.
Example:

          <constraints>
            <!-- example on how to setup an error message: the variable %s
            will be replaced with the actual value -->
            <eq ref="my:email" error-message="Confirmation E-mail address 
doesn't match: %s" />
          </constraints>

The form validation is  returning a State object which can be checked to see if 
whether or not the validation succeeded.
A state may also contains children states. 
For example if you are validation all fields in a form your result will be a 
multi state that will contains all the error states of the fields that failed 
to validate.
You can also do a single validation on a specified field. This can be useful 
when doing interactive validation filed by field.
Example of multiple field validation:

Form form = Engine.getFormManager().getForm("MyForm");
// the form is validated against a form instance (e.g. a form data) that was 
// submited by the user and that can be retrieved from the Context.
State state = form.validate(Context.getForm());
if (!state.isOk()) {
  Request.sendError("Failed to validate the form: "+state.getMessage());
}


Example of a single field validation:

Form form = Engine.getFormManager().getForm("MyForm");
Field field = form.getField("my_field");
State state = field.validate(Context.getForm());
if (!state.isOk()) {
  Request.sendError("Failed to validate the field: "+state.getMessage());
}

Also, the validation status can be converted to a JSON string so that you can 
easily use 
form validation with AJAX on the client side to implemented interactive 
validation.
Example:

Form form = Engine.getFormManager().getForm("MyForm");
Field field = form.getField("my_field");
State state = field.validate(Context.getForm());
if (!state.isOk()) {
   Request.setStatus(HttpServletResponse.SC_FORBIDDEN); // failed
   Request.getWriter().write(state.toJSONString());
} else {
   Request.setStatus(200); // success
}



> Form validation
> ---------------
>
>                 Key: WEB-61
>                 URL: http://jira.nuxeo.org/browse/WEB-61
>             Project: Nuxeo Web Engine
>          Issue Type: Task
>    Affects Versions: 1.0 Beta
>            Reporter: Thomas Roger
>            Assignee: Bogdan Stefanescu
>             Fix For: 1.0 RC
>
>         Attachments: test-forms.xml
>
>


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.nuxeo.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        
_______________________________________________
ECM-tickets mailing list
[email protected]
http://lists.nuxeo.com/mailman/listinfo/ecm-tickets

Reply via email to