jQuery-Validation-Engine required only if value of another field is x or y - jquery-validation-engine

"requiredInFunction": {
"regex": "none",
"func": function(field, rules, i, options){
if (field.val() === '' && $('#CartItem_vision').val()==="Progressive") {
return false;
}
if (field.val() === '' && $('#CartItem_vision').val()==="Bifocal")
{
return false;
}
else
{
return true;
}
},
"alertText": "Addition is Required"
},
validate[custom[requiredInFunction]]
This validation does not do anything.
unless I put validate[required, custom[requiredInFunction]], but it becomes required even if the CartItem_vision is not "Progressive" or "Bifocal". I want them to be only required if value is "Progressive" or "Bifocal".

In your function you can add
rules.push('required');
Like this :
function checkCell(field, rules, i, options){
if ($('#cellNum').val().length > 0 && field.val().length == 0) {
rules.push('required');
return "Provider necessary with phone number";
}
}
This means that if #cellNum is populated then the provider is necessary.
The initialization is :
<select name="provider" id="provider" class="validate[funcCall[checkCell]">

Related

Redirection to a page for new customers without an active product WHMCS

Good morning,
I made a hook to try to redirect new customers and those who no longer have an active product among product group 1 :
add_hook('ClientAreaPage', 1, function($vars) {
if ($_SERVER['REQUEST_URI'] === '/store/adhesion') {
return;
}
if (isset($_SESSION['uid']) && $_SESSION['uid']) {
$client_id = $_SESSION['uid'];
$result = localAPI('GetClientsProducts', array('clientid' => $client_id, 'status' => 'Active'), '');
if ($result['result'] == 'success') {
$products = $result['products'];
$has_active_product = false;
foreach ($products as $product) {
if ($product['gid'] == 1 && $product['status'] == 'Active') {
$has_active_product = true;
break;
}
}
if (!$has_active_product) {
header('Location: /store/adhesion');
exit;
}
}
}
});
With this, the customer is always redirected to the page /store/adhesion even if he has an active product,
I check documentation, this is my first hook and I can't figure out where the problem is.
Can you help me ?
Thank you so much

CouchDB Design Doc...Save Failed: Expression Does Not Eval To a Function

So I'm trying to create a design doc in CouchDB and there's a slight problem. This is for a robotics team scouting system. The design doc is supposed to check if a user has the role "marea". If they do, then they are allowed to input a document into the database. Then the design doc should take the JSON field "points" and average it if the "team" fields match. The averaged value should be merged back into the database. If the user does not have the "marea" role, then they should be restricted from inputting any docs. Please let me know what you think of the below code, and I would also like some advice on the error message I get. I'm able to save the design doc just fine, but its functioning is a little iffy. It prevents the user from creating a new doc with a "points" value even when that user has the role "marea". The exact error message is:
Save failed: Expression does not eval to a function. (function(newDoc, oldDoc, userCtx, secObj) { if ((userCtx.roles.indexOf("2018marea") !==
-1) || (userCtx.name == oldDoc.name)) { if (!doc) { if ("id" in req && req["id"]) { return [{"_id": req["id"]}, "New Doc"] emit(null, "points") var pointsArray = ["points"], thisTotal = 0, thisAverage = 0; for(var i = 0;i < pointsArray.length; i++) { thisTotal+ = pointsArray[i]; } thisAverage = (thisTotal/pointsArray.length); } return [null, "Empty Database."] } doc["New Doc"] = thisAverage; doc["edited_by"] = req["userCtx"]["name"] return [doc, "Edited Data."] } else { return [null, "Cannot Overwrite Data"] } } )
How do I fix this problem so that the design doc carries out its intended function?
{
"_id": "_design/marea",
"language": "javascript",
"validate_doc_update": "function(newDoc, oldDoc, userCtx, secObj) {\r\n\r\n if ((userCtx.roles.indexOf(\"2018marea\") !== -1) || (userCtx.name == oldDoc.name)) { \r\n \r\n if (!doc) {\r\n \r\n if (\"id\" in req && req[\"id\"]) {\r\n \r\n return [{\"_id\": req[\"id\"]}, \"New Doc\"] \r\n emit(null, \"points\")\r\n var pointsArray = [\"points\"], thisTotal = 0, thisAverage = 0;\r\n for(var i = 0;i < pointsArray.length; i++) {\r\n \r\n thisTotal+ = pointsArray[i];\r\n \r\n }\r\n \r\n thisAverage = (thisTotal/pointsArray.length); \r\n \r\n }\r\n \r\n return [null, \"Empty Database.\"]\r\n \r\n }\r\n \r\n doc[\"New Doc\"] = thisAverage;\r\n doc[\"edited_by\"] = req[\"userCtx\"][\"name\"] \r\n return [doc, \"Edited Data.\"] \r\n\r\n } else {\r\n \r\n return [null, \"Cannot Overwrite Data\"]\r\n \r\n }\r\n } "
}
The error is pretty obvious: You function is not valid
There's a space between + = : { thisTotal+ = pointsArray[i]; }
You forgot some semi colon between few statements:
"New Doc"] ; emit(null, "points"); var pointsArray
This should work better:
function(newDoc, oldDoc, userCtx, secObj) {
if ((userCtx.roles.indexOf("2018marea") !== -1) || (userCtx.name == oldDoc.name)) {
if (!doc) {
if ("id" in req && req["id"]) {
return [{
"_id": req["id"]
}, "New Doc"];emit(null, "points"); var pointsArray = ["points"],
thisTotal = 0,
thisAverage = 0;
for (var i = 0; i < pointsArray.length; i++) {
thisTotal += pointsArray[i];
}
thisAverage = (thisTotal / pointsArray.length);
}
return [null, "Empty Database."]
}
doc["New Doc"] = thisAverage;
doc["edited_by"] = req["userCtx"]["name"]
return [doc, "Edited Data."]
} else {
return [null, "Cannot Overwrite Data"]
}
}

The Intern: Polling Until Element is Visible

Is there a way in Intern that I can poll until an element is visible? A lot of elements in my website are in the dom but are hidden, so every time I do a "find for" element X after it is supposed to appear, it fails because the element clearly breaks one of the visible attributes that selenium checks.
I've tried the helper function "pollUntil" but I can't seem to get that to work. Dojo seems to not like document.getElement*()
Helper Function that is passed into pollUntil
//this is a helper function for pollUntil
//first we want to find an element by class name and text
var elementVisibleAndText = function(elems, innerText){
elems = document.getElementsByClassName(elems);
//if we can't even find it, we return null
//but if we do find it, we want to return a
//not null element
if (!elems || elems.length == 0){
return null;
}
//now let's look at all of the elements found by
//in elems, and see if the innerHTML matches. If it
//does then we want to return that it was found
var each;
for(each in elems){
if(elems[each].innerHTML == innerText)
return (elems[each].offsetWidth > 0 && elems[each].offsetHeight > 0) ? elems[each] : null;
}
//else return null if nothing is found in the elements
return null;
};
Check out https://theintern.github.io/leadfoot/pollUntil.html. Intern uses leadfoot - so you should have access to this functionality.
var Command = require('leadfoot/Command');
var pollUntil = require('leadfoot/helpers/pollUntil');
new Command(session)
.get('http://example.com')
.then(pollUntil('return document.getElementById("a");', 1000))
.then(function (elementA) {
// element was found
}, function (error) {
// element was not found
});
To use the function within one of your tests - you would import it using the following path:
'intern/dojo/node!leadfoot/helpers/pollUntil'
I run into this issue all the time and we used the pollUntil functionality of intern to write a few helper utilities. In our tests we use something like .then(pollUntil(util.element_visible_by_class(), ['toast_notification'], 22000))
In a separate util.js file we have
/**
* Our shared utility for unit testing
*/
define([
'intern!object',
'intern/chai!assert',
'require',
'intern/dojo/node!leadfoot/helpers/pollUntil'
], function (registerSuite, assert, require, util, pollUntil) {
return {
element_visible_by_class: function(elem) {
return function(elem) {
elem = document.getElementsByClassName(elem);
if (!elem || elem.length == 0) { return null; }
elem = elem[0];
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
}
},
element_visible_by_id: function(elem) {
return function(elem) {
elem = document.getElementById(elem);
if (!elem) { return null; }
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? elem : null;
}
},
element_hidden_by_id: function(elem) {
return function(elem) {
elem = document.getElementById(elem);
if (!elem) { return null; }
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
}
},
element_hidden_by_class: function(elem) {
return function(elem) {
elem = document.getElementsByClassName(elem);
if (!elem || elem.length == 0) { return null; }
elem = elem[0];
return (elem.offsetWidth > 0 && elem.offsetHeight > 0) ? null : elem;
}
},
}
})

Couchbase/CouchDB grouping by part of key and ordering by another part

I've got a database of messages between users. Document looks like:
"_id": "msg_0bec56c1-cbfb-47a5-8882-4a61fec332cd",
"_rev": "1-00000eda4d07c93d0000009100000112",
"$flags": 0,
"$expiration": 0,
"Date": 1340280439303,
"OwnerId": 35,
"RcptId": 37,
"SndrId": 35,
"Text": "msg5",
"Unread": false,
"id": "0bec56c1-cbfb-47a5-8882-4a61fec332cd",
"type": "msg"
For each message it stores 2 documents with different OwnerId. I need to get the lastest message between one specified person and 10 uniqe "pen friends" ordered by last message date.
My query params:
descending=true&endkey=[35,35]&startkey=[35,35,"{}"]&limit=10&skip=0
My map function:
function (doc) {
if (doc.type == "msg"){
emit([doc.OwnerId, doc.SndrId, doc.Date, doc.RcptId], doc);
emit([doc.OwnerId, doc.RcptId, doc.Date, doc.SndrId], doc);
}
}
As a result I get list of messages for desired account. Last value (4th) in key is that value on which we should group values.
Groupping with group level 4 not working because dates are different.
Here is my new map function that does exactly what i've wanted BUT only if I have one server.
var uniqPairs = [];
function(doc){
if (doc.type == "msg"){
if (doc.OwnerId == doc.SndrId){
if (uniqPairs.indexOf(doc.OwnerId + "_" + doc.RcptId) == -1){
uniqPairs.push(doc.SndrId + "_" + doc.RcptId);
uniqPairs.push(doc.RcptId + "_" + doc.SndrId);
emit([doc.OwnerId, doc.Date], {"owner": doc.OwnerId, "from":doc.SndrId, "to":doc.RcptId});
}
}
if (doc.OwnerId == doc.RcptId){
if (uniqPairs.indexOf(doc.OwnerId + "_" + doc.SndrId) == -1){
//uniqPairs.push(doc.SndrId + "_" + doc.RcptId);
uniqPairs.push(doc.RcptId + "_" + doc.SndrId);
emit([doc.OwnerId, doc.Date], {"owner": doc.OwnerId, "from":doc.SndrId, "to":doc.RcptId});
}
}
}
}
So for cluster I sacrificed "ordering by date" and got such functions for map/reduce:
Map:
function (doc) {
if (doc.type == "msg"){
if (doc.OwnerId == doc.SndrId){
emit([doc.OwnerId, doc.RcptId, doc.Date], {"Date":doc.Date, "OwnerId":doc.OwnerId, "RcptId":doc.RcptId, "SndrId":doc.SndrId, "Text":doc.Text, "Unread":doc.Unread, "id": doc.id, "type":doc.type});
} else {
emit([doc.OwnerId, doc.SndrId, doc.Date], {"Date":doc.Date, "OwnerId":doc.OwnerId, "RcptId":doc.RcptId, "SndrId":doc.SndrId, "Text":doc.Text, "Unread":doc.Unread, "id": doc.id, "type":doc.type});
}
}
}
Reduce:
var tmp = [];
var t = [];
function findRecent(arr){
var max = 0;
var maxId = 0;
for (var i in arr){
if (arr[i].Date > max){
max = arr[i].Date;
maxId = i;
}
}
return arr[maxId];
}
function(k,v,r){
if (!r){
tmp.push(v);
}
else{
tmp.push(v);
}
if (r){
for (var i1 in tmp[0])
{
t.push(tmp[0][i1]);
}
} else {
return findRecent(v);
}
return findRecent(t);
}
If someone knows better solution (i.e. how to oreder results by date) - you're welcome to answer.

Drupal - Dispaly block if term page has no tagged nodes and no child terms?

How can I show a block on term pages if no nodes are tagged with that term and also there are no child terms?
Im using Drupal 6.
Thanks
Tricky one but you could use custom PHP code for your block display, something like this (assuming Drupal 6 here):
if (strstr($_GET['q'], 'taxonomy/term/')) {
$parts = explode('/', $_GET['q']);
$term = taxonomy_get_term($parts(2));
if ($term && $term->tid) {
$node_count = db_result(db_query('SELECT COUNT(nid) FROM {term_node} WHERE tid = %d', $term->tid));
if ($node_count == 0) {
return FALSE;
}
if (count(taxonomy_get_children($term->tid)) == 0) {
return FALSE;
}
return TRUE;
}
}
return TRUE;
And for Drupal 7:
if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric($arg(2))) {
$term = taxonomy_term_load(arg(2));
if ($term && $term->tid) {
if (db_query('SELECT COUNT(nid) FROM {taxonomy_index} WHERE tid = :tid', array(':tid' => $term->tid))->fetchField() == 0) {
return FALSE;
}
if (count(taxonomy_get_children($term->tid)) == 0) {
return FALSE;
}
return TRUE;
}
}
return TRUE;

Resources