stefangabos / Zebra_Form

A jQuery augmented PHP library for creating secure HTML forms and validating them easily
Other
98 stars 49 forks source link

add_error issue #41

Open userc479 opened 4 years ago

userc479 commented 4 years ago

Hi there,

Whilst I'm trying to figure out what's not working on the coding - I've seen there's a bit of coding issue on your documentation under the add_error() section.

It shows the following: ` // create a new form $form = new Zebra_Form('my_form');

// add a text control to the form
$obj = $form->add('text', 'my_text');

// make the text field required
$obj->set_rule(
     'required' => array(
        'error',            // variable to add the error message to
        'Field is required' // error message if value doesn't validate
     )
);

// don't forget to always call this method before rendering the form
if ($form->validate()) {

    // for the purpose of this example, we will do a custom validation
    // after calling the "validate" method.
    // for custom validations, using the "custom" rule is recommended instead

    // check if value's is between 1 and 10
    if ((int)$_POST['my_text']) < 1 || (int)$_POST['my_text']) > 10) {

        $form->add_error('error', 'Value must be an integer between 1 and 10!');

    } else {

        // put code here that is to be executed when the form values are ok

    }

}

//  output the form using an automatically generated template
$form->render();`

When the below works ( rules had array added and int has been changed to intval and added submit to make it work)

` // create a new form $form = new Zebra_Form('my_form');

// add a text control to the form
$obj = $form->add('text', 'my_text');

// make the text field required
$obj->set_rule(array(
     'required' => array(
        'error',            // variable to add the error message to
        'Field is required' // error message if value doesn't validate
     ))
);

 // "submit"
$form->add('submit', 'btnsubmit', 'Submit message.');

// don't forget to always call this method before rendering the form
if ($form->validate()) {

    // for the purpose of this example, we will do a custom validation
    // after calling the "validate" method.
    // for custom validations, using the "custom" rule is recommended instead

    // check if value's is between 1 and 10
    if (intval($_POST['my_text'] < 1) || (intval($_POST['my_text'] > 10))) {

        $form->add_error('error', 'Value must be an integer between 1 and 10!');

    } else {

        // put code here that is to be executed when the form values are ok

    }

}

// output the form using an automatically generated template $form->render();`