PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
HTML_QUICK_FORM Rules
Goto page 1, 2  Next
 
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML
View previous topic :: View next topic  
Author Message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Fri Aug 10, 2007 9:52 am    Post subject: HTML_QUICK_FORM Rules Reply with quote

Hi all,

i am a newb in using PEAR and want to ask you a questions.
I want to use the HTML_QUICK_FORM pakage. When i created a form having two fields:

$a[] = &HTML_QuickForm::createElement('text', 'answer', "Answer:",array_merge(array("style"=>"width:270px;","value"=>$row["Answer"]),$cls_tb));

and

$a[] = &HTML_QuickForm::createElement('text', 'points', 'Points',array_merge(array("value"=>$row["points"]),$cls_tb_klein));


what can i do to look if when "answer" has no text "Points" has to be '0'?
I found no possible rule for that. How can i do it? How can i get the value of an Element?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Fri Aug 10, 2007 3:57 pm    Post subject: Reply with quote

There are several possibilities:
- "no text" can be checked by a "required" rule
- the 0 can be checked via a custom rule that is very similar to the bundled "required" rule

If both fields are in group, you might need to add a group rule.

If both rules are depending on each other, addFormRule() is your friend.
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Mon Aug 13, 2007 9:52 am    Post subject: THNX Reply with quote

Thnx i will try it... Very Happy

But one question,

is required not only for fields who have to be filled? And if not filled you get an error "plz write something into field"?

For my form it should be possible to let this field empty?!
Did i missunderstand this rule "required"?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Mon Aug 13, 2007 11:47 am    Post subject: Re: THNX Reply with quote

Pear_juice wrote:
But one question,

is required not only for fields who have to be filled? And if not filled you get an error "plz write something into field"?


Of course, yes. But you were talking about "no text".
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Mon Aug 13, 2007 12:24 pm    Post subject: Reply with quote

Oh,sorry.

maybe it was not easy to understand. English is not my first language.

So, my problem is another. When someone writes no text into the first field (he is allowed to) i want to check that the other field (only numeric) has to be a "0".

Thnx
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Mon Aug 13, 2007 1:16 pm    Post subject: Reply with quote

Okay, in that case, the last sentence of my first reply is the right way. (=> addFormRule(), or a group rule if you have one)
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Tue Aug 14, 2007 3:25 pm    Post subject: Reply with quote

Hi again,

I tryed this:

function cmpPoints($val)
{
for($b = 1; $b <=$countQuestions; $b++){
for($a = 3;$a <= $countAnswers; $a++){
if ($val["questionNr_".$b]["answer_".$a] == "" && $val["questionNr_".$b]["points_".$a] != '0') {
return array("question_".$b => 'For empty field Points have to be 0.');
}
}
}
return true;
}

$form->addFormRule('cmpPoints');

but nothing happens after pressing the submit button. What is my problem?
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Tue Aug 14, 2007 10:20 pm    Post subject: Reply with quote

I don't see where e.g. $countQuestions and $countAnswers are defined in your function? The loops will not be evaluated without these variables.

P.S.: Please use the {code} / {/code} (with square brackets) tags here in the forum, they make your code more readable.
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Wed Aug 15, 2007 1:08 pm    Post subject: Reply with quote

Ok,

the $countQuestions and $countAnswers are a problem. i changed it. i now take the values from hidden fields. thnx for that.

I changed many things and i have so many questions Embarassed

How does this work:

i have 2 loops as you have seen and i need to return a array like this:
Code:
return array('question_' .$b => 'Text.', 'question_' .$b => 'Text 2.',...);


so i tried this:
Code:

$errors;
loop1
  loop2
     $errors += "'frage_' .$b => 'text.',";
return array($errors);


but it wont work!?

I also have a problem with checking textfields:

Code:
if ($fields['antwort_'.$a] == "" && $fields['punkte_'.$a] != '0')


do i ask the right things to check an empty field and the number of another field???


this is my whole function:

Code:

function cmpPoints($fields)
{
   $errors = "";
   for($b = 1; $b <=$fields[anzahl_fragen]; $b++){
      for($a = 3;$a <= $fields[anzahl_antworten]; $a++){
           if ($fields['antwort_'.$a] == "" && $fields['punkte_'.$a] != '0') {
          $errors += "'frage_' .($b-1) => 'text.'";
          
       }
     }
  }
  if (errors != ""){
     return array($errors);
  }else{
     return true;
  }
}


Thnx
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Wed Aug 15, 2007 1:22 pm    Post subject: Reply with quote

I see German array keys in your code. If you speak German, it might be easier to proceed on pear-forum.de (I'm also registered there, and there some more QF users active there).

Anyway,
Code:
$fields[anzahl_fragen]
should be written as
Code:
$fields['anzahl_fragen']
to avoid a PHP notice. Another notice will be generated for this check:
Code:
if (errors != ""){


Try to replace it by
Code:
if ($errors != ""){
and your code might work then.

Code:
$error += ...
is also wrong. You need to use
Code:
$error[] = ...


BTW, using error_reporting = E_ALL should help to find some problems yourself.
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Wed Aug 15, 2007 2:42 pm    Post subject: Reply with quote

Thnx for help.
I tried to register at pear-forum.de but everytime i got an error message and could not use my email again. so i tried all my 3 Email adresses. no more Emails to register Laughing

And the Email to the webmaster could not be send. Also error Confused
____________________


--> You need to use Code:
Code:
$error[] = ...


i tried with

Code:
$errors .=


and the result looks good when give out the text via "echo"

Code:
echo $errors;


but

Code:
return array($errors);


wont work. If i take the echo output

Code:
return array('frage_' .(1-1) => 'text',...);


and write into the array, it works. Question
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Wed Aug 15, 2007 8:49 pm    Post subject: Reply with quote

Pear_juice wrote:
I tried to register at pear-forum.de but everytime i got an error message and could not use my email again. so i tried all my 3 Email adresses. no more Emails to register Laughing


If you want, send me an email (wiesemann(at)php.net; gerne auf Deutsch *g*) next time, and I'll try to forward if for you to the forum admin.

Pear_juice wrote:
i tried with

Code:
$errors .=


and the result looks good when give out the text via "echo"

Code:
echo $errors;


Well, yes, for an echo call you need a string. QF however, expects an array with the field name as the index and the error message as the value.

Not sure, whether all question are now answered!? If not, ask again please.
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Thu Aug 16, 2007 1:17 pm    Post subject: Reply with quote

Thnx, thats cool. I wrote you an Email (natürlich in deutsch Very Happy ).

Now 90% works perfect. The only problem is the return value. I think its because of the quotes.

Code:
return array('frage_' .(1-1) => text 1.','frage_' .(3-1) => text 2.',);


works but

Code:
$errors[1] .= "'frage_' .($b-1) => text x.',";
return array($errors[1])


wont work because of the quotes. but without the quotes (" ") i can´t allocate the text to the array.

what can i do?

Here is my whole function:

Code:
function cmpPoints($fields)
{
   
   $errors;
   for($b = 1; $b <=$fields['anzahl_fragen']; $b++){
      for($a = 3;$a <= $fields['anzahl_antworten']; $a++){
      
       if (strlen($fields["antworten_zu_frage_".($b-1)]["antwort_".($a-1)]) == 0 && $fields["antworten_zu_frage_".($b-1)]["punkte_".($a-1)] != 0) { 
          $errors[1] .= "'frage_' .($b-1) => 'Text.',";
       }
     }
  }
  if($errors[1] != ""){
      return array($errors[1]);
     
   }else{
    return true;
   }
}


Thnx!!!
Back to top
View user's profile Send private message
mark



Joined: 07 Jan 2007
Posts: 1048

PostPosted: Thu Aug 16, 2007 2:05 pm    Post subject: Reply with quote

Code:
$errors;

should maybe be
Code:
$errors = array();


The main problem is that you need to stick with arrays. Don't event new string notation please. Wink

For example, you could replace
Code:
$errors[1] .= "'frage_' .($b-1) => 'Text.',";

by
Code:
$errors['frage_' .($b-1)] = 'Text.';


This will assign 'Text.' as the error message for the form field with the name 'frage_'.($b-1). If you need to append more text in another iteration of the loop, you can use ".=" instead of "=". To avoid a notice in the first iteration, you should maybe collect the errors for one element in a simple string, and put this string into the array once all possible errors for an element were checked. (I haven't had a closer look at your checks, so the last advice might not be needed.)

At the end, you could do something like this:
Code:

if (count($errors)) {
    return $errors;
}
return true;
Back to top
View user's profile Send private message
Pear_juice



Joined: 10 Aug 2007
Posts: 9

PostPosted: Thu Aug 16, 2007 2:06 pm    Post subject: Reply with quote

Oh, i think i got it. Wink

Everything works fine. Thnx for all. Maybe the next questions will appear in a few days Laughing

My final function:

Code:
function cmpPoints($fields)
{
   
   $errors;
   for($b = 1; $b <=$fields['anzahl_fragen']; $b++){
      for($a = 3;$a <= $fields['anzahl_antworten']; $a++){
       if (strlen($fields["antworten_zu_frage_".($b-1)]["antwort_".($a-1)]) == 0 && $fields["antworten_zu_frage_".($b-1)]["punkte_".($a-1)] != 0) { 
         $errors['frage_'.($b-1)] = Text.';
       }
     }
  }
  if(count($errors)<1) $errors = true;
  return $errors;
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> HTML All times are GMT + 2 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software