render() public method

@param string $template The output of the form can be generated automatically, can be given from a template file or can be generated programmatically by a callback function. For the automatically generated template there are two options: - when $template is an empty string or is "*vertical", the script will automatically generate an output where the labels are above the controls and controls come one under another (vertical view) - when $template is "*horizontal", the script will automatically generate an output where the labels are positioned to the left of the controls while the controls come one under another (horizontal view) When templates are user-defined, $template needs to be a string representing the path/to/the/template.php. The template file itself must be a plain PHP file where all the controls added to the form (except for the hidden controls, which are handled automatically) will be available as variables with the names as described in the documentation for each of the controls. Also, error messages will be available as described at {@link Zebra_Form_Control::set_rule() set_rule()}. A special variable will also be available in the template file - a variable with the name of the form and being an associative array containing all the controls added to the form, as objects. The template file must not contain the
and
tags, nor any of the controls added to the form as these are generated automatically!
There is a third method of generating the output and that is programmatically, through a callback function. In this case $template needs to be the name of an existing function. The function will be called with two arguments: - an associative array with the form's controls' ids and their respective generated HTML, ready for echo-ing (except for the hidden controls which will still be handled automatically); note that this array will also contain variables assigned through the {@link assign()} method as well as any server-side error messages, as you would in a custom template (see {@link Zebra_Form_Control::set_rule() set_rule()} method and read until the second highlighted box, inclusive) - an associative array with all the controls added to the form, as objects THE USER FUNCTION MUST RETURN THE GENERATED OUTPUT!
public render ( string $template = '', boolean $return = false, array $variables = '' ) : mixed
$template string
$return boolean (Optional) If set to TRUE, the output will be returned instead of being printed to the screen. Default is FALSE. @param array $variables (Optional) An associative array in the form of "variable_name" => "value" representing variable names and their associated values, to be made available in custom template files. This represents a quicker alternative for assigning many variables at once instead of calling the {@link assign()} method for each variable. @return mixed Returns or displays the rendered form.
$variables array
return mixed
function page_add_bookmark()
{
    $form = new Zebra_Form('form');
    $form->clientside_validation(array('close_tips' => true, 'on_ready' => false, 'disable_upload_validation' => true, 'scroll_to_error' => false, 'tips_position' => 'right', 'validate_on_the_fly' => true, 'validate_all' => true));
    $form->add('label', 'label_url', 'url', 'URL');
    $url = $form->add('text', 'url', 'http://');
    $url->set_rule(array('required' => array('url_error', 'URL musí být vyplněno.'), 'url' => array(true, 'url_error', 'Pole musí obsahovat platné URL (včetně protokolu).')));
    $form->add('label', 'label_title', 'title', 'Název stránky');
    $title = $form->add('text', 'title', '');
    $title->set_rule(array('required' => array('title_error', 'Název musí být vyplněn.')));
    $form->add('submit', 'submitbtn', 'Přidat');
    if ($form->validate()) {
        $ok = model_add($_POST['url'], $_POST['title'], array());
        if ($ok) {
            flash('info', 'Záložka byla vytvořena');
        } else {
            flash('error', 'Záložku se nepodařilo vytvořit.');
        }
        redirect_to('/');
    }
    // set('form', $form->render('views/add_form.php', true));
    set('form', $form->render('', true));
    set('title', 'Nová záložka');
    return html('add.html.php');
}
Example #2
0
/**
	Sets the formulary and returns the values of the fields
*/
function form()
{
    require_once './forms/Zebra_Form.php';
    // instantiate a Zebra_Form object
    $form = new Zebra_Form('form');
    // "submit"
    $form->add('submit', 'btnBack', 'Volver a generar otra consulta');
    // if the form is valid
    if ($form->validate()) {
        header("Location: sentwittmentForm.php");
        exit;
        // otherwise
    } else {
        // generate output using a custom template
        $form->render('*vertical');
    }
}
Example #3
0
/**
	Sets the formulary and returns the values of the fields
*/
function form()
{
    require_once './forms/Zebra_Form.php';
    // instantiate a Zebra_Form object
    $form = new Zebra_Form('form');
    $form->clientside_validation(true);
    // the label for the "query" element
    $form->add('label', 'label_query', 'query', 'Query');
    // add the "query" element
    $obj =& $form->add('text', 'query', '', array('autocomplete' => 'off'));
    // set rules
    $obj->set_rule(array('required' => array('error', 'Se necesita una query')));
    // "Return Per page"
    $form->add('label', 'label_returnpp', 'returnpp', 'Numero de Tweets');
    $obj =& $form->add('text', 'returnpp', 10, array('autocomplete' => 'off'));
    $obj->set_rule(array('digits' => array('', 'error', 'El valor ha de ser un entero'), 'length' => array(0, 3, 'error', 'El numero es demasiado grande'), 'custom' => array('is_valid_number', 'error', 'El numero de Tweets debe ser menor de 500')));
    // "Result Type"
    $form->add('label', 'label_type', 'type', 'Tipo de resultado:');
    $obj =& $form->add('select', 'type', 'mixed');
    $obj->add_options(array('mixed' => 'Mixto', 'recent' => 'Reciente', 'popular' => 'Popular'), true);
    // "submit"
    $form->add('submit', 'btnsubmit', 'Buscar Tweets');
    // if the form is valid
    if ($form->validate()) {
        $values = array();
        foreach ($_POST as $key => $value) {
            if (strpos($key, 'name_') !== 0 && strpos($key, 'timer_') !== 0 && strpos($key, 'response_') !== 0) {
                $values[$key] = $value;
                // echo $key . " " . $values[$key] . "<br>";
            }
        }
        $_SESSION["values"] = $values;
        header("Location: sentwittment.php");
        exit;
        // otherwise
    } else {
        // generate output using a custom template
        $form->render('*horizontal');
    }
}
$form->add('label', 'label_room', 'room', 'Which room would you like to reserve:');
$obj = $form->add('radios', 'room', array('A' => 'Room A', 'B' => 'Room B', 'C' => 'Room C'));
$obj->set_rule(array('required' => array('error', 'Room selection is required!')));
// "extra"
$form->add('label', 'label_extra', 'extra', 'Extra requirements:');
$obj = $form->add('checkboxes', 'extra[]', array('flipchard' => 'Flipchard and pens', 'plasma' => 'Plasma TV screen', 'beverages' => 'Coffee, tea and mineral water'));
// "date"
$form->add('label', 'label_date', 'date', 'Reservation date');
$date = $form->add('date', 'date');
$date->set_rule(array('required' => array('error', 'Date is required!'), 'date' => array('error', 'Date is invalid!')));
// date format
// don't forget to use $date->get_date() if the form is valid to get the date in YYYY-MM-DD format ready to be used
// in a database or with PHP's strtotime function!
$date->format('M d, Y');
// selectable dates are starting with the current day
$date->direction(1);
$form->add('note', 'note_date', 'date', 'Date format is M d, Y');
// "time"
$form->add('label', 'label_time', 'time', 'Reservation time :');
$form->add('time', 'time', '', array('hours' => array(9, 10, 11, 12, 13, 14, 15, 16, 17), 'minutes' => array(0, 30)));
// "submit"
$form->add('submit', 'btnsubmit', 'Submit');
// if the form is valid
if ($form->validate()) {
    // show results
    show_results();
    // otherwise
} else {
    // generate output using a custom template
    $form->render('includes/custom-templates/reservation.php');
}
<h2>Basic file upload</h2>

<p>Once you upload the file, don't forget to look into the "tmp" folder inside the "examples" folder for the result.</p>

<?php 
// include the Zebra_Form class
require '../Zebra_Form.php';
// instantiate a Zebra_Form object
$form = new Zebra_Form('form');
// the label for the "file" element
$form->add('label', 'label_file', 'file', 'Upload Microsoft Word document');
// add the "file" element
$obj = $form->add('file', 'file');
// set rules
$obj->set_rule(array('required' => array('error', 'A Microsoft Word document is required!'), 'upload' => array('tmp', ZEBRA_FORM_UPLOAD_RANDOM_NAMES, 'error', 'Could not upload file!<br>Check that the "tmp" folder exists inside the "examples" folder and that it is writable'), 'filetype' => array('doc, docx', 'error', 'File must be a Microsoft Word document!'), 'filesize' => array(102400, 'error', 'File size must not exceed 100Kb!')));
// attach a note
$form->add('note', 'note_file', 'file', 'File must have the .doc or .docx extension, and no more than 100Kb!');
// "submit"
$form->add('submit', 'btnsubmit', 'Submit');
// validate the form
if ($form->validate()) {
    // do stuff here
    print_r('<pre>');
    print_r($form->file_upload);
    die;
}
// auto generate output, labels above form elements
$form->render();
// set rules
$obj->set_rule(array('required' => array('error', 'Name is required!')));
// "email"
$form->add('label', 'label_email', 'email', 'Your email address:');
$obj = $form->add('text', 'email', '', array('data-prefix' => 'img:public/images/letter.png'));
$obj->set_rule(array('required' => array('error', 'Email is required!'), 'email' => array('error', 'Email address seems to be invalid!')));
$form->add('note', 'note_email', 'email', 'Your email address will not be published.');
// "website"
$form->add('label', 'label_website', 'website', 'Your website:');
$obj = $form->add('text', 'website', '', array('data-prefix' => 'http://'));
$obj->set_rule(array('url' => array(true, 'error', 'Invalid URL specified!')));
$form->add('note', 'note_website', 'website', 'Enter the URL of your website, if you have one.');
// "subject"
$form->add('label', 'label_subject', 'subject', 'Subject');
$obj = $form->add('text', 'subject', '', array('data-prefix' => 'img:public/images/comment.png'));
$obj->set_rule(array('required' => array('error', 'Subject is required!')));
// "message"
$form->add('label', 'label_message', 'message', 'Message:');
$obj = $form->add('textarea', 'message');
$obj->set_rule(array('required' => array('error', 'Message is required!'), 'length' => array(0, 140, 'error', 'Maximum length is 140 characters!', true)));
// "submit"
$form->add('submit', 'btn_submit', 'Submit');
// if the form is valid
if ($form->validate()) {
    // show results
    show_results();
    // otherwise
} else {
    // generate output using a custom template
    $form->render('*horizontal');
}
a control's label is to the left of the control). Don't forget to check the "Template source" tab to see how it's done.</p>

<?php 
// include the Zebra_Form class
require '../Zebra_Form.php';
// instantiate a Zebra_Form object
$form = new Zebra_Form('form', 'post', '', array('autocomplete' => 'off'));
// the label for the "email" element
$form->add('label', 'label_email', 'email', 'Email');
// add the "email" element
$obj = $form->add('text', 'email');
// set rules
$obj->set_rule(array('required' => array('error', 'Email is required!'), 'email' => array('error', 'Email address seems to be invalid!')));
// "password"
$form->add('label', 'label_password', 'password', 'Password');
$obj = $form->add('password', 'password');
$obj->set_rule(array('required' => array('error', 'Password is required!'), 'length' => array(6, 10, 'error', 'The password must have between 6 and 10 characters!')));
// "remember me"
$form->add('checkbox', 'remember_me', 'yes');
$form->add('label', 'label_remember_me_yes', 'remember_me_yes', 'Remember me', array('style' => 'font-weight:normal'));
// "submit"
$form->add('submit', 'btnsubmit', 'Submit');
// if the form is valid
if ($form->validate()) {
    // show results
    show_results();
    // otherwise
} else {
    // generate output using a custom template
    $form->render('includes/custom-templates/tables-login.php');
}
$obj->set_rule(array('required' => array('error', 'Email is required!'), 'email' => array('error', 'Email address seems to be invalid!')));
// attach a note to the email element
$form->add('note', 'note_email', 'email', 'Please enter a valid email address. An email will be sent to this
    address with a link you need to click on in order to activate your account', array('style' => 'width:200px'));
// "password"
$form->add('label', 'label_password', 'password', 'Choose a password:'******'password', 'password');
$obj->set_rule(array('required' => array('error', 'Password is required!'), 'length' => array(6, 10, 'error', 'The password must have between 6 and 10 characters')));
$form->add('note', 'note_password', 'password', 'Password must be have between 6 and 10 characters.', array('style' => 'width: 180px'));
// "confirm password"
$form->add('label', 'label_confirm_password', 'confirm_password', 'Confirm password:'******'password', 'confirm_password');
$obj->set_rule(array('compare' => array('password', 'error', 'Password not confirmed correctly!')));
// "captcha"
$form->add('captcha', 'captcha_image', 'captcha_code');
$form->add('label', 'label_captcha_code', 'captcha_code', 'Are you human?');
$obj = $form->add('text', 'captcha_code');
$form->add('note', 'note_captcha', 'captcha_code', 'You must enter the characters with black color that stand
    out from the other characters', array('style' => 'width: 200px'));
$obj->set_rule(array('required' => array('error', 'Enter the characters from the image above!'), 'captcha' => array('error', 'Characters from image entered incorrectly!')));
// "submit"
$form->add('submit', 'btnsubmit', 'Submit');
// if the form is valid
if ($form->validate()) {
    // show results
    show_results();
    // otherwise
} else {
    // generate output using a custom template
    $form->render('includes/custom-templates/registration.php');
}
$obj = $form->add('checkboxes', 'method[]', array('email' => 'By e-mail', 'phone' => 'By phone', 'post' => 'By land mail'));
$obj->set_rule(array('required' => array('error', 'Please specify how you would like to be notified about promotional offers!'), 'dependencies' => array(array('notifications' => 'yes'), 'mycallback, 1')));
// "email"
$form->add('label', 'label_email', 'email', 'Your email address:');
$obj = $form->add('text', 'email');
$obj->set_rule(array('required' => array('error', 'Email is required!'), 'email' => array('error', 'Email address seems to be invalid!'), 'dependencies' => array(array('method' => 'email'), 'mycallback, 2')));
$form->add('note', 'note_email', 'email', 'Your email address will not be published.');
// "phone"
$form->add('label', 'label_phone', 'phone', 'Your telephone number:');
$obj = $form->add('text', 'phone');
$obj->set_rule(array('required' => array('error', 'Phone number is required!'), 'digits' => array('', 'error', 'Phone number must contain only digits!'), 'dependencies' => array(array('method' => 'phone'), 'mycallback, 3')));
$form->add('note', 'note_phone', 'phone', 'Enter your phone number using digits only');
// "post"
$form->add('label', 'label_post', 'post', 'Your postal address:');
$obj = $form->add('text', 'post');
$obj->set_rule(array('required' => array('error', 'Postal address is required!'), 'dependencies' => array(array('method' => 'post'), 'mycallback, 4')));
$form->add('note', 'note_post', 'post', 'Enter the address where the notifications about promotional offers should be delivered');
// "why"
$form->add('label', 'label_why', 'why', 'Please tell us why:');
$obj = $form->add('textarea', 'why');
$obj->set_rule(array('required' => array('error', 'Please leave us a message!'), 'dependencies' => array(array('notifications' => 'no'), 'mycallback, 5')));
// "submit"
$form->add('submit', 'btnsubmit', 'Submit');
// if the form is valid
if ($form->validate()) {
    // show results
    show_results();
    // otherwise
} else {
    $form->render('includes/custom-templates/example1-dependencies.php');
}
Example #10
0
// set rules
$obj->set_rule(array('required' => array('error', 'Name is required!')));
// "email"
$form->add('label', 'label_email', 'email', 'Your email address:');
$obj = $form->add('text', 'email', '', array('style' => 'width: 195px', 'data-prefix' => 'img:public/images/letter.png'));
$obj->set_rule(array('required' => array('error', 'Email is required!'), 'email' => array('error', 'Email address seems to be invalid!')));
$form->add('note', 'note_email', 'email', 'Your email address will not be published.');
// "website"
$form->add('label', 'label_website', 'website', 'Your website:');
$obj = $form->add('text', 'website', '', array('style' => 'width: 400px', 'data-prefix' => 'http://'));
$obj->set_rule(array('url' => array(true, 'error', 'Invalid URL specified!')));
$form->add('note', 'note_website', 'website', 'Enter the URL of your website, if you have one.');
// "subject"
$form->add('label', 'label_subject', 'subject', 'Subject');
$obj = $form->add('text', 'subject', '', array('style' => 'width: 400px', 'data-prefix' => 'img:public/images/comment.png'));
$obj->set_rule(array('required' => array('error', 'Subject is required!')));
// "message"
$form->add('label', 'label_message', 'message', 'Message:');
$obj = $form->add('textarea', 'message');
$obj->set_rule(array('required' => array('error', 'Message is required!'), 'length' => array(0, 140, 'error', 'Maximum length is 140 characters!', true)));
// "submit"
$form->add('submit', 'btnsubmit', 'Submit');
// if the form is valid
if ($form->validate()) {
    // show results
    show_results();
    // otherwise
} else {
    // generate output using a custom template
    $form->render('includes/custom-templates/contact.php');
}