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.