xpages validate one field if a checkbox is selected - validation

I only want to require a field if the user has not checked a check box elsewhere on the form.
The checkbox's value is "Yes" if checked or "No" if not checked.
On the Edit Box's validation formula I have the computed SSJS:
getComponentValue("SoleSource") == "No"
the function is stored in as a JS source
function getComponentValue(id){
var field = getComponent(id);
var value = field.getSubmittedValue();
if( null == value ){
// else not yet submitted
value = field.getValue();
}
return value;
}
No matter if the checkbox (with the ID "SoleSource") is checked or not, it always returns "No", which is the default value, and attempts to validate my field.
Is there any way to do this without submitting the form first?

You can get access to the field value only when the value is send to the server. I would suggest a two fold approach. One: use a client side JavaScript to check (for the comfort of the user) and a server side on complete submission (for data integrity).

As stwissel already said there are two ways to validate your inputField, clientSide and on ServerSide. To manipulate the validation depending on user Input i would use ClientSide javascript code to make it feal more 'On the fly' and then validate the whole document on Submit again on the Server Side.
Here a short example how i would build this on ClientSide with dojo. But please dont forget to validate the data again on the Server when the user saves/submits it to avoid corrupted data because of manipulation of your ClientSide javascript (or someone just turns of javascript in his browser).
<xe:djCheckBox id="select" value="#{viewScope.check}"
checkedValue="yes" uncheckedValue="no">
<xp:this.dojoAttributes>
<xp:dojoAttribute name="onChange">
<xp:this.value><![CDATA[#{javascript:return "var input = dijit.byId('"+getClientId('required')+ "'); input.required = !input.required;"}]]></xp:this.value>
</xp:dojoAttribute>
</xp:this.dojoAttributes>
</xe:djCheckBox>
<xp:inputText id="required" dojoType="dijit.form.ValidationTextBox">
<xp:this.dojoAttributes>
<xp:dojoAttribute name="required"
value="true">
</xp:dojoAttribute>
</xp:this.dojoAttributes>
</xp:inputText>
In this example i use dojo to convert my inputField to a dijit.form.ValidationTextBox wich allowes me to change it's required propertie. DojoValidation.

Related

parsley.js prevents submit after failing ajax validation has been corrected

i have a form where i need to see if an email address entered into the form is already in a database. this check needs to be performed conditionally based on the value of another field in the form.
here is the form field:
<input type="email" value="" class="form-control" name="email_bill" id="email" required data-parsley-type="email" data-parsley-registered="1" data-parsley-trigger="focusout submit">
and here's the validator code:
Parsley.addValidator('registered', {
validateString: function(value) {
if ($('input[name="d_type"]:checked').val() == 'S') {
return $.ajax({
type: "POST",
url: "/is_registered.html",
data: $('form').serialize()
});
} else {
var parent = $('#email').closest('div');
var this_id = $('#email').attr('data-parsley-id');
$(parent).removeClass('has-error').addClass('parsley-success');
$(this_id).remove();
return true;
}
},
messages: {en: "Someone has already registered with the Email address you entered"}
});
the server code is trivial and returns a '200 OK' header if the address isn't in the database and a '404 Not Found' if it is. that works.
i followed the example in the parsley.js custom validator example for the simple ajax request.
what happens is: if i enter a 'registered' address, i get the appropriate error message. if i then go and modify that address to one i know is NOT registered and tab or mouse out to the next field, the error message goes away, BUT the submit button doesn't work. to further complicate the situation, if i load and fill out a form with a 'non-registered' address, the submit button doesn't work either. it appears that execution of the custom validator disables submit upon entry.
i've played with this for hours, trying all sorts of event manipulation, but nothing works.
i should point out that if the checked value of d_type (see field definition above) is NOT 'S', then everything works as expected.
i am totally baffled as to why following the documentation results in failure.
as it turns out, this was not a problem with parsley at all.
a colleague created the form and the submit button had an id of "submit' which could not allow parsley to resolve the submit handler. under those circumstances, apparently parsley blocks the submit.
who knew?
well now, we know.....

Async validate single redux form field, but not on submit

Question
How can one build a async validation on a single form field that also can trigger filed level errors, but without using a asyncValidation that also triggers onSubmit?
Background to question
I have a Redux form where the user can choose to:
a) Input adress details by hand
b) Fill in their social security number and fetch all details from a API and get the redux form fields prefilled (wohoo, time saved!)
c) Being 100% anonymous by checking a checkbox which removed all personal details fields.
Given this kind of setup a asyncValidator that triggers onSubmit and onBlur seems tricky, i don't want them to trigger more than once if a get the desired details and the form get pre-filled. But if user submits a invalid social security number I want to trigger a field error.
if you just want to validate a single field, you can pass a prop validate
with an array of validation functions, that will return false if its valid, and a string error if invalid based on your logic. and will prevent submission, or will show an error on submittion.
const required = (value) => (value ? false : 'Required')
const isSSN = (value) => (willBeTrueIfSSN ? false : 'Invalid SSN')
<Field name="ssn" validate={[isSSN, required]} {...rest} />
some good examples in the redux-form docs:
https://redux-form.com/7.2.3/examples/fieldlevelvalidation/

Download raw data in Joomla component

I have written a Joomla component which is working well. I now want to add a button to an admin list view that when clicked automatically starts a CSV download of only the items selected in the list.
I'm OK with the model logic, the problem I've got is passing the selected cids or presenting raw output without the template.
If I use JToolBar's appendButton function to add a 'Link' type button, then I can send the user to a URL with 'format=raw', but I can't send information about which items were checked.
If I use JToolBarHelper::custom to add a custom list button, then I can send the information about which buttons were checked, but I can't send format=raw
As far as I can see there are two solutions, but I don't know how to implement either of them. Option one would be to force templateless raw output without a URL parameter of format=raw. Option two would be to set a hidden variable with format=raw in the admin form.
Any help would be appreciated
I've solved this as follows:
I added this hidden field to the admin form
<input type="hidden" name="format" value="html" />
Then subclassed JToolbarButtonStandard overriding the _getCommand with
protected function _getCommand($name,$task,$list)
{
JHtml::_('behavior.framework');
$message = JText::_('JLIB_HTML_PLEASE_MAKE_A_SELECTION_FROM_THE_LIST');
$message = addslashes($message);
if ($list)
{
$cmd = "if (document.adminForm.boxchecked.value==0){alert('$message');}else{document.getElementById('adminForm').format.value='raw'; Joomla.submitbutton('$task')}";
}
else
{
$cmd = "document.getElementById('adminForm').format.value='raw'; Joomla.submitbutton('$task')";
}
return $cmd;
}
So that when the button was clicked it changed the format parameter from html to raw.
I'm not going to mark this as solved in case anyone has any better ideas

How do I auto fill field values in a section of a form that is loaded via ajax in Laravel 4?

I have a section of a form that dynamically loads different sets of fields based on the user's selection in a control. I'm using a javascript event handler to detect when the selection changes, and using AJAX (with HTML payload) to pull in the proper set of fields.
I would like to be able to use Laravel's Form::getValueAttribute() method to automatically fill in the form fields' values in both the static and dynamic form parts. However, the partial view that is loaded by my AJAX call does not have the same instance of the Form class as the view with my main Form, so I can't simply call getValueAttribute() in the partial.
My thought is to make the AJAX call a POST, and serialize the necessary data (a subset of Input::old() or the model data depending whether the page is loaded as the result of validation errors, or an UPDATE request) to send along with the POST so that the HTML fragment I get back has the values set properly.
Is this the best way to get what I want? If so, does Laravel have any tools to help with the serialization of form data? If not, what might be a better approach?
I've found an approach I like better. When the view is loaded normally I use AJAX as usual to load the partial. But when the view is loaded for a validation post-back or for editing, I use Laravel's Views' nest method to nest the partial view containing the proper fields directly into the response. The partial then has access to all the Input and error data I need. The user is still able to change the field set as usual but I put up a confirm prompt for them if they have already set some values in a field set they previously selected. If they decide to proceed anyway, the field set is cleared and a new field set is brought in via AJAX as usual.
My code looks something like this:
Controller:
public function newThing() {
if ( Request::session()->has('errors') ) {
// this is a validation post-back
return View::make('thing')
->nest('fields', 'fields_partial');
} else {
// just a normal unfilled form
return View::make('thing');
}
}
public function editThing() {
return View::make('thing')
->nest('fields', 'fields_partial');
}
View: thing.blade.php (just a snip of it)
...
<form>
...
<select id="picker">...</select>
<div class="sub-fields">
{{ isset($fields) ? $fields : '' }}
</div>
...
</form>
...
<script>
$('#picker').change(function() {
// if any .sub-fields inputs have been changed, get confirmation from the user
// if user confirms, do ajax stuff to replace .sub-fields contents with new field set
// otherwise cancel the change
});
</script>

knock out observable for telerik radcaptcha

I am trying out with knockout validation.
I understand that i am able to use observable for inputs, i.e
<asp:TextBox ID="TxtName" runat="server" data-bind="value:Name"></asp:TextBox>
var self = this;
self.Name = ko.observable().extend({ required: { message: "Please enter your name." } });
but what if i am going to do a validation for a telerik radcaptcha
<telerik:RadCaptcha ID="RadCaptcha" runat="server"></telerik:RadCaptcha>
Am i able to do an observable to check if the radcaptcha is valid and display a message if its not?
For security reasons it is not possible to perform a client-side check on the Telerik RadCaptcha control. Think about it, if client side validation was enabled it would be possible to create a program to bombard the input with responses until the correct one was found.
See Telerik Forum Posts:
http://www.telerik.com/community/forums/aspnet-ajax/captcha/rad-captcha-client-side-validation.aspx
http://www.telerik.com/community/forums/aspnet-ajax/captcha/client-side-support-needed-for-radcaptcha.aspx

Resources