• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Script called before form is validated

Script called before form is validated 1 week 3 days ago #44107

  • lesmar
  • lesmar's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 5
Is there a way to check if two form fields i.e Race_Name_01 and Race_Name_02 have the same value and if so can the form fields be set to be invalid and an error message displayed saying Race Names are duplicated .

I tried this code but it came back with error
// Retrieve the values of the twelve text fields for race name 
$field01 = $_POST['form']['Race_Name_01']; 
$field02 = $_POST['form']['Race_Name_02']; 
$field03 = $_POST['form']['Race_Name_03']; 
$field04 = $_POST['form']['Race_Name_04']; 
$field05 = $_POST['form']['Race_Name_05']; 
$field06 = $_POST['form']['Race_Name_06'];
$field07 = $_POST['form']['Race_Name_07'];
$field08 = $_POST['form']['Race_Name_08']; 
$field09 = $_POST['form']['Race_Name_09']; 
$field10 = $_POST['form']['Race_Name_10']; 
$field11 = $_POST['form']['Race_Name_11']; 
$field12 = $_POST['form']['Race_Name_12'];
// Check if the values are duplicates
if ($field01 == $field02) { 
// Mark the form as invalid 
$invalid = true;
// Add an error message for the user
$form->setError('Race_Name_01', 'The selections for Race Name in Race 01 and Race 02 cannot be the same.');
$form->setError('Race_Name_02', 'The selections for Race Name in Race 01 and Race 02 cannot be the same.');
}

I get the error message -: Call to undefined method stdClass::setError() when Race_Name_01 = "'a" and Race_Name_02 = ""a""
The administrator has disabled public write access.

Script called before form is validated 4 days 10 hours ago #44116

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 267
  • Thank you received: 70
It's easier done with javascript using two functions, here's a breakdown of how it all works and be patient, it's a long one ;)

1. capitalizeWords(field)
Purpose: Automatically capitalizes the first letter of every word in a single input field.
How it works:
let value = field.value; → Gets whatever the user has typed in the field.
value.replace(/\b\w/g, c => c.toUpperCase()); → Looks for the first character of each word (\b\w) and converts it to uppercase.
field.value = value; → Puts the capitalized text back into the input field.

Example:
User types: race one
Function changes it to: Race One

2.checkRaceDuplicate(currentField)
Purpose:
Capitalizes words like capitalizeWords
Checks all 12 Race_Name fields for duplicates
Highlights duplicates and shows an inline message if found

Step-by-step:
const form = currentField.form; → Gets the form that this field belongs to.
Capitalize words in the current field (same as capitalizeWords).
Collect all 12 Race_Name fields in an array (raceFields).
Clear previous highlights and messages from all fields so old warnings disappear.
Create a map of values to fields, ignoring case (val.toLowerCase()).

Example: "Race One" and "race one" are treated as the same.
Look for any value that appears more than once in valueMap.
For each duplicate:
Add a red border around the field
Insert an inline message below it:
"This value duplicates another field. Please enter a unique value."

Example:
Race_Name_01 = Race One
Race_Name_02 = race one
The function detects a duplicate, highlights both fields, and shows messages below them.

So finally here's the code
<script>
 
function capitalizeWords(field) {
    let value = field.value;
    // Capitalize the first letter of each word
    value = value.replace(/\b\w/g, c => c.toUpperCase());
    field.value = value;
}  
 
 
function checkRaceDuplicate(currentField) {
    const form = currentField.form;
    if (!form) return;
 
    // Capitalize each word in the current field
    let value = currentField.value.trim();
    value = value.replace(/\b\w/g, c => c.toUpperCase());
    currentField.value = value;
 
    // Collect all Race_Name fields
    const raceFields = [];
    for (let i = 1; i <= 12; i++) {
        const field = form.querySelector('[name="form[Race_Name_' + String(i).padStart(2, '0') + ']"]');
        if (field) raceFields.push(field);
    }
 
    // Clear previous highlights and messages
    raceFields.forEach(f => {
        f.style.border = '';
        const msg = f.nextElementSibling;
        if (msg && msg.classList.contains('duplicate-msg')) msg.remove();
    });
 
    // Map values to fields (case-insensitive)
    const valueMap = {};
    raceFields.forEach(f => {
        const val = f.value.trim().toLowerCase(); // lowercase for comparison
        if (!val) return;
        if (!valueMap[val]) {
            valueMap[val] = [f];
        } else {
            valueMap[val].push(f);
        }
    });
 
    // Highlight duplicates and add messages
    Object.values(valueMap).forEach(fields => {
        if (fields.length > 1) {
            fields.forEach(f => {
                f.style.border = '2px solid red';
                const msg = document.createElement('div');
                msg.className = 'duplicate-msg';
                msg.style.color = 'red';
                msg.style.fontSize = '0.9em';
                msg.innerText = 'This value duplicates another field. Please enter a unique value.';
                f.insertAdjacentElement('afterend', msg);
            });
        }
    });
}
</script>

call the functions like this, in the field Race_name_01 attributes use
oninput="capitalizeWords(this);"
and in each of the other fields use
oninput="checkRaceDuplicate(this);"
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
Last Edit: 4 days 9 hours ago by iceferret.
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!