Forums

Set required field while putting in data

Ripei 26 Jan, 2010
Hey guys...

i've got a problem: i do have multiple fields... and one checkbox, my aim is, that if the user does check the checkbox... some of the fields should not be requiered any more...
i tried it with javascript code:

function setSeminarClass(){
var seminar1 = document.getElementById("text_1"); 
if(document.getElementById("mitarbeiter_checkbox").checked){
seminar1.setAttribute("class", "cf_inputbox");
}else{
seminar1.setAttribute("class", "cf_inputbox validate-alpharequired");
}
}


but the problem is that this doesn't work. well the js code does work... it changes the class name... but this doesn't have any affect... the seminar1-field is still required.😑

regards Ripei
GreyHead 27 Jan, 2010
Hi Ripei,

Yes I think that you have to use the LiveValidation enable and disable functions. From memory ChronoForms reads these classes on page load and adds the LiveValidations then.

There was a discussion about this a month or so ago.

Bob
Ripei 27 Jan, 2010
Ah... ok, thanks for your advice, but i am afraid i don't know how to set the LiveValidation enable and disable function? If i got you right, then i have to change them every time the checkbox is checked?
GreyHead 27 Jan, 2010
Hi Ripei,

Yes, it's possible but tricky to get it right - some one made a good post about this at the end of last year. In that case it was about using a script to add extra copies of some fields I think.

Or. . . you ditch the ChronoForms LiveValidation and set up your own - that gives you some more control.

Bob
Ripei 27 Jan, 2010
hey...

well i would prefer to take the "enable,disable of livevalidation" - way
the problem i have to face now is, that i don't know how i can geht the active validation.
i know, that i can set a new one with:
var field1 = new LiveValidation(...);

But I have to disable the active livevalidation, don't I? So is there a way like:
var field1 = getActiveValidation(...);?
Ripei 28 Jan, 2010
well i searched through the net... but couldn't find any ideas about how to get the active livevalidation. is there anybody, knowing a possible solution?
GreyHead 28 Jan, 2010
Hi Ripei,

The LiveValidation docs for disable are here

Bob
nml375 28 Jan, 2010
Hi Ripei,
This is a bit scetchy and untested, but hopefully it should get you started:
<? 
/* Prepare the JavaScript delcaration */
$script = "
var seminar = new LiveValidation('text_1', {validMessage: '', onlyOnSubmit: true});
seminar.add(
  Validate.Format,
  {
    pattern: \^[a-zA-Z ]+$/,
    failureMessage: 'Please use letters only (a-z) in this field.'
  }
);

var check_seminar = function(e) {
  if (e.checked) {
    seminar.enable();
  else
    seminar.disable();
}";

/* Add the JavaScript to the document head only if we're on the front-end.
 * This way, we don't break the backend/admin interface...
 */
global $mainframe;
if ($mainframe->isSite())
  JFactory::getDocument()->addScriptDeclaration($script);
?>
...
Seminar: <input type="checkbox" name="text_1" id="text_1" onBlur="check_seminar(this)" />

The code above is intended to go into the Form HTML. Just make sure you don't add any validation classes to the text_1 input field in ChronoForms backend.

/Fredrik
Ripei 30 Jan, 2010
thanks @nml375

i added the following code into the "Form Javascript":


$script = "
var seminar = new LiveValidation('text_0', {validMessage: '', onlyOnBlur: true});
seminar.add(Validate.Presence,{failureMessage: 'Bitte Seminar wählen'});


var checkSeminar = function(e) {
  if (e.checked) {
    seminar.enable();
  else
    seminar.disable();
}";

/* Add the JavaScript to the document head only if we're on the front-end.
* This way, we don't break the backend/admin interface...
*/
global $mainframe;
if ($mainframe->isSite()){
  JFactory::getDocument()->addScriptDeclaration($script);
}


But this still doesn't work as expected. I removed the Css - Classes of the "text_0" field, to avoid a conflict with the normal LiveValidation... but the code still has no effect?!
GreyHead 30 Jan, 2010
Hi Ripei,

Fredrik has written this to go in the Form HTML box (no the JavaScript box).

The code above is intended to go into the Form HTML.



Bob
Ripei 30 Jan, 2010
well you're right Bob, i first inserted the code into the "Form HTML" but there i recieved the following error:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /homepages/21/*/htdocs/*/components/com_chronocontact/chronocontact.html.php(180) : eval()'d code on line 19


it seems like there is a probleme on this line:
JFactory::getDocument()->addScriptDeclaration($script);
GreyHead 30 Jan, 2010
Hi Ripei,

Plese try making that into two lines:
$doc =& JFactory::getDocument();
$doc->addScriptDeclaration($script);

Bob
nml375 30 Jan, 2010
Hi all,
Bob is right, I jumped a corner too much in that piece of code. Just remember to adjust the if-conditional, as you'll need to enclose the two commands with {} for them both to be part of the 'if-true' code
if ($mainframe->isSite()) {
  $doc =& JFactory::getDocument();
  $doc ->addScriptDeclaration($script);
}


/Fredrik
Ripei 31 Jan, 2010
Ok, now there's no error when i put the code into the "Form HTML".
put actually the code does not have an effect. and i figured out another thing:
i put an alert into the checkseminar function. if i put it before the "seminar.disable()" - the popup appears. but if i put it after this line, the alert doesn't show up.
does this make any sense?

<?
$script = "
var seminar = new LiveValidation('text0',{ validMessage: 'Hey there!', wait: 500});
seminar.add(Validate.Presence,{failureMessage: 'Bitte Seminar wählen'});


function checkseminar(e){
  if (e.checked){
    seminar.disable();
    alert('if');
  }else{
    seminar.enable();
    alert('else');
  }
}";

/* Add the JavaScript to the document head only if we're on the front-end.
* This way, we don't break the backend/admin interface...
*/
global $mainframe;
if ($mainframe->isSite()){
  $doc =& JFactory::getDocument();
  $doc->addScriptDeclaration($script);
}
?>
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger