customValidator without requiredValidator? - validation

I am struggling with a custom validator for a text field. It seems that the custom validation only works AFTER the required validation is executed. This means that a field without a requiredValidator cannot be custom validated - is that true?
What I want to do:
I have a text field. The value is only required if a specific value in another field is selected (here this is a checkbox group). It is a dependant validation. My custom validator works fine until the text field is required - but this should not be the case.
checkbox does not have the specific value -> text field can be blank
checkbox holds the value -> text field must have a value.
Any ideas?
<xp:inputText id="inputText1" disableClientSideValidation="true">
</xp:inputText>
<xp:inputText id="inputText2" disableClientSideValidation="true">
<xp:this.validators>
<xp:customValidator message="err">
<xp:this.validate><![CDATA[#{javascript:if(getComponentValue("inputText1").length>0 && getComponentValue("inputText2").length==0) postValidationError(this, "foo")}]]></xp:this.validate>
</xp:customValidator>
</xp:this.validators>
</xp:inputText>
Where getComponentValue is a method to receive either the value with getValue or getSubmittedValue from the component and postValidationError is a method to add a faces message.
EDIT & FINAL ANSWER
Conclusion and a sample here: http://mardou.dyndns.org/Privat/osnippets.nsf/id/OBUE-95BLK4

The required validator is always the first validator which will be executed during the validation. That means that the answer to the first part of your question is YES.
But this does not mean that you need a required validator to use a custom validator: This part of your question has to be answered with a clear NO.
The required validator is a special kind of "hack", because a validator is only executed if your component receives a new value (aka not blank).
I am not sure why you have a problem with a custom validator - in the scenario you are describing you are just using a required validator...
EDIT:
Just "turn your validators around": Add the custom validator from inputText2 to inputText1 and it should work.
EDIT 2:
<xp:inputText id="inputText1" disableClientSideValidation="true">
<xp:this.validators>
<xp:customValidator message="err">
<xp:this.validate><![CDATA[#{javascript:
var val = getComponent("inputText2").getSubmittedValue();
if( val.equals("") == true )
return false;
null}]]>
</xp:this.validate>
</xp:customValidator>
</xp:this.validators>
</xp:inputText>
<xp:inputText id="inputText2" disableClientSideValidation="true" />

Related

How to STOP JSF validation on p:calendar when it is not required field?

I have the following date field
<p:calendar id="sampleDate"
value="#{sampleController.sampleDate}"
navigator="true" showOn="button" pattern="MM/dd/yyyy" converterMessage="Sample date is not valid" placeholder="MM/DD/YYYY"/>
everything works as expected but this is not the required field but when i submit the page with wrong value in this field like 11/11 JSF validates and gives me error message Sample date is not valid
I have handled some validations for this date field in Controller .
So how do i stop this validation when the wrong value is given like 11/11 as this validation is taken care in Controller.
I need this validation because this field is not required field.

TYPO3 Fluid - How To Make f:form.textarea Mandatory

I have text fields like this that will turn red and won't let you submit the form automatically if I put the property required on "1":
<f:form.textfield required="1"
property="name"
class="lcapp-formwidth"/>
Now I'm searching for the same in a textarea...the property required doesn't work here anymore...what would be the "best practice" to make it a required field just like the textfield?
<f:form.textarea property="story"
rows="3"
cols="7"
class="lcapp-formwidth" />
It is true, the TextareaViewHelper does not support the required attribute as an argument, but you can add any attribute to a fluid generated tag by using the additionalAttributes argument.
E.g.:
<f:form.textarea property="story"
rows="3"
cols="7"
class="lcapp-formwidth"
additionalAttributes="{required: 'required'}" />
Notice how additionalAttributes expects an array notation, where the key is the name of the attribute.

onChange event does not fire if there is field level validation

Below is the source of a very simplified XPage. It has a radio button group with two choices and an onchange event that sets viewScope.vsCompanies depending on the value selected. Then there is a field called Title that I have made Required. If I click on the radio button it changes from Contract to Lease and back but the onchange event never fires. Instead I get a warning that the Title is required. I only want the validation to fire when the document is being submitted so the onchange works. Do I have to make every one of the validations conditional on the submit being pressed, which seems like a lot of additional work. I could set a viewScope when the submit button is pressed and make it only required if that viewScope is true.
Sorry missed adding the code ps clientsideValidation is disabled
<xp:this.data>
<xp:dominoDocument var="CLDoc"
databaseName="Client Apps\LGI\XPages\LGIContracts-Leases.nsf"
formName="frmCL">
</xp:dominoDocument>
</xp:this.data>
<xp:this.properties>
<xp:parameter name="xsp.client.validation" value="false" />
</xp:this.properties>
<xp:br></xp:br>
<xp:messages id="messages1"></xp:messages>
<xp:radioGroup id="radioGroup1" value="#{CLDoc.Type}">
<xp:selectItem itemLabel="Contract"></xp:selectItem>
<xp:selectItem itemLabel="Lease"></xp:selectItem>
<xp:eventHandler event="onchange" submit="true"
refreshMode="partial" refreshId="comboBox1">
<xp:this.action><![CDATA[#{javascript:if (CLDoc.getValue("Type") == "Contract"){
viewScope.vsCompanies = ["A","B","C"];
return;
break;
}else{
viewScope.vsCompanies = ["X","Y","Z"];
return;
break;
}}]]></xp:this.action>
</xp:eventHandler>
</xp:radioGroup>
Company
<xp:br></xp:br>
<xp:comboBox id="comboBox1" value="#{CLDoc.Company}">
<xp:selectItems>
<xp:this.value><![CDATA[#{javascript:viewScope.vsCompanies}]]></xp:this.value>
</xp:selectItems>
</xp:comboBox>
<xp:br></xp:br>
Title
<xp:br></xp:br>
<xp:inputText id="inputText1" style="width:392.0px" value="#{CLDoc.Title}"
required="true">
<xp:this.validators>
<xp:validateRequired message="Title is required"></xp:validateRequired>
</xp:this.validators>
</xp:inputText>
I believe if you go to the Events you are able to disable validators for that event.
[edit]
I found a duplicate question here. xpages validation on field having onChange script
Looks like the event handler has the following parameter
disableValidators="true"

Alfresco Share forms: how to define a custom validation-handler for a non-mandatory field?

I have already read the Alfresco documentation as well as the wiki, but still it does not get clear: what's the proper way to define a custom validation-handler for a certain field in an Alfresco Share form?
The problem is that the approach that I found everywhere in the forum always overrides the MANDATORY type, so the validation handler only gets called if the field is set to mandatory="true".
<config evaluator="aspect" condition="my:aspect">
<forms>
<form>
<field-visibility>
<show id="my:property" />
</field-visibility>
<appearance>
<field id="my:property" mandatory="true">
<constraint-handlers>
<constraint type="MANDATORY" validation-handler="Alfresco.forms.validation.myValidator" event="keyup" />
</constraint-handlers>
</field>
</appearance>
</form>
</forms>
</config>
But how to define a custom validation-handler for a field that is not mandatory? Or else, which is the correct type to choose/override? I tried type="LENGTH" instead, but this also does not get called when the user enters something in the form field and the field.
Update:
I have also tried Andreas' suggestion from the comment below, however, the validation-handler does not get called. This is my xml and function:
<field id="my:field" mandatory="false">
<constraint-handlers>
<constraint message-id="mandatory.field.empty.suffix" event="keyup,propertychange" validation-handler="Alfresco.forms.validation.metahead"/>
</constraint-handlers>
</field>
Validation handler function:
Alfresco.forms.validation.metahead = function FormEditMetadataWebWorkflowExt_metahead(field, args, event, form, silent, message)
{
console.log('metahead!');
return false;
}
The above validation function does not get called at any time, neither when I change the value of my field nor any other form field.
Next, I tried to register the validation handler manually as I read in the Alfresco forum somewhere:
YAHOO.Bubbling.fire("registerValidationHandler",{fieldId: 'fieldIdOfMyField',handler: Alfresco.forms.validation.metahead, when: "onkeyup"});
but this only works partly. It does call my validation handler function only when any other (mandatory) form field changes (which is normal since the entire form validation happens when a field changes), but not when I change the value of my own field ("my:field").
(And also, why would the validation handler need to be registered manually; I would think this is what the constraint configuration in xml is for.)
Just omit the type attribute, so it reads:
<constraint validation-handler="YourCompany.forms.validation.yourValidator" event="keyup" />
If your function
YourCompany.forms.validation.yourValidator = function (field, args, event, form, silent, message)
{...}
returns false, the message displayed resolves to the value of YourCompany.forms.validation.yourValidator.message from the message resources (default mapping).

Client side validation not working for hidden field in asp.net mvc 3

I have got a hidden field with a validation for it as below
#Html.HiddenFor(m => m.Rating)
#Html.ValidationMessageFor(m => m.Rating)
The Rating property has Range validator attribute applied with range being 1-5. This is put inside a form with a submit button.
I have then got following jquery that sets the value in hidden field on some user event (Basically user clicks on some stars to rate)
$(".star").click(function(){
$("#Rating").val(2);
});
Now if I submit the form without the user event that sets the hidden field, the validation works. The error messages is displayed properly and it works all client side.
Now, in this situation, if I click on stars, that invokes the above javascript a sets the hidden field, the validation error message would not go away. I can submit the form after the hidden variable has some valid value. But I'm expecting that the client side validation should work. (When the hidden variable has been set with some valid value, the validation error should go away)
Initially I thought, the jquery validation would be invoked on some special events so I tried raising click, change, keyup, blur and focusout events myself as below
$(".star").click(function(){
$("#Rating").val(2);
$("#Rating").change();
});
But this is still not working. The error messages once appeared, does not go away at all.
You can wrap your hidden field with a div put somewhere but still inside the <form>. Add css to kick it to outer space.
<div style="position:absolute; top:-9999px; left:-9999px">
<input id="Rating" type="hidden" name="rating" >
</div>
Then add the following label to where you want to show the error:
<label for="rating" class="error" style="display:none">I am an an error message, please modify me.</label>
Client-side validation ignores hidden fields. You can set the "ignore" option dynamically but just to get it to work I did the following directlyl in the .js file.
For now this should do the trick.
In my aspx...
<%: Html.HiddenFor(model => model.age, new { #class="formValidator" }) %>
In jquery.validate.js
ignore: ":hidden:not('.formValidator')",
This turned out to be a very interesting issue. the default "ignore" setting is ignores hidden fields. The field was hidden in a jQuery ui plug-in. I simply added a class called "includeCheckBox" to the rendered input I wanted to validate and put the following line of code in...
var validator = $('#formMyPita').validate();
validator.settings.ignore = ':hidden:not(".includeCheckBox")';
if ($('#formMyPita').valid()) {....
In the code which sets the hidden field's value, manually invoke validation for the form, like so:
$("form").validate().form();
I think it is because hidden inputs don't fire any of these events.
What you could do instead would be to use a <input type="text" style="display:none" /> instead of the hidden field;
#html.TextBoxFor(m => m.Rating, new {display = "display:none"})

Resources