formfields.com Forum Index formfields.com
FORMfields Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Conditional fields in FORMfields

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    formfields.com Forum Index -> FF 2.X - Frequently Asked Questions (FAQs)
View previous topic :: View next topic  
Author Message
rwstalzer



Joined: 23 Apr 2006
Posts: 1

PostPosted: Sun Apr 23, 2006 4:34 am    Post subject: Conditional fields in FORMfields Reply with quote

I need to make a field required only if there is data entered in another (previous) field.

For example:
I create a field for hourly wages, and make it required. But I also need a field for monthly salary and need it to be required, but only if there is no value intered in the hourly wages.

Can, and if so how can this be accomplished

Thanks

Bob
Back to top
View user's profile Send private message
formfields



Joined: 01 Aug 2005
Posts: 465

PostPosted: Thu Apr 27, 2006 9:10 pm    Post subject: Reply with quote

In this example, we will assume that you require either Hourly Wage or Monthly Salary to be completed, but not both.

Let's say we have the following fields:
Code:

   $formBean->addField(new DollarField("hourly_wages", "Hourly Wages", NOT_REQUIRED, 5, 1, 5, 1000, 1));
   $formBean->addField(new DollarField("monthly_salary", "Monthly Salary", NOT_REQUIRED, 100, 1, 5, 1000, 1));

then we can enforce this restriction by adding the following:
Code:

...
   if (FormField::getOrNull("_submit", $_REQUEST) || FormField::getOrNull("_confirm", $_REQUEST)) {
      if ($formBean->isBlank("hourly_wages") && $formBean->isBlank("monthly_salary")) {
         $formBean->setError("hourly_wages", "Hourly Wages and Monthly Salary cannot both be blank!");
         $formBean->setError("monthly_salary", "Hourly Wages and Monthly Salary cannot both be blank!");
      } else if (!$formBean->isBlank("hourly_wages") && !$formBean->isBlank("monthly_salary")) {
         $formBean->setError("hourly_wages", "Hourly Wages and Monthly Salary cannot both be filled!");
         $formBean->setError("monthly_salary", "Hourly Wages and Monthly Salary cannot both be filled!");
      }
      if ($formBean->checkValues()) {
         if (FF_DISPLAY_CONFIRMATION && !FormField::getOrNull("_confirm", $_REQUEST)) {
            // CONFIRMATION OPERATION:
...
Back to top
View user's profile Send private message
formfields



Joined: 01 Aug 2005
Posts: 465

PostPosted: Thu Dec 14, 2006 9:09 pm    Post subject: Reply with quote

Here is an example of how to make fields appear dynamically when a checkbox is checked.

1. Use FORMgen to generate the form with all the fields, ignoring the conditions:
Code:

...
$formBean = new FormBean();
$formBean->addField(new SubmitField("_submit", "Submit"));
$formBean->addField(new SubmitField("clear", "Clear"));
$formBean->addField(new SubmitField("cancel", "Cancel"));
$formBean->addField(new CheckboxField("sports_in_collge", "I played sports in college"));
$formBean->addField(new TextField("sport", "Sport", REQUIRED, 30, 1, null));
$formBean->addField(new TextField("position", "Position", REQUIRED, 30, 1, null));
$formBean->addField(new TextField("college", "College", REQUIRED, 30, 1, null));
...


2. Add code to refresh the page when the user clicks the checkbox. Comment out the fields that only display when the check box is checked:
Code:

...
$formBean = new FormBean();
$formBean->addField(new SubmitField("_submit", "Submit"));
$formBean->addField(new SubmitField("clear", "Clear"));
$formBean->addField(new SubmitField("cancel", "Cancel"));
$formBean->addField(new CheckboxField("sports_in_collge", "I played sports in college"));
$formBean->formFields["sports_in_collge"]->setExtraHtml("onclick=\"document.forms[0].submit();\"");
//$formBean->addField(new TextField("sport", "Sport", REQUIRED, 30, 1, null));
//$formBean->addField(new TextField("position", "Position", REQUIRED, 30, 1, null));
$formBean->addField(new TextField("college", "College", REQUIRED, 30, 1, null));
...


3. Add the code to only display the fields when "I played sports in college" is checked:
Code:

...
$formBean = new FormBean();
$formBean->addField(new SubmitField("_submit", "Submit"));
$formBean->addField(new SubmitField("clear", "Clear"));
$formBean->addField(new SubmitField("cancel", "Cancel"));
$formBean->addField(new CheckboxField("sports_in_collge", "I played sports in college"));
$formBean->formFields["sports_in_collge"]->setExtraHtml("onclick=\"document.forms[0].submit();\"");
//$formBean->addField(new TextField("sport", "Sport", REQUIRED, 30, 1, null));
//$formBean->addField(new TextField("position", "Position", REQUIRED, 30, 1, null));
if (FormField::getOrNull("sports_in_collge", $_REQUEST)) {
   $formBean->addField(new TextField("sport", "Sport", REQUIRED, 30, 1, null));
   $formBean->addField(new TextField("position", "Position", REQUIRED, 30, 1, null));
}
$formBean->addField(new TextField("college", "College", REQUIRED, 30, 1, null));
...


4. Add code to remove the conditional fields when clear is clicked:
Code:

...
} else if (FormField::getOrNull("clear", $_REQUEST)) {
   // CLEAR OPERATION:
   $formBean->clearAllData();
   unset($formBean->formFields["sport"]);
   unset($formBean->formFields["position"]);
} else if (FormField::getOrNull("cancel", $_REQUEST)) {
...
Back to top
View user's profile Send private message
ec_cts



Joined: 28 Dec 2006
Posts: 3

PostPosted: Wed Jan 03, 2007 8:08 pm    Post subject: It's possible to use another field that is not a checkbox fo Reply with quote

It's possible to use like in the previous example, instead of a checkbox a field DropDownField?, in order that depending on the selected option, a following field appear dynamically.

Thanks in advance.

Carlos Torres
Back to top
View user's profile Send private message
ec_cts



Joined: 28 Dec 2006
Posts: 3

PostPosted: Wed Jan 03, 2007 8:14 pm    Post subject: I find the example Reply with quote

I find the example http://www.formfields.com/phpBB2/viewtopic.php?t=91&highlight=%2Amatthews%2A for DropDownField conditional.

Sorry

Carlos Torres
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    formfields.com Forum Index -> FF 2.X - Frequently Asked Questions (FAQs) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group