Zebra_Form::add_error PHP Method

add_error() public method

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();
public add_error ( string $error_block, string $error_message ) : void
$error_block string The name of the error block to append the error message to (also the name of the PHP variable that will be available in the template file). @param string $error_message The error message to append to the error block. @return void
$error_message string
return void
    function add_error($error_block, $error_message)
    {
        // if the error block was not yet created, create the error block
        if (!isset($this->errors[$error_block])) {
            $this->errors[$error_block] = array();
        }
        // if the same exact message doesn't already exists
        if (!in_array(trim($error_message), $this->errors[$error_block])) {
            // append the error message to the error block
            $this->errors[$error_block][] = trim($error_message);
        }
    }