|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Adding rules to select elements.
Hi
I would like to validate a select element add with html_quick form. The first entry of the select should be either a blank string or some text stating 'select a collection' I cant see how I would apply a rule to insure an option is selected. Code:
$form->addRule('collection', 'Please provide a collection', 'required', null, 'client');
The required rule would be: if the select option = 'select a collection' dont validate Last edited by tallberg : November 2nd, 2007 at 08:49 AM. |
|
#2
|
|||
|
|||
|
Another one.
How do you add a rule to the file element to enforce file type? Thanks |
|
#3
|
|||
|
|||
|
The 'required' rule compares the selected values against '' (= empty string). Therefore, you just have to assign an empty string as the value to the items that you identify as not valid. Otherwise, you'll need a custom rule.
For mime type checks, you can use the 'mimetype' rule: http://pear.php.net/manual/en/package.html.html-quickform.intro-validation.php |
|
#4
|
|||
|
|||
|
Quote:
Thanks for the reply im new to this pear stuff. here is what i got for the jpg validation. Not working $jpgArray = Array('0' => 'image/jpe', '1' => 'image/jpeg', '2' => 'image/jpg'); $form->addRule('file', 'File must be jpg', 'mimetype', $jpgArray, 'server'); And for the select element i take your point about required. this is my select element $form->addElement('select', 'collection', 'Collection', $collection_options, 'class=selectbox'); the first value of the $collection_options array is 0 => ' ' which is blank string but the value is 0 so the form validates which not what i want. thank again. |
|
#5
|
|||
|
|||
|
$form->addRule('file'
should be $form->addRule('collection' The first parameter of addRule() is the name of an element. |
|
#6
|
|||
|
|||
|
Quote:
I get that. "file" is the name i gave to the file element. collection is the select element. Ive sort out the select. still cant get file working. Ive changed the name to see if that helps and it made not difference. Here it is. Code:
$jpgArray = Array('0' => 'image/jpe', '1' => 'image/jpeg', '2' => 'image/jpg');
$form->addRule('uploadjpg', 'File must be jpg', 'mimetype', $jpgArray, 'server');
Last edited by tallberg : November 2nd, 2007 at 12:19 PM. |
|
#7
|
|||
|
|||
|
Okay, let's see:
If you have only numeric values in your select element, you can use the 'nonzero' rule which doesn't validate on 0. You should be able to combine this rule with the 'required' rule to get the star symbol. About the mime type problem: The code looks right, but maybe the detected mime type differs from the specified types. You can check this by outputting the $_FILES array in your script. The key 'type' holds the mime type that QF checks in the 'mimetype' rule. |
|
#8
|
|||
|
|||
|
Quote:
Thank very much for your help. I found a solution to my problem. First line of the options array. $photo_collection[''] = 'Select a collection'; There is no value so the form does not validate as needed. Regarding the file element validation. The element: $form->addElement('file', 'uploaded_file', "<span class='fieldtitle'>JPG file:</span>", 'class=fileupload'); The rules: $form->addRule('uploaded_file','Please upload a file','uploadedfile'); $form->addRule('uploaded_file','Please upload a JPG file', 'mimetype', Array('0' => 'image/jpe', '1' => 'image/jpeg', '2' => 'image/jpg')); Hopefully the end. |
![]() |
| Viewing: Codewalkers Forums > PHP Related > PEAR Packages > Adding rules to select elements. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|