depage / depage-forms

depage-forms is PHP library for HTML form generation with focus on usability. It is part of the upcoming version of depage-cms, but it also works as a standalone library. By abstracting HTML, browser flaws (duplicate form submissions) and form validation, it provides a comfortable way to obtain reliable and validated data from users.
http://docs.depage.net/depage-forms/
22 stars 10 forks source link

how can I make fields mandatory depending on another value #16

Open bwl21 opened 7 years ago

bwl21 commented 7 years ago

In the following form, I would like to make the subsequent fields mandatory only if user choses "folgende Anschrift"

How can I achieve this? I tried to register a validator in the fields but this only fires if user has entered something there.

And even if, how can I create a more specific message to tell the user, what is wrong.

screenshot_907

jonasjonas commented 7 years ago

Serverside, the easiest way is to subclass the HtmlForm class and override the onValidate function. I would also recommend defining your inputs in "addChildElements". There you can add a custom validator logic and also adjust the "required" attributes of your inputs.

If you use client side validation you have to write some custom logic in javascript.

bwl21 commented 7 years ago

I did not want to create an extra subclass of HtmlForm for every form i have. Your answer directed me to http://docs.depage.net/depage-forms/documentation/html/creditcard_8php_source.html where I found addChildElements

This is the code which generates the form. Do I get it right, that I could even add a validator to the field set $fs3?

  $fs3 = $firstForm->addFieldset("fs_bill", ARRAY("label" => $t->__("Rechnung")));

  $fs3->addSingle('person_bill', ARRAY('label' => $t->__('Rechnung bitte ausstellen an') ,
    'required' => true, "class"=>"vrinput form_8col alpha omega",
  //   'skin' => 'select',
    'list' => $person_bill
    )
  );

  //**** rechnungsanschrift

  $fs3->addText("billto_name",     ARRAY("label"=> $t->__("Name Rechnungsempfänger"), 'required' => false, "class"=>"vrinput form_8col alpha omega" ));
  $fs3->addText("billto_street",   ARRAY("label" => $t->__("Straße"), 'required' => false, "class"=>"vrinput form_8col alpha omega"));
  $fs3->addHtml('<div class="cf"></div>');
  $fs3->addText("billto_zip",      ARRAY("label" => $t->__("PLZ"), 'required' => false, "class"=>"vrinput form_2col alpha"));
  $fs3->addText("billto_city",     ARRAY("label" => $t->__("Ort"), 'required' => false, "class"=>"vrinput form_6col omega"));
  $fs3->addText("billto_country",  ARRAY("label" => $t->__("Land"), 'required' => false, "class"=>"vrinput form_8col alpha omega"));
  $fs3->addEmail("billto_email",   ARRAY("label" => $t->__("Email"), 'required' => false, "class"=>"vrinput form_8col alpha omega"));