Message-ID: <1131844438.379192.1711699247212.JavaMail.root@confluence-doc2-production> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_379191_116028299.1711699247206" ------=_Part_379191_116028299.1711699247206 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html HelperForm

HelperForm

Table of contents

=20 =20

HelperForm

This helper is used to generate an edition form for an object of type Ob= jectModel. Example: editing the client's profile.

Form declaration

Fields inside [brackets] are optional as per the HTML standard.
Value= s between {curly braces} list the possible values for this field.

=20
$this-&g=
t;fields_form =3D array(   =20
  'legend' =3D> array(       =20
    'title' =3D> $this->l('Edit carrier'),                     // Thi=
s is the name of the fieldset, which can contain many option fields
    'image' =3D> '../img/admin/icon_to_display.gif'            // The ic=
on must, if there is one, must be of the size 16*16
  ),   =20
  'input' =3D> array(       =20
    array(           =20
      'type' =3D> {'text', 'select', 'textarea', 'radio', 'checkbox', 'f=
ile', 'shop', 'asso_shop', 'free', 'color'},
      ['label'] =3D> $this->l('Shipping method'),              // The=
oretically optional, but in reality each field has to have a label
      'name' =3D> 'shipping_method',                           // The na=
me of the object property from which we get the value
      ['required'] =3D> {true, false},                         // If tru=
e, PrestaShop will add a red star next to the field
      ['desc'] =3D> $this->l('Description displayed under the field')=
,
      ['hint'] =3D> $this->l('Invalid characters:').' <>;=3D#{}=
' // This is displayed when the mouse hovers the field.
      ['suffix'] =3D> 'kg'                                     // This i=
s displayed after the field (ie. to indicate the unit of measure)
      ['options'] =3D> array(                                  // This i=
s only useful if type =3D=3D select
        'query' =3D> $array_of_rows,                           // $array=
_of_rows must contain an array of arrays, inner arrays (rows) being mode of=
 many fields=09
        'id' =3D> 'id_carrier',                                // The ke=
y that will be used for each option "value" attribute
      ),
      ['values'] =3D> array(                                   // This i=
s only useful if type =3D=3D radio
        array(
          'id' =3D> 'active_on',
          'value' =3D> 1,
          'label' =3D> $this->l('Enabled')
        ),
        array(
          'id' =3D> 'active_off',
          'value' =3D> 0,
          'label' =3D> $this->l('Disabled')
        )
      ),
      [is_bool] =3D> {true, false},                            // This i=
s only useful if type =3D=3D radio. It display a "yes or no" choice.
      ['empty_message'] =3D> $this->l('To be displayed when the field=
 is empty'),
      ['lang'] =3D> {true, false},                             // Is the=
 field multilang?
     ),
    array(         =20
      //another field     =20
    ),   =20
  ),
  'submit' =3D> array(
    'title' =3D> $this->l('   Save   '),                       // Thi=
s is the button that saves the whole fieldset.
    'class' =3D> 'button'   =20
  )
);
=20

If you want to use the "color" type, you can add the "color mColor= PickerInput" classes

Basic declaration

Removing all the optional fields, this is how to build a basic HelperFor= m element:

=20
$this-&g=
t;fields_form =3D array(
  'legend' =3D> array(       =20
    'title' =3D> $this->l('Edit carrier'),       =20
    'image' =3D> '../img/admin/icon_to_display.gif'   =20
  ),   =20
  'input' =3D> array(       =20
    array(           =20
      'type' =3D> 'text',
      'name' =3D> 'shipping_method',
     ),
  ),
  'submit' =3D> array(
    'title' =3D> $this->l('Save'),       =20
    'class' =3D> 'button'   =20
  )
);
=20

This specific code generates this HTML code (simplified here for readabi= lity reasons):

=20
<form=
 id=3D"_form">
  <fieldset id=3D"fieldset_main_conf">
    <legend>
      <img alt=3D"Edit carrier" src=3D"../img/admin/icon_to_display.gif"=
>Edit carrier
    </legend>
    <div class=3D"margin-form">
      <input type=3D"text" class=3D"" value=3D"" id=3D"shipping_method" =
name=3D"shipping_method">
    </div>
    <div class=3D"clear"></div>
    <div class=3D"margin-form">
      <input type=3D"submit" class=3D"button" name=3D"" value=3D"Save" i=
d=3D"_form_submit_btn">
    </div>
  </fieldset>
</form>
=20

Generating specific elemen= ts

The 'input' variable of the form declaration takes an array containing t= he content of your form. Using the various offered possibilities, you can b= uild just about any type of form, and be assured that it will comply with P= restaShop's style and form processing.

You can use as many element arrays as necessary for your form, one after= the other.

Text input

Here is how to generate a basic <input> element:

=20
array(
  'type'     =3D> 'text',                                // This is a re=
gular <input> tag.
  'label'    =3D> $this->l('Name'),                      // The =
<label> for this <input> tag.
  'name'     =3D> 'name',                                // The content =
of the 'id' attribute of the <input> tag.
  'size'     =3D> 50,                                    // The content =
of the 'size' attribute of the <input> tag.
  'required' =3D> true,                                  // If set to tr=
ue, this option must be set.
  'desc'     =3D> $this->l('Please enter your name.')    // A help te=
xt, displayed right next to the <input> tag.
),
=20

Selector

Here is how to generate a <select> element:

=20
array(
  'type' =3D> 'select',                              // This is a <se=
lect> tag.
  'label' =3D> $this->l('Shipping method:'),         // The <label=
> for this <select> tag.
  'desc' =3D> $this->l('Choose a shipping method'),  // A help text, =
displayed right next to the <select> tag.
  'name' =3D> 'shipping_method',                     // The content=
 of the 'id' attribute of the <select> tag.
  'required' =3D> true,                              // If set to true, =
this option must be set.
  'options' =3D> array(
    'query' =3D> $options,                           // $options contain=
s the data itself.
    'id' =3D> 'id_option',                           // The value of the=
 'id' key must be the same as the key for 'value' attribute of the <opti=
on> tag in each $options sub-array.
    'name' =3D> 'name'                               // The value of the=
 'name' key must be the same as the key for the text content of the <opt=
ion> tag in each $options sub-array.
  )
),
=20

The content of the selector is stored in the $options varia= ble, which is an array of arrays. It must contain two keys: id and name.

$options can take this value:

=20
$options=
 =3D array(
  array(
    'id_option' =3D> 1,                 // The value of the 'value' attr=
ibute of the <option> tag.
    'name' =3D> 'Method 1'              // The value of the text content=
 of the  <option> tag.
  ),
  array(
    'id_option' =3D> 2,
    'name' =3D> 'Method 2'
  ),
);
=20

...but of course, you would be better off generating such an array of ar= rays yourself, from the data stored in PrestaShop. For instance, here is ho= w to display a gender (social title) selector:

=20
$options=
 =3D array();
foreach (Gender::getGenders((int)Context::getContext()->language->id)=
 as $gender)
{
  $options[] =3D array(
    "id" =3D> (int)$gender->id,
    "name" =3D> $gender->name
  );
}
=20

Checkbox

Here is how to generate a <input> of type "checkbox":=

=20
array(
  'type'    =3D> 'checkbox',                         // This is an <i=
nput type=3D"checkbox"> tag.
  'label'   =3D> $this->l('Options'),                // The <label=
> for this <input> tag.
  'desc'    =3D> $this->l('Choose options.'),        // A help text, =
displayed right next to the <input> tag.
  'name'    =3D> 'options',                          // The content of t=
he 'id' attribute of the <input> tag.
  'values'  =3D> array(
    'query' =3D> $options,                           // $options contain=
s the data itself.
    'id'    =3D> 'id_option',                        // The value of the=
 'id' key must be the same as the key for 'value' attribute of the <opti=
on> tag in each $options sub-array.
    'name'  =3D> 'name'                              // The value of the=
 'name' key must be the same as the key for the text content of the <opt=
ion> tag in each $options sub-array.
  ),
),
=20

Just as for a selector input, check boxes take an array of arrays as the= value of $options.

Radio button

Here is how to generate a <input> of type "radio":

=20
array(
  'type'      =3D> 'radio',                               // This is an =
<input type=3D"checkbox"> tag.
  'label'     =3D> $this->l('Enable this option'),        // The <=
label> for this <input> tag.
  'desc'      =3D> $this->l('Are you a customer too?'),   // A help t=
ext, displayed right next to the <input> tag.
  'name'      =3D> 'active',                              // The co=
ntent of the 'id' attribute of the <input> tag.
  'required'  =3D> true,                                  // If set to t=
rue, this option must be set.
  'class'     =3D> 't',                                   // The content=
 of the 'class' attribute of the <label> tag for the <input> ta=
g.
  'is_bool'   =3D> true,                                  // If set to t=
rue, this means you want to display a yes/no or true/false option.
                                                        // The CSS styling =
will therefore use green mark for the option value '1', and a red mark for =
value '2'.
                                                        // If set to false,=
 this means there can be more than two radio buttons,=20
                                                        // and the option l=
abel text will be displayed instead of marks.
  'values'    =3D> array(                                 // $value=
s contains the data itself.
    array(
      'id'    =3D> 'active_on',                           // The content=
 of the 'id' attribute of the <input> tag, and of the 'for' attribute=
 for the <label> tag.
      'value' =3D> 1,                                     // The content=
 of the 'value' attribute of the <input> tag.=09
      'label' =3D> $this->l('Enabled')                    // The <=
label> for this radio button.
    ),
    array(
      'id'    =3D> 'active_off',
      'value' =3D> 0,
      'label' =3D> $this->l('Disabled')
    )
  ),
),
=20

Note that you have to use the "t" CSS class on your labels = in order to have the proper styling (but you can redefine that class using = the "class" variable).

Other HTML elements

The type variable of the element declaration makes it possi= ble to generate just about any kind of <input> element: = text, select, textarea, radio<= /code>, checkbox, file and many others! See the l= ist of available types here: http= s://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input
You can= also use some PrestaShop specific: shop, asso_shop, free, color. Try them out!

------=_Part_379191_116028299.1711699247206--