depage-forms icon indicating copy to clipboard operation
depage-forms copied to clipboard

how can I make fields mandatory depending on another value

Open bwl21 opened this issue 8 years ago • 2 comments

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

bwl21 avatar Aug 24 '17 10:08 bwl21

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.

jonasjonas avatar Sep 01 '17 08:09 jonasjonas

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"));

bwl21 avatar Sep 01 '17 11:09 bwl21