Rules Example

Formagic code

$form = new Formagic();

// rules can be assigned as pre-instantiated objects
$stringLength = new Formagic_Rule_StringLength(array(
    'min' => 2,
    'max' => 8,

// rules can also be assigned as item option
$form->addItem('input', 'myInput', array(
    'label' => 'Please enter something',
    'rules' => array('mandatory'),
    ->addItem('input', 'my2ndInput', array(
        'label' => 'Some input required',
        'rules' => new Formagic_Rule_Mandatory(array(
            'message' => 'Custom error message'
    ->addItem('input', 'inputWithLengthRule', array(
        'rules' => $stringLength,
        'label' => 'Two to eight characters'
    ->addItem('textarea', 'myTextarea', array(
        'label' => 'So much empty space...',
        'attributes' => array(
            'rows' => 5,
            'cols' => 75
    ->addItem('submit', 'mySubmit', array(
        'label' => 'Send'

// validate the form.
if ($form->validate()) {
    echo '<p>
        <strong>Form status:</strong><br />
        submitted and ok


echo $form->render();



By default, an asterisk is added to the label of each item that has a rule type mandatory assigned. A default description for this asterisk however is not included by default. Normally you would want to place this description at a fitting place in your website layout.

Leave a Reply

Your email address will not be published. Required fields are marked *