Required fields elements

Bodaggnmo

New Member
Hello,
is it possible to link elements to define only one of them as required field?
The user can enter the document title in German and English, or only in German or only in English. These are two elements:

document_title_ger
document_title_en

One of them must be completed, but not necessarily both. Therefore, the question is whether you can link the elements so that at least one is filled in. I know that with Validation I can set an element to a required field, but I don't know how to link the elements, that only one of them has to be filled in.

Thanks a lot
 
You'll have to use a PHP validation, and check yourself. So put a PHP validation on both, and do something like ...

Code:
return !empty($formModel->formData['yourtable___document_title_ger']) || !empty($formModel->formData['yourtable___document_title_gen]);

... in both. Which should only return false (fail validation) if both fields are empty.

Replace 'yourtable' with your table name, ie. the full element names.

-- hugh
 
@cheesegrits maybe you can help we again.

I have 85 elemets with the same structure, they are all Radio Buttons. The user can select if he puts 1,0 or H. But he is only allowed to put in one Element "H", 1 can be put in more then one. is their also a Validation function that checks, that only one element has the H?

I tried:
$element_1 = $formModel->formData['table___element_1'];
$element_2 = $formModel->formData['table___element_2'];
if ($element_1 == "H" and $element_2 == "H")
{ return true;}
else return false;

I put this in the Validation as PHP in the second Code block.. but it doesn't work..


edit:

I managed it:

if ($formModel->formData['master_table___assign_id_115_10'][0] == "H" and $formModel->formData['master_table___assign_id_100_10'][0] == "H")
{ return false;}
else return true;


Is there a possibility for a Shorter Code? Because i have 85 Elements.
 
Last edited:
There isn't a validation plugin to do that. There's an 'isunique', but that tests to see if the value entered for a single field is unique in the whole table (so does a database lookup to see if any rows already have that field set to that value).

Are your element names actually structured like 'element_1', 'element_2', ..., 'element_85', or do they have different names?

if they are structured like that, you could do a loop ...

Code:
$found = 0;
for ($x = 1; $x <= 85; $x++) {
   if ($formModel->formData['yourtable___element_' . $x][0] === 'H') {
      $found ++;
   }
   if ($found >= 2) {
      return false;
   }
}
return true;

If the names aren't structured in such a way that you can use a loop like that, you'll have to create an array of all the element names you want to check, and loop through that.

Rather than putting the above code in every PHP validation, I'd recommend using the Custom class feature. Here's how to do that, with code using an array of elements names. If your names are sequential, just replace the 'foreach' loop in the following code with the one above, and get rid of the $elements array.

Rename the file ...

libraries/fabrik/fabrik/Helpers/CustomSample.php

... to ...

libraries/fabrik/fabrik/Helpers/Custom.php

... and follow the instructions within it - so rename 'class CustomClass' to 'class Custom'.

Then replace the example 'doMyThing' function with this. You can call the function whatever you want, it doesn't have to be "checkUnique".

Code:
/**
   * Validation function called by elements on form "My Form Name"
  * (replace this comment with whatever makes sense so you (or someone else) will know what it does in the future)
  */
static public function checkUnique($formModel)
{
   // replace the names in this array with all the elements you want to check
   $elements = array(
      'yourtable___element1',
      'yourtable___element2',
      'yourtable___element3'
   );
   
   $found = 0;

   foreach ($elements as $element) {
      if ($formModel->formData[$element][0] === 'H') {
         $found ++;
      }
      if ($found >= 2) {
         return false;
      }
   }

   return true;
}

Then, in your PHP validations, do ...

Code:
return FabrikCustom::checkUnique($formModel);

The advantage of using the Custom class and just calling your function from within the validation is that if you ever need to change the code (like you add a new element, or value that needs testing), you only have to change the code in one place, not 85.

NOTE - be careful when creating and editing this file. If you break it such that it errors out, your site won't load, as this file gets loaded automatically, on every page load.

-- hugh
 
Thank you for your detailed Explanation. Unfortunatelly i don't have such i file in this Directory. And does it check the whole Database? Because one H in this colums is only allowed in one line. The Elements are the columns. And yes as you thought, the Elements are structured in another way. May i create this file or does it Crash the System?
 
You must do a GitHub update to get the "Custom" feature.

The code above only tests the form fields. If you want additionally test against something in the database (I don't understand what you mean with "one H in this colums is only allowed in one line") you must add database queries to get the data.
 
Thank you! Code from @cheesegrits works also without the Custom.php file. I just put the Array command and the foreach part in one element as php Validation and that's it. Perfect!
 
During the testing, i saw that if an error occurs, all Elements with the Validation are marked as an error. Can i manage it, that the Error Message occures only on the Element which is wrong? I know that everything is red, because of the Validation failed, because in one element the Validation checks all Elements..
 
Are you talking about a full form validation (after submitting), or AJAX validation of a single element?

-- hugh
 
I think im talking about a AJAX Validation. I put to one element a php Validation, which checks all the other Elements. If i put the Validation to every element which is checked, the error is on every element, which i understand and which is right. But is there a possibility to only Highlight as an error the elemets, which are wrong?
 
Last edited:
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top