Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML_QuickForm Form Rules

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi there,

I have a form built using HTML_QuickForm which has no mandatory fields. However, this means that the form can be submitted with a set of empty values. Ideally, what I would like to do is have the client-side validation kick-in to say "hey, you haven't filled anything in". Now, I've noticed that custom validation can be applied to elements and element groups, but how would I add a general validation to check that at least one field in the form is populated, so that when the call to $form->validate() is made it runs this check also.

Does anyone have any ideas?

Your advice would be much appreciated!

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Hi Clive

there are quite a few ways to skin this cat. is the form made up exclusively of text input boxes?
 
At present, the form is solely made up of "select" drop-down boxes (with the possible inclusion of text inputs further down the line).

Incidentally, the form is serving as a basic search facility.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
but "selects" always send something back to the server.

two questions:
1. are all the selects in a group (if not, can they be?)
2. by "emtpy" do you mean that the selects return an "empty-equivalent" value?
 
1. The selects are not currently part of a group - but I guess they can be - the only thing that relates all of them is the fact that they are search criteria!

2. By empty, I mean an empty string:
Code:
<select name="dateCollected[d]">
  [b]<option value="">Select...</option>[/b]
  <option value="1">01</option>
  <option value="2">02</option>
  ...

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
if you can assign them all to a group then you can apply a custom group rule.

the alternative is to assign the same custom rule to each of the select elements and have a counter built into the custom rule. I feel that this is not a good solution

if you need the rule to be client-side then i would apply some custom JS to the form.submit() action that iterates over the select boxes. not technically a quickform operation....

to make it a bit more quickformish you could:
+ create a text input element in a group with the first select element.
+ assign a css class to the text element that renders its display property to none.
+ assign a required and non-empty validation rule to the text input element and make it client side.
+ assign some JS to the first select box that puts some text into the input text box when the first select box is something other than "select ..." and removes the text when the user resets the form or otherwise changes back the select option.

there are probably better options, i have a feeling that i've missed something obvious (within the constraints of quickform)
 
Thanks for your suggestions jpadie.

I actually stumbled across a quickform method called addFormRule with the following description:
This should be used when you want to add a rule involving several fields or if you want to use some completely custom validation for your form. The rule function/method should return TRUE in case of successful validation and array('element name' => 'error') when there were errors.
Source:
See the example below:
Code:
$form->addFormRule('check_search_criteria_exists');
Code:
function check_search_criteria_exists($values) {
    
    if ((empty($values['sampleType'])) &&
        (empty($values['reasonColl'])) &&
        (empty($values['failureMode']))) {
      return array('submit' => 'Please select at least one search criteria');
    } else {
      return true;
    } 
         
  }

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
As an addendum to the above, I have found the link below to be a very useful resource and it was actually this resource that alerted me to the addFormRule method. I then googled "addFormRule" and came up with the page linked above.


Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
interesting. i wonder when that got added to the API. thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top