Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

CustomForm

CodexiLab edited this page Jun 9, 2020 · 1 revision

New class Form (CustomForm)

What's new? Three new methods are added to build select, textarea and input fields. Deals with a better way to build form fields customized in benderbs/includes/frm/Form.form.class.php:

Examples of how to use

<select> select(array $attr, array $options)

Building our first select. For example, we want to add a new field select to our custom AlertForm (CustomAlertForm):

/**
 * Class AlertForm
 */
class CustomAlertForm extends CustomForm {

    public static function custom_select_example() {
        $attr = array(
            'id'    => 'lorem',
            'name'  => 'lorem',
            'class' => 'form-control'
        );

        $options = array(
            array('value' => 'lorem1', 'option' => 'Lorem 1'),
            array('value' => 'lorem2', 'option' => 'Lorem 2', 'selected' => 'selected')
        );
        
        parent::select($attr, $options);
        return true;
    }
  
}

The value assigned to 'option' => 'Lorem 1' or $options[0]['option'] = 'Lorem 1' will between <option>Lorem 1</option>.

CustomAlertForm::custom_select_example();

Output:

<select id="lorem" name="lorem" class="form-control">
    <option value="lorem1">Lorem 1</option>
    <option value="lorem2" selected="selected">Lorem 2</option>
</select>

<textarea> textarea(array $attr)

/**
 * Class AlertForm
 */
class CustomAlertForm extends CustomForm {
    public static function custom_textarea_example() {
        parent::textarea(array('id' => 'loremId', 'name' => 'loremId', 'value' => 'Hello world', 'class' => 'form-control'));
        return true;
    }
}

The value assigned to 'value' => 'Hello world' will between <textarea>Hello world</textarea>.

CustomAlertForm::custom_textarea_example();

Output:

<textarea id="loremId" name="loremId" class="form-control">Hello world</textarea>

<input type="text"> input(array('type' => 'text'))

/**
 * Class AlertForm
 */
class CustomAlertForm extends CustomForm {

    public static function input_text_example() {
        $attr = array(
            'type'  => 'text',
            'name'  => 'computer',
            'value' => '16',
            'class' => 'form-control'
        );
            
        parent::input($attr);
        return true;
    }

}
CustomAlertForm::input_text_example();

Output:

<input type="text" id="computer" name="computer" value="16" class="form-control">

<input type="email"> input(array('type' => 'email'))

This is applied in a real example:

/**
 * Class AlertForm
 */
class CustomAlertForm extends CustomForm {

    public static function default_email_text() {
        return __('Enter your e-mail');
    }

    public static function email_text() {
        $attr = array(
            'type'  => 'email',
            'name'  => 'alert_email',
            'value' => '',
            'class' => 'form-control form-control-light mb-2'
        );

        if (osc_logged_user_email() == '') $attr['placeholder'] = self::default_email_text();
            
        parent::input($attr);
        return true;
    }
    
}
CustomAlertForm::email_text();

Output:

<input type="email" name="alert_email" id="alert_email" value="" class="form-control form-control-light mb-2" placeholder="Enter your email">

If there is no user email logged in, simply the placeholder attribute will not appear, because it is dynamically conditioned.

<input type="hidden"> input(array('type' => 'hidden'))

/**
 * Class AlertForm
 */
class CustomAlertForm extends CustomForm {

    protected static function generic_input_hidden($name, $value)
    {
        self::input(array('type' => 'hidden', 'name' => $name, 'value' => $value));
    }

}
CustomAlertForm::generic_input_hidden('computerId', '16');

Output:

<input type="hidden" id="computerId" name="computerId" value="16">

<input type="password"> input(array('type' => 'password'))

<input type="checkbox"> input(array('type' => 'checkbox'))