OK, I've implemeted Scotts code but I'm still not getting any validation.

Reg.page

<component id="tcCheckbox" type="Checkbox">
        <binding name="value"       value="readTC"/>
        <binding name="validators"  value="validators:$cbValidator"/>
</component>

<bean name="cbValidator" class="com.azudio.tapestry.validator.RequiredCheckbox">
        <set name="message"  value="literal:Terms Accepted"/>
</bean>

I've put some debugging statements into the RequiredCheckbox class but it seems that when the page with the required checkbox is rendered the 'renderContribution' method never gets called, therefore no javascript gets output. Also when if the checkbox is left unchecked and the form submitted the 'validate' method also does not get called, only if its checked does 'validate' get called.


Thanks,
Adam


On 25 Jun 2006, at 13:27, Adam Henderson Azudio wrote:

Thanks Scott,
I'll give your code a try.

It might be worth sticking this up on the wiki as well.

Thanks again,

Adam

On 24 Jun 2006, at 16:55, Maura Wilder wrote:

no
----- Original Message ----- From: "Jesse Kuhnert" <[EMAIL PROTECTED]>
To: "Tapestry users" <users@tapestry.apache.org>
Sent: Saturday, June 24, 2006 11:05 AM
Subject: Re: Checkbox required validation not working


Looks good Scott! I just wanted to say that in the upcoming 4.1 release the required generated client side javascript handles any/all form input types
and so will produce meaningful results universally when applied.

On 6/24/06, Scott Russell <[EMAIL PROTECTED]> wrote:

Hi Adam,

The required validator generates client-side javascript that calls the
Tapestry.require_field function. This is the code of that function:

Tapestry.require_field = function(event, fieldId, message)
{
    var field = this.find(fieldId);

    if (field.value.length == 0)
      event.invalid_field(field, message);
}

Problem is, for a Checkbox field, the value of the checkbox is either true or false, so is thus never empty. Hence it always passes the require_field
test.

I wrote my own RequiredCheckbox validator as follows:

RequiredCheckbox.java

public class RequiredCheckbox extends Required {
    public void validate(IFormComponent field, ValidationMessages
messages, Object object)
            throws ValidatorException
    {
        super.validate(field, messages, object);
        if ((object == null)
                || (Boolean.class.isInstance(object) && !((Boolean)
object).booleanValue()))
        {
            String message = buildMessage(messages, field);
            throw new ValidatorException(message,
ValidationConstraint.REQUIRED);
        }
    }

    private String buildMessage(ValidationMessages messages,
IFormComponent field)
    {
        return messages.formatValidationMessage(
                getMessage(),
                ValidationStrings.REQUIRED_FIELD,
                new Object[]
                { field.getDisplayName() });
    }

public void renderContribution(IMarkupWriter writer, IRequestCycle
cycle,
FormComponentContributorContext context, IFormComponent field)
    {
context.registerForFocus (ValidationConstants.REQUIRED_FIELD);
        context.includeClasspathScript
("/com/myapp/validator/BooleanValidator.js");

        StringBuffer buffer = new StringBuffer("function(event) {
Tapestry.require_boolean_field(event, '");
        buffer.append(field.getClientId());
        buffer.append("', ");
        buffer.append(TapestryUtils.enquote(buildMessage(context,
field)));
        buffer.append("); }");

        context.addSubmitHandler(buffer.toString());
    }
}

BooleanValidator.js

Tapestry.require_boolean_field = function(event, fieldId, message)
{
    var field = this.find(fieldId);

    if (!field.checked)
        event.invalid_field(field, message);
}


Then you use it in your page file like this:

Home.page

    <component id="MustHaveCheckbox" type="Checkbox">
        <binding name="value"       value="termsAccepted"/>
<binding name="validators" value="validators: $cbValidator"/>
    </component>

<bean name="cbValidator" class="com.myapp.validator.RequiredCheckbox">
        <set name="message"  value="message:terms.required"/>
    </bean>


Hope this helps.

regards,
Scott

On Saturday 24 June 2006 22:22, Adam Henderson wrote:
> Hi All,
>
> I'm doing a simple form that has a checkbox which must be checked in
> order to submit the form:
>
> <form jwcid="[EMAIL PROTECTED]" clientValidationEnabled="ognl:true">
> T&C:<input jwcid="[EMAIL PROTECTED]" value="ognl:readTC"
> validators="validators:required"/>
> <input jwcid="@Submit" listener="listener:confirm" type="submit"
> name="Submit" value="Confirm"/>
> </form>
>
> but if I can still submit my form without the checkbox being checked,
> so it looks like no validation is taking place.
>
> When the page renders the js generated at the base of the html is:
> <script language="JavaScript" type="text/javascript"><!--
> Tapestry.register_form('myForm');
> Tapestry.set_focus('tandcCheckbox');
>
> // --></script>
>
> I'm obviously missing a trick but I can't see what it is?
>
> Thanks,
> Adam.




--
Jesse Kuhnert
Tacos/Tapestry, team member/developer

Open source based consulting work centered around
dojo/tapestry/tacos/hivemind.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to