Kohana 3.0 - Validate: Error if one of fields aren't empty - kohana

I have four fields. Lets call them a, b, c and d. I need to validate them.
Error is when:
One til three fields are not empty;
Error is not when:
All fields are not empty,
All fields are empty;
Any neat solution here? Thanks in advice.
Edit:
Only relationships are that all four variables are prefixed with event_. It gives me event_name, event_description etc..
Edit #2:
At the moment I have something like...
if (
!empty($values['event_date'])
&& !empty($values['event_time'])
&& !empty($values['event_name'])
&& !empty($values['event_description'])
) {
It checks that all fields are filled up and then, if that's true, adds event.
As I said before, I need to display user-friendly error when some field isn't filled up (for example, user had forgot to enter description). Anyway, when all fields are filled up (it means - all okay) or when no fields are filled up (it means - user ignores event adding and don't want to add one) - no error should be displayed.
I could write code with 16 'if' statements, but isn't there any better way? :)

This isn't beautiful, but as long as you have something unique about the fields you want to check (such as "event_..."), you could loop through the variable array ($values, $_POST, etc) and check only the fields that matter. Then, you can easily check for an all or none situation.
Here is a quick example:
$total = 0;
$filled = 0;
foreach($values as $field => $val) {
if(strpos($field,'event_') === 0) {
$total++;
if( ! empty($val)) {
$filled++;
}
}
}
if($filled == 0 OR $total == $filled) {
//PASS VALIDATION
} else {
//FAIL VALIDATION
}

Is there a relationship between one of the entered values and the none entered values??
could you just parse it as an empty value?
if ( ! isset($post->a) ) $post->a = '';

Related

Trying to check to see if something exists and if not, assign something different to it with JavaScript

Wondering if someone can help, I'll try to explain the best I can. I am looking at a json file for various steam games. Basically what I want to do is check to see if a certain "key(?)" is present and if not then return "None".
e.g. Check to see if body[id].data.metacritic and if it does then assign score to body[id].data.metacritic.score. The same with URL. I cannot figure this out!
I have tried the following:
if(bulk.metacritic) var { score, url } = bulk.metacritic[0] || "None";
I just can't figure out how to get this right! Basically, body[id].data.metacritic doesn't exist in all json files, along with other parameters so I just want to display some placeholder text if they don't appear.
You can check if the body[id].data.metacritic is an array with a non-zero length and if so, use a value from it, otherwise use default values:
let score, url;
let data = body[id].data;
if (data && Array.isArray(data.metacritic) && data.metacritic.length > 0) {
score = data.metacritic[0].score;
url = data.metacritic[0].url;
} else {
score = url = "None";
}
console.log(score, url);

NotesXSPDocument and NotesDocument

I have created a function in SSJS Library. Because I use it in more than one XPages.
When I call this function behind a button I cannot see the value in the field
If I print it out I can see the value at the Admin Console but cannot see it in the form Even if I get page with full refreshed.
Actually my another question is.. is it possible to compare notesXSPDocument and NotesDocument. Maybe someoen can say that what is the best way for that?
function deneme(document1:NotesXSPDocument,otherDocfromOtherDatabase:NotesDocument)
{
//do staff here
if (document1.getItemValueString("field1")==otherDocfromOtherDatabase.getItemValueString("field2"))
{ //do some staff here...
document1.replaceItemValue("fieldName","FieldValue");}
}
You can compare item values from Document and XSPDocument, just be careful with the type you are comparing.
In your code you are comparing 2 javascript strings with == operator.
The code seems to be OK, just remember to save the document1 after the changes and maybe check that the items have some value.
var valueFromXspDoc = document1.getItemValueString("field1");
var valueFromDoc = otherDocfromOtherDatabase.getItemValueString("field2");
if (valueFromXspDoc && valueFromDoc && (valueFromXspDoc === valueFromDoc)) {
// stuff here...
document1.replaceItemValue("fieldName","FieldValue");
document1.save();
}
Don not compare it with == sign. A better way is to document1.getItemValueString("field1").equals(otherDocfromOtherDatabase.getItemValueString("field2"))

text field attributes/methods dynamics crm 2011

I'm looking a method or way how to check that the text field in crm form is "null"
I've got a tab, there are section and text field inside of it;
furthermore, I'm using that function in order to hide/show tab.
function setVisibleTabSection(tabname, TextFieldName, show) {
var tab = Xrm.Page.ui.tabs.get(tabname);
if (tab != null) {
if (TextFieldName == null)
tab.setVisible(show);
else {
var section = Xrm.Page.data.entity.attributes.get(TextFieldName).getValue();
if (section != null) {
show == true;
tab.setVisible(show);
}
}
}
}
however, It doesn't work. There is nothing inside of the text box, and the tab expanded anyway.
by the way, parameters, which I give the function: "tab_8", "new_conf_report", false
where the secon one the name of the text field
Try
if (section != null && section !="")...
You may find that a field which is initially blank is null, whereas one from which you have deleted content but not yet saved the form is simply an empty string.
Certainly worth a shot.
show==true
is incorrect as others have pointed out (needs to be show=true) but is simply redundant as written inside the same IF statement, just replace next line as:
tab.setVisible(true);
It is possible you intended "show" to be the default tab state to use if text field is not empty, in which case just move this line outside the IF instead of changing it (as shown below)
It looks like the construction using the third "show" parameter is to allow you to use the function to set the tab state to a specific state of shown or not without looking for a text field value at all. You would need to pass parameters as eg tabname,,true - you might consider swapping the TextFieldName and Show parameters so it is easier to just drop the third rather than remember to double-comma.
While we're fixing stuff, lets replace that variable "section" with something with a more meaningful name:
function setVisibleTabSection(tabname, show, TextFieldName) //usage: show is state Tab will have if no TextFieldName is specified, or if text field is empty
{
var tab = Xrm.Page.ui.tabs.get(tabname);
if (tab != null)
{
if (show==null){show=true;}
if (TextFieldName == null)
{
tab.setVisible(show);
}
else
{
var strFieldValue = Xrm.Page.data.entity.attributes.get(TextFieldName).getValue();
if (strFieldValue != null && strFieldValue !="")
{show=true;}
tab.setVisible(show);
}
}
}
I don't see anything wrong with your Javascript (besides what Guido points out, which basically will only set the tab to visible if you pass in true for show). Use the debugging tool within IE by pushing F12, and set a break point at the top of your function to see where your logic is failing.
If you've never debugged javascript before, see http://social.technet.microsoft.com/wiki/contents/articles/3256.how-to-debug-jscript-in-microsoft-dynamics-crm-2011.aspx
or
How to debug jScript for Dynamics CRM?
I think there is a typo in the code:
show == true;
actually the code (assuming "=" instead of "==") will show always the tab if TextFieldName isn't empty, removing that line will show/hide the tab according to show parameter value
It seems to work when I run it but I'm not sure what you'd expect it to do so it might not be working the way you'd like it to. :)
function setVisibleTabSection(tabName, textFieldName, show) {
var tab = Xrm.Page.ui.tabs.get(tabName);
if(!tab) return;
if (!TextFieldName)
tab.setVisible(show);
else {
var section = Xrm.Page.data.entity.attributes.get(textFieldName).getValue();
if (section)
tab.setVisible(true);
}
}

How to search and sort with CouchDB in one map function

I'm stumbling a bit with my CouchDB knowledge.
I have a database of content that is tagged with an array of tags and has a created date.
I want to create a view that pulls a limited number of newest stories tagged with a specific tag.
For example, the newest 6 stories tagged "Business."
Ran across this question, which seems to get me almost to where I need to go, but I'm missing one key element, which I think is how to craft the query string to sort by one key while searching by the other.
Here's my map function.
function(doc) {
if (doc.published == "yes" && doc.type == "news") {
for (var i = 0; i < doc.tags.length; i++) {
if (doc.tags[i]) {
emit([doc.created, doc.tags[i]], doc);
}
}
}
}
So how do I query that view for a all documents tagged "Business" that are the newest documents based on created.
The created attribute is a date sortable format.
First, I would switch the order of your emit:
emit([doc.tags[i], doc.created]);
(leave out doc as well, you can just add include_docs=true to get the entire document, and your view won't take up so much disk-space in the process)
Now you can query for the all the stories tagged as "Business" by using the following querystring:
startkey=["Business"]&endkey=["Business",{}]
You'll get all the documents with the tag business, and they'll be sorted by date.
This takes advantage of view collation, which basically is the rules governing how indexes are sorted/queried. For complex keys like this, the sorting is done for each item of the array separately. (ie. the first key is sorted first, the second key is sorted second, etc) This is why the order matters, as you must always move from left to right when querying a view index.
If you want the 6 most recent, your querystring will need to change:
descending=true&limit=6&endkey=["Business"]&startkey=["Business",{}]
NOTICE You need to swap the startkey/endkey values, due to how the descending parameter works. See the View reference page on the wiki for further explanation.
OK, I think I figured this out, but I'm not quite certain I fully understand it.
I found this story about complex keys and searching and sorting.
My map function looks like this:
function(doc) {
if (doc.published == "yes" && doc.type == "news") {
for (var i = 0; i < doc.tags.length; i++) {
if (doc.tags[i]) {
emit([doc.tags[i], doc.created], doc);
}
}
}
}
And to query and sort using it, the query looks like this.
http://localhost:5984/database/_design/story/_view/tagged?limit=10&startkey=["Business"]&endkey=["Business",{}]&descending=false
I'm getting the results I want, but I'm not entirely certain I understand it all.

getSubmittedValue() vs. getValue()

Is that correct:
When I query a value before validation (or if validation failed) I have to use getSubmittedValue();. Once the value is validated, even if I query it in another validation later in the page/control I have to use .getValue(); since getSubmittedValue(); returns null after successful validation?
This xsnippet makes it easier to handle this. It allows you to just call getComponentValue("inputText1") to get either value or submittedValue.
Here's the function for reference:
function getComponentValue(id){
  var field = getComponent(id);
  var value = field.getSubmittedValue();
  if( null == value ){
         // else not yet submitted
         value = field.getValue();
  }
 
  return value
}
There's a slightly easier way: if you're just expecting a simple single-value String, just call:
var compare = firstField.getValueAsString();
Otherwise, call:
var compare = com.ibm.xsp.util.FacesUtil.convertValue(facesContext, firstField);
The former calls the latter anyway, but is obviously a terser syntax. This does what you're looking for and more:
If the value hasn't yet been validated, returns the submitted value
If validation has already passed, returns the value after it's already been processed by any converters and / or content filters, so particularly in cases where you're trying to compare two field values, this should ensure that both values have been properly trimmed, etc., and is therefore less likely to return a false positive than just comparing the raw submitted values.
Found the answer here. So when you want to ensure that 2 text fields have the same value (use case: please repeat your email) and the first box already has a validation that might fail, you need to use submittedValue unless it is null, then you use the value. Code in the validation expression for the second field looks like this:
var firstField = getComponent("inputText1");
var compare = firstField.getSubmittedValue() || firstField.getValue();
compare == value;
You have to love it.

Resources