From b89c8ed1df6be3a7aff383165cd2c511145d4d04 Mon Sep 17 00:00:00 2001 From: Shaun Adlam Date: Fri, 8 May 2015 23:49:28 +0200 Subject: [PATCH] Add function $form->add_error_to_field Link error message to a spesific field same as clientside validation $form = new Zebra_Form('my_form'); $obj = $form->add('text', 'my_text'); if ($form->validate()) { if ((int)$_POST['my_text']) < 1 || (int)$_POST['my_text']) > 10) { $form->add_error_to_field('my_text', 'Value must be an integer between 1 and 10!'); } $form->render(); * @param string $field_id The Unique name to identify the control where to attach the error block * * @param string $error_message The error message to append to the error block. * */ --- Zebra_Form.php | 270 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 269 insertions(+), 1 deletion(-) diff --git a/Zebra_Form.php b/Zebra_Form.php index ddccd6d..04f887e 100644 --- a/Zebra_Form.php +++ b/Zebra_Form.php @@ -517,7 +517,275 @@ function add_error($error_block, $error_message) $this->errors[$error_block][] = trim($error_message); } + + /** + * Link error message to a spesific field same as clientside validation + * + * + * // 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_to_field('my_text', 'Value must be an integer between 1 and 10!'); + * + * } else { + * + * // put code here that is to be executed when the form values are ok + * + * } + * + * } + * + * // Link error message to a spesific field same as clientside validation + * $form->render(); + * + * + * @param string $field_id The Unique name to identify the control where to attach the error block + * + * @param string $error_message The error message to append to the error block. + * + * @return void + */ + function add_error_to_field($field_id,$error_message) + { + echo ""; + } /** * Set the server path and URL to the "process.php" and "mimes.json" files. * @@ -4946,4 +5214,4 @@ function _zebra_form_show_error($message, $type) } -?> \ No newline at end of file +?>