You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Kamal Bhatt <kb...@tt.com.au> on 2006/07/20 09:00:02 UTC

Adding a validator at runtime

Hi
This is a follow on from my previous email. I have decided that would 
like to create a custom validator when a certain condition is met. In 
this case, the validation checks if a value in is equal to an existing 
field.

Here is a snippet of my flow script:

    var validator = new 
Packages.creative.validation.ParentRegionsValidation();

    /* Keep showing form until it is validated and saved correctly to 
the database */
    while (true)
    {
        try
        {
            form.showForm("call-display-pipeline.jx", screenData);

            var idx = 0;
            var parentregions = form.getChild("parentregions");

            while (parentregions != null && idx < parentregions.getSize())
            {
                cocoon.log.error("removing validator");
                
parentregions.getRow(idx).getChild("id").removeValidator(validator);
                idx = idx + 1;
            }
 
            if (!isTopMost)
            {
                idx = 0;
                while (parentregions != null && idx < 
parentregions.getSize())
                {
                    cocoon.log.error("adding validator");
                    
parentregions.getRow(idx).getChild("id").addValidator(validator);
                    if 
(parentregions.getRow(idx).getChild("id").removeValidator(validator))
                    {
                        cocoon.log.error("KamBha: Removed validator");
                    }
                    else
                    {  
                        cocoon.log.error("KamBha: Validator was never 
added");
                    }

                    
parentregions.getRow(idx).getChild("id").addValidator(validator);
                    idx = idx + 1;
                }
            }
        }
        catch(e)
        {
            return;
        }
I know that I have got the field I want to attach the validator to as I 
had code to check the value of the widget, and it was what I expected. I 
found that the validator does get added. If I move the showForm to after 
I add the validator I get the following error:

2059002 2006-07-20 16:48:08,919 [http-8080-Processor25] ERROR 
sitemap.handled-errors.prepareErrorHandler():165  - <map:call function> 
did not send a response, at 
file:/opt/lenya/jakarta-tomcat-5.0.28/webapps/cms/lenya/pubs/ch/usecase-region.xmap:138:35
org.apache.cocoon.ProcessingException: <map:call function> did not send 
a response.

The following is my validator.

package creative.validation;

import java.lang.String;

import org.apache.avalon.framework.context.Context;
import org.apache.avalon.framework.context.ContextException;
import org.apache.avalon.framework.context.Contextualizable;
import org.apache.avalon.framework.logger.LogEnabled;
import org.apache.avalon.framework.logger.Logger;
import org.apache.cocoon.forms.formmodel.Widget;
import org.apache.cocoon.forms.validation.ValidationError;
import org.apache.cocoon.forms.validation.ValidationErrorAware;
import org.apache.cocoon.forms.validation.WidgetValidator;

public class ParentRegionsValidation implements WidgetValidator, 
LogEnabled, Contextualizable
{
    private Logger logger = null;
    private Context context = null;

    public boolean validate(Widget widget)
    {
        String regionId = (String) 
widget.getForm().getChild("region_id").getValue();
        System.out.println("In validate");
        logger.error("In validate");


        if (regionId.equals((String)widget.getValue()))
        {
            if (widget instanceof ValidationErrorAware)
            {
                System.out.println("validation error");
                logger.error("Validation error");
                ((ValidationErrorAware)widget).setValidationError(new 
ValidationError("Current region cannot be parent of itself"));
                return(false);
            }
        }
        System.out.println("No validation error");
        logger.error("No validation error");
        return(true);
    }

    public void enableLogging(Logger logger)
    {
        this.logger = logger;
    }

    public void contextualize(Context context) throws ContextException
    {
        this.context = context;
    }
}

None of the print statements in the validator gets called when it is 
working. I guess I understand why it wouldn't work when 
call-display-pipeline is called before adding the validator, but I don't 
understand why it would give me an error when I move it to the end. I am 
using cocoon 2.1.7. If you need further code, I will give it. Any help 
is appreciated.

Thanks

-- 
Kamal Bhatt


--
Disclaimer: This email is confidential and may contain privileged information for the sole use of the person or business to which it is addressed. If you are not the intended recipient, please notify the sender by return e-mail or phone as you must not view, disseminate, distribute or copy this email without our consent. We do not accept any liability in connection with any computer virus, data corruption, incompleteness, or unauthorised amendment of this email. It is the sole responsibility of the receiver to scan for viruses before opening.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org