 |
formfields.com FORMfields Forums
|
| View previous topic :: View next topic |
| Author |
Message |
rwstalzer
Joined: 23 Apr 2006 Posts: 1
|
Posted: Sun Apr 23, 2006 4:34 am Post subject: Conditional fields in FORMfields |
|
|
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 |
|
 |
formfields
Joined: 01 Aug 2005 Posts: 465
|
Posted: Thu Apr 27, 2006 9:10 pm Post subject: |
|
|
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 |
|
 |
formfields
Joined: 01 Aug 2005 Posts: 465
|
Posted: Thu Dec 14, 2006 9:09 pm Post subject: |
|
|
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 |
|
 |
ec_cts
Joined: 28 Dec 2006 Posts: 3
|
Posted: Wed Jan 03, 2007 8:08 pm Post subject: It's possible to use another field that is not a checkbox fo |
|
|
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 |
|
 |
ec_cts
Joined: 28 Dec 2006 Posts: 3
|
|
| Back to top |
|
 |
|
|
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
|