Login   Register  
PHP Classes
elePHPant
Icontem

default to value of a previous field

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Forms generation and validation  >  All threads  >  default to value of a previous field  >  (Un) Subscribe thread alerts  
Subject:default to value of a previous field
Summary:Default to Customer name for Company name
Messages:4
Author:Geoff Stacey
Date:2012-08-29 15:29:04
Update:2012-09-10 20:35:33
 

  1. default to value of a previous field   Reply   Report abuse  
Picture of Geoff Stacey
Geoff Stacey
2012-08-29 15:29:04
I have a form which accepts Customer first and last names and a Company name. If customers are not companies, but ordinary individuals, is there some way I can default the company to the customer's first and last names. I would like to do this as most of the customers are not companies.


This is the relevant code in the customer_form.php:


$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"last",
"ID"=>"last",
"MAXLENGTH"=>20,
"ValidateAsNotEmpty"=>1,
"ValidationErrorMessage"=>"Invalid Surname.",
"LABEL"=>"<u>S</u>urname",
"ACCESSKEY"=>"S"
));
$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"first",
"ID"=>"first",
"MAXLENGTH"=>20,
"ValidateAsNotEmpty"=>1,
"ValidationErrorMessage"=>"Invalid first name.",
"LABEL"=>"<u>F</u>irst name",
"ACCESSKEY"=>"F"
));
$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"company",
"ID"=>"company",
"MAXLENGTH"=>100,
"ValidateAsNotEmpty"=>1,
"ValidationErrorMessage"=>"Invalid company.",
"LABEL"=>"<u>C</u>ompany name",
"ACCESSKEY"=>"C"


And this is the code in the template:

<tr>
<th align="right" >
<?php $form->AddLabelPart(array("FOR"=>"last")); ?>:</th>
<td><?php $form->AddInputPart("last"); ?></td>
<td><?php echo (IsSet($verify["last"]) ? "[Verify]" : ""); ?></td>
</tr>

<tr>
<th align="right">
<?php $form->AddLabelPart(array("FOR"=>"first")); ?>:</th>
<td><?php $form->AddInputPart("first"); ?></td>
<td><?php echo (IsSet($verify["first"]) ? "[Verify]" : ""); ?></td>
</tr>

<tr>
<th align="right">
<?php $form->AddLabelPart(array("FOR"=>"company")); ?>:</th>
<td><?php $form->AddInputPart("company"); ?></td>
<td><?php echo (IsSet($verify["company"]) ? "[Verify]" : ""); ?></td>
</tr>

Regards,

Geoff.

  2. Re: default to value of a previous field   Reply   Report abuse  
Picture of Manuel Lemos
Manuel Lemos
2012-08-29 23:20:47 - In reply to message 1 from Geoff Stacey
That is a very specific requirement. The class does not provide a direct way to do that but you can implement it either in your code or creating a custom plug-in to do it.

There are two parts to implement, one to generate JavaScript to do that on the browser side, and the other to implement it on the server side, so it also works if JavaScript was disabled for some reason.

I would implement that behavior as a custom input so you can reuse it easily in many forms in your sites. The package comes with many custom inputs for different purposes, but all follow the same principle.

A custom plug-in is a separate class that extend the form_custom_class class. Take a look at the documentation and the available custom inputs to learn how to achieve what you want. Just let me know if you have difficulties.

  3. Re: default to value of a previous field   Reply   Report abuse  
Picture of Geoff Stacey
Geoff Stacey
2012-09-10 16:43:29 - In reply to message 2 from Manuel Lemos
Hi Manuel,

Thanks for your reply. I have got a chance to look at this again. I don't seem to have enough knowledge of how these plug-ins work and keep getting error messages I don't understand. I based my plug-in on the on named "form_custom_validation.php". This is what I get:
____________________________________________________________________________

My Form:

$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"last",
"ID"=>"last",
"MAXLENGTH"=>20,
"ValidateAsNotEmpty"=>1,
"ValidationErrorMessage"=>"Invalid Surname.",
"LABEL"=>"*<u>S</u>urname",
"ACCESSKEY"=>"S"
));
$form->AddInput(array(
"TYPE"=>"text",
"NAME"=>"first",
"ID"=>"first",
"MAXLENGTH"=>20,
"ValidateAsNotEmpty"=>1,
"ValidationErrorMessage"=>"Invalid first name.",
"LABEL"=>"*<u>F</u>irst name",
"ACCESSKEY"=>"F"
));

/*
* Add a custom input that will be used to retrieve the first and last fields
*/
$error=$form->AddInput(array(
'TYPE'=>'custom',
'NAME'=>'company',
'ID'=>'company',
"LABEL"=>"*Company name",
"ACCESSKEY"=>"C",
'CustomClass'=>'form_retrieve_field_class',
'FirstInput'=>'first',
'FirstValidationErrorMessage'=>'The first name is missing.',
'LastInput'=>'last',
'LastValidationErrorMessage'=>'The surname is missing.'
));


__________________________________________________________________________

My custom plug-in (form_retrieve_field.php) :

<?php
/*
*
* @(#) $Id: form_retrieve_field.php,v 1.3 2012/09/10 02:02:30 gstacey Exp $
*
*/

if(!defined("PHP_LIBRARY_RETRIEVE_FIELD_FORMS"))
{
define("PHP_LIBRARY_RETRIEVE_FIELD_FORMS",1);

class form_retrieve_field_class extends form_custom_class
{
/*
* Tell the forms class that this custom input will perform both server
* side and client side validation.
*/
var $client_retrieve=1;
var $server_retrieve=1;

var $first='';
var $last='';
var $first_validation_error_message='The first name input is empty.';
var $last_validation_error_message='The surname is empty.';

Function AddInput(&$form, $arguments)
{
/*
* Get the identifiers of the inputs to retrieve
*/
if(!IsSet($arguments['FirstInput'])
|| strlen($this->first=$arguments['FirstInput'])==0)
return('Invalid first input identifier');
if(!IsSet($arguments['LastInput'])
|| strlen($this->last=$arguments['LastInput'])==0)
return('Invalid last input identifier');

/*
* Get the error messages to assign when the inputs are invalid.
*/
if(IsSet($arguments['FirstValidationErrorMessage']))
{
if(strlen($arguments['FirstValidationErrorMessage'])==0)
return('Invalid first validation error message');
$this->first_validation_error_message=$arguments['FirstValidationErrorMessage'];
}
if(IsSet($arguments['LastValidationErrorMessage']))
{
if(strlen($arguments['LastValidationErrorMessage'])==0)
return('Invalid last validation error message');
$this->last_validation_error_message=$arguments['LastValidationErrorMessage'];
}
//$this->company=$this->first=$arguments['FirstInput'])
$this->company=$this->GenerateInputID($form, $this->input, "first");
return('');
}

Function ValidateInput(&$form)
{
/*
* Perform server side validation by checking whether one of the
* input values contains the other.
*
* This function is called after all validations were performed on
* all basic inputs.
*/
$first=$form->GetInputValue($this->first);
$last=$form->GetInputValue($this->last);
/*
* It is not necessary to do this check, although it won't do any harm
*
*/
if(strlen($first)
&& strstr($last, $first))
{
$form->FlagInvalidInput($this->first, $this->first_validation_error_message);
return('');
}
if(strlen($last)
&& strstr($first, $last))
{
$form->FlagInvalidInput($this->last, $this->last_validation_error_message);
return('');
}
return('');
}

Function GetJavascriptValidations(&$form, $form_object, &$validations)
{
/*
* Generate all the necessary Javascript to perform server side
* validation.
*/
if(strlen($first=$form->GetJavascriptInputValue($form_object,$this->first))==0)
return('it was not possible to retrieve the first input Javascript value');
if(strlen($last=$form->GetJavascriptInputValue($form_object,$this->last))==0)
return('it was not possible to retrieve the last input Javascript value');

/*
* Return an array with a list of all validation checks to be
* performed.
*/
$validations=array();
$validations[]=array(

/*
* Each.validation check may be preceed by a list of Javascript
* commands that are executed before each validation is performed.
*/
'Commands'=>array(
'first='.$first,
'last='.$last,
),

/*
* The condition is a boolean Javascript expression that is true
* when the input is invalid.
*/
'Condition'=>'last.indexOf(first) != -1',

/*
* Error message associated to the invalid input
*/
'ErrorMessage'=>$this->first_validation_error_message,

/*
* Input that gets the user input focus so the user fixes its value
* to make the input valid
*/
'Focus'=>$this->first
);
$validations[]=array(
'Commands'=>array(),
'Condition'=>'first.indexOf(last) != -1',
'ErrorMessage'=>$this->last_validation_error_message,
'Focus'=>$this->last
);
return('');
}

Function AddLabelPart(&$form, $arguments)
{
if(!strcmp($this->inputs[$for]["TYPE"],"custom"))
{
if(strlen($error=$this->inputs[$for]["object"]->AddLabelPart($this, $arguments)))
{
$this->OutputError($error, $for);
return($error);
}
return("");
}
$label=array("FOR"=>$for);
if(IsSet($arguments["LABEL"]))
$label["LABEL"]=$arguments["LABEL"];
else
{
if(IsSet($this->inputs[$for]["LABEL"]))
$label["LABEL"]=$this->inputs[$for]["LABEL"];
else
$label["LABEL"]="";
}
if(IsSet($arguments["ACCESSKEY"]))
$label["ACCESSKEY"]=$arguments["ACCESSKEY"];
else
{
if(IsSet($this->inputs[$for]["ACCESSKEY"]))
$label["ACCESSKEY"]=$this->inputs[$for]["ACCESSKEY"];
else
$label["ACCESSKEY"]="";
}
if(strlen($label["ACCESSKEY"])==0)
Unset($label["ACCESSKEY"]);
else
{
$lower=$this->tolower_function;
$key=$lower($label["ACCESSKEY"]);
if(!$this->allow_used_access_keys)
{
if(IsSet($this->label_access_keys[$key]))
return($this->OutputError("it was specified label FOR input \"".$label["FOR"]."\" that was already specified for input \"".$this->label_access_keys[$key]."\"",$for));
}
if(IsSet($this->reserved_access_keys[$key]))
return($this->OutputError("it was specified label FOR input \"".$label["FOR"]."\" that is already reserved for \"".$this->reserved_access_keys[$key]."\"",$for));
$this->label_access_keys[$key]=$for;
}
if(strlen($label["LABEL"])==0)
return($this->OutputError("it was not specified a valid label",$for));
if(IsSet($arguments["ID"]))
$label["ID"]=$arguments["ID"];
elseif(IsSet($this->inputs[$for]["LabelID"]))
$label["ID"]=$this->inputs[$for]["LabelID"];
if(IsSet($arguments["TITLE"]))
$label["TITLE"]=$arguments["TITLE"];
elseif(IsSet($this->inputs[$for]["LabelTITLE"]))
$label["TITLE"]=$this->inputs[$for]["LabelTITLE"];
if(IsSet($arguments["STYLE"]))
$label["STYLE"]=$arguments["STYLE"];
elseif(IsSet($this->inputs[$for]["LabelSTYLE"]))
$label["STYLE"]=$this->inputs[$for]["LabelSTYLE"];
if(IsSet($arguments["CLASS"]))
$label["CLASS"]=$arguments["CLASS"];
elseif(IsSet($this->inputs[$for]["LabelCLASS"]))
$label["CLASS"]=$this->inputs[$for]["LabelCLASS"];
if(IsSet($arguments["ExtraAttributes"]))
$label["ExtraAttributes"]=$arguments["ExtraAttributes"];
elseif(IsSet($this->inputs[$for]["LabelExtraAttributes"]))
$label["ExtraAttributes"]=$this->inputs[$for]["LabelExtraAttributes"];
if($this->capturing)
$this->AddDataPart("");
$this->parts[]=$label;
$this->types[]=(($this->ReadOnly && (!IsSet($this->inputs[$for]["Accessible"]) || !$this->inputs[$for]["Accessible"])) ? "READ_ONLY_LABEL" : "LABEL");
return("");
}
Function OutputError($error,$scope="")
{
$this->error=(strcmp($scope,"") ? $scope.": ".$error : $error);
if(strcmp($function=$this->debug,"")
&& strcmp($this->error,""))
$this->OutputDebug($this->error);
return($this->error);
}

Function SetSelectedOptions($name, &$input, &$options, &$value)
{
if(!IsSet($options)
|| strcmp(GetType($options),"array")
|| count($options)==0)
return($this->OutputError("it was not defined a valid options array",$name));
if(IsSet($input["MULTIPLE"]))
{
$selected=array();
if(strcmp(GetType($value),"array"))
return($this->OutputError("it was not defined a valid selected options array",$name));
for($option=0;$option<count($value);$option++)
{
$option_value=$value[$option];
if(!IsSet($options[$option_value]))
return($this->OutputError("it specified a selected value that is not a valid option",$name));
if(IsSet($selected[$option_value]))
return($this->OutputError("it specified a repeated selected option value",$name));
$selected[$option_value]=1;
}
$input["SELECTED"]=$selected;
}
else
{
if(!IsSet($value)
|| (strcmp(GetType($value),"string")
&& strcmp(GetType($value),"integer")
&& strcmp(GetType($value),"double"))
|| !IsSet($options[strval($value)]))
return($this->OutputError("it was not defined a valid input value",$name));
$input["VALUE"]=strval($value);
}
$input["OPTIONS"]=$options;
return("");
}
Function AddInputPart(&$form)
{
if(count($this->valid_marks)==0
|| strlen($this->format)==0)
return("form input custom class does not implement the AddInputPart function");
if(strlen($error=$this->ParseInputFormat()))
return($error);
return($this->AddFormattedPart($form, $this->format_data, $this->format_marks, 0));
}

};

}

?>
______________________________________________________________

The error messages:


Notice: company: it was not specified a valid label inC:\xampp\htdocs\autoroller\php\forms.php on line 730
:

Notice: company: form input custom class does not implement the AddInputPart function inC:\xampp\htdocs\autoroller\php\forms.php on line 730

__________________________________________________________

One error message was where the label should be; the other where the input field should be.

If it would not be too much trouble, I would be most grateful if you could have a look at this and see if you can see the problem,

Best regards,

Geoff.

  4. Re: default to value of a previous field   Reply   Report abuse  
Picture of Geoff Stacey
Geoff Stacey
2012-09-10 20:35:33 - In reply to message 3 from Geoff Stacey
I have had another look at what I had done and see that I shouldn't be trying to use the custom_class for input at all. I will have to redesign and get back to you again. Apologies.