 |
|
|
| View previous topic :: View next topic |
| Author |
Message |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Thu Jul 05, 2007 11:26 pm Post subject: Quickform - Select validation |
|
|
| Code: |
$state = array (
"1" => "AL",
"2" => "AK",
"3" => "AZ", .... ''51"=>"WY")
$form->addElement('select','state_id','State:', $state(), 'class="signupData"');
|
Hello all, Im a QuickForm Noob here.
The state array is coming from my table called states in my DB it has 51 records. How do I put a blank record in the beginning of the state array list? So, my first listing from my drop down box will be "SELECT STATE".
Also, how do I make sure the user select a valid "state"?
Is it possible to register a rule to make sure a state is selected and get the value of it using a callback function?
I cant seem to find an example on how to do this.
I appreciate your help in advance! |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Fri Jul 06, 2007 11:36 am Post subject: Re: Quickform - Select validation |
|
|
| imchaz wrote: | | The state array is coming from my table called states in my DB it has 51 records. How do I put a blank record in the beginning of the state array list? So, my first listing from my drop down box will be "SELECT STATE". |
You could simply add | Code: | | '' => 'SELECT STATE' | at the beginning of your array definition, e.g.:
| Code: |
$state = array('' => 'SELECT STATE', 1 => 'AL', ...);
|
| imchaz wrote: | | Also, how do I make sure the user select a valid "state"? |
If you don't expect users to fool you, a simple 'required' rule would be sufficient. If you do expect that, you could check the submitted value against the keys of your array, and use setElementError() if the value is invalid.
| imchaz wrote: | | Is it possible to register a rule to make sure a state is selected and get the value of it using a callback function? |
It is possible, but not needed. If you want, you could register your own rule (with a callback) and check the validity of the id this way.
To get the submitted value, you can just use e.g. exportValues(). |
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Fri Jul 06, 2007 4:27 pm Post subject: |
|
|
Mark, thanks for your response. I was able to reconstruct the array as you suggested.
I also made a required rule within the Select which I didnt know you can do that.
And, I was able to get my SELECT value using a callback function with the exportValue().
My follow questions are what are the differences with the "SAFE" and "RAW" descriptions here:
HTML_QuickForm::exportValue() -- Returns the element's "safe" value
HTML_QuickForm::exportValues() -- Returns "safe" elements' values
HTML_QuickForm::getElementValue() -- Returns the element's raw value
Also what did you mean by this:
| mark wrote: |
If you don't expect users to fool you, a simple 'required' rule would be sufficient. If you do expect that, you could check the submitted value against the keys of your array, and use setElementError() if the value is invalid. |
Again, thanks for your help. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Fri Jul 06, 2007 6:06 pm Post subject: |
|
|
| imchaz wrote: | My follow questions are what are the differences with the "SAFE" and "RAW" descriptions here:
HTML_QuickForm::exportValue() -- Returns the element's "safe" value
HTML_QuickForm::exportValues() -- Returns "safe" elements' values
HTML_QuickForm::getElementValue() -- Returns the element's raw value |
May I just point to you to the (longer) descriptions of these methods in the API docs?
http://pear.php.net/package/HTML_QuickForm/docs/latest/HTML_QuickForm/HTML_QuickForm.html
| imchaz wrote: |
Also what did you mean by this:
| mark wrote: |
If you don't expect users to fool you, a simple 'required' rule would be sufficient. If you do expect that, you could check the submitted value against the keys of your array, and use setElementError() if the value is invalid. |
|
For example, there exists a plugin for Firefox called "Web Developer Toolbar". This allows you to easily replace every select box by a simple text field. So, instead of selecting your predefinied values from the box, a user could enter whatever he wants. (This is only *one* example how people could submit "wrong" values.)
Checking the validity of the submitted value against the keys of states array is therefore a good idea. If you have only a few users that you can trust, and your application is not reachable by other people, you could skip this check. But the check is easy to write, anyway. |
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Sat Jul 07, 2007 11:05 am Post subject: |
|
|
| imchaz wrote: | | Do you know of any other tutorials on Quickform usage? |
No, I also know only the mentioned tutorials (without the last one). But they are all together quite extensive. And if you need further help, you know where you can ask.
| imchaz wrote: | | Im stuck using PHP4 as my host provider uses it and doesnt plan on migrating to PHP5. |
PHP 5 is now four years old. I'd consider going to another host if he isn't willing to install PHP 5.
With the new GoPHP5 project, you might not have access to new versions of popular PHP 5 applications in 2008. (http://www.gophp5.org/)
| imchaz wrote: | | In you opinion, do you think QUICKFORM is still the way to go? You have a suggestion? |
I'm biased (because I'm the author of HTML_QuickForm_Renderer_Tableless), but yes, I'd say that QF is still a good idea. Even on PHP 5 system, it is worth using it. Once QF2 is ready, one should consider switching to it, of course.
There are also other form package available, of course. It's IMHO a matter of personal preference and the features one needs, which packages one should choose. |
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Mon Jul 09, 2007 3:55 pm Post subject: |
|
|
| mark wrote: |
No, I also know only the mentioned tutorials (without the last one). But they are all together quite extensive. And if you need further help, you know where you can ask. |
Im so glad I found this forum knowing someone like you are monitoring the questions actively.
| mark wrote: |
I'm biased (because I'm the author of HTML_QuickForm_Renderer_Tableless), but yes, I'd say that QF is still a good idea. Even on PHP 5 system, it is worth using it. Once QF2 is ready, one should consider switching to it, of course.
There are also other form package available, of course. It's IMHO a matter of personal preference and the features one needs, which packages one should choose. |
Wow, the author...that is a bonus! |
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Mon Jul 09, 2007 8:56 pm Post subject: |
|
|
I get this error when I validate...
Notice: Array to string conversion in c:\php\php4\PEAR\HTML\QuickForm\Rule\Required.php on line 39
It references this function:
| Code: | function validate($value, $options = null)
{
if ((string)$value == '') {
return false;
}
return true;
} // end func validate
|
Can you tell me what I need to correct to make the validation on the HeirSelect to work properly.
It does prompt me to select a state so the $company array populates, but when I submit it the error above appears.
| Code: |
$state = array();
$company = array();
$state [0] = "Select State";
$state [1] = "AL";
$state [2] = "AK";
$state [3] = "AZ";
.....$state[51] = "WY";
$company[0][0] = "";
$company[1][1] = "ABC Company";
$company[1][2] = "DEF Company";
$company[2][3] = "GHI Company";
$company[2][4] = "JKL Company";
$company[3][5] = "MNO Company";
$company[3][6] = "PQR Company";
$company[3][7] = "STU Company;
$sel =& $form->addElement('hierselect', 'state_id', 'Select OCN Company:','class="signupData"','<br />');
$sel->setOptions(array($state,$company) );
$form->addRule('state_id','STATE selection is required','required',false,'client');
|
|
|
| Back to top |
|
 |
alex
Joined: 13 Sep 2006 Posts: 72
|
Posted: Tue Jul 10, 2007 3:13 pm Post subject: |
|
|
| First I would use the "nonzero" rule instead the "required" rule in your case. "Required" checks if a field is empty I think. |
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Tue Jul 10, 2007 3:36 pm Post subject: |
|
|
| alex wrote: | | First I would use the "nonzero" rule instead the "required" rule in your case. "Required" checks if a field is empty I think. |
What is the "nonzero" rule? Im not familiar with this. Can you elaborate?
Thanks for the response. |
|
| Back to top |
|
 |
alex
Joined: 13 Sep 2006 Posts: 72
|
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Tue Jul 10, 2007 5:08 pm Post subject: |
|
|
| Code: |
$sel =& $form->addElement('hierselect', 'state_id', 'Select OCN Company:','class="signupData"','<br />');
$sel->setOptions(array($state,$company) );
$form->addRule('state_id','STATE selection is required','nonzero'); |
Alex, I put in the addrule 'nonzero' instead of 'required' and when it attempts to validate
Im getting this error:
Warning: preg_match() expects parameter 2 to be string, array given in c:\php\php4\PEAR\HTML\QuickForm\Rule\Regex.php on line 57
I must be setting up something wrong. Do you have an example of making sure, your first hierselect option is selected before submitting a form?
Thanks again. |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Tue Jul 10, 2007 5:22 pm Post subject: |
|
|
'hierselect' is internally a group of select elements. Therefore, you need to use addGroupRule() instead of addRule().
Although you might not understand the text on the following link, you can still copy the last example:
http://www.pear-forum.de/fpost7159.html
('auswahl' is the name of the element, and 'Auswahl erforderlich!' is the error message [translation: 'selection requied']) |
|
| Back to top |
|
 |
imchaz
Joined: 05 Jul 2007 Posts: 31
|
Posted: Tue Jul 10, 2007 6:27 pm Post subject: |
|
|
| mark wrote: | 'hierselect' is internally a group of select elements. Therefore, you need to use addGroupRule() instead of addRule().
Although you might not understand the text on the following link, you can still copy the last example:
http://www.pear-forum.de/fpost7159.html
('auswahl' is the name of the element, and 'Auswahl erforderlich!' is the error message [translation: 'selection requied']) |
Mark, I looked at it and I guess Im still doing something wrong. Im such a noob at this.
This is what I have:
| Code: |
$sel =& $form->addElement('hierselect', 'state_id', 'Select OCN Company:','class="signupData"','<br />');
$sel->setOptions(array($state,$company) );
$form->addGroupRule( 'state_id', array( 'state_id' => array( 0 => array( 'Selection Required!', 'nonzero', null, 'client' ) ) ) ); |
Am I leaving something out? |
|
| Back to top |
|
 |
mark

Joined: 07 Jan 2007 Posts: 1051
|
Posted: Tue Jul 10, 2007 6:48 pm Post subject: |
|
|
Your example works here. I've used the array definitions that you've posted on "Mon Jul 09, 2007 20:56" and the rule definition from your last post. I'm then forced to select else than "Select State" before I can submit the form.
=> Check whether you have also changed something else. If you don't find the problem, then please show your complete code or at least the important parts of it. |
|
| 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
|
|