开发者

Making tags in php form builder class

开发者_StackOverflow中文版I am using php form builder class

$form->addTextbox("Tags:", "thread_tags", "", array("required" => 1));

Does anyone know how you can validate tags (e.g. split by strings tag1, tag2, tag3) using this class? Do you do it after validation or before?

Obviously you can use preg_split, but where would you do it here? I want to make sure each tag is atleast 3 chars in length.


This isn't a nice form builder to work with, but here is how I managed to create the validation for the tag element.

Add a textbox element:

$form->addTextbox('Tags:', 'tags', "", array("required" => 1));

There is a method "bind([form object], [javascript condition], [php condition])", I think this method is to add a validation rule to an element.

$form->bind($form, '', 'validateTags()');

The third parameter is a function to validate the tag value. return true if the textbox contains correct data, or false if it's incorrect. Also, if the value is incorrect add an error message to the form error session and then return false.

function validateTags($form) {
    if (isset($_POST["tag"])) {
        $value = strip_tags($_POST["tag"]);
        $tags = explode(',', $value);
        foreach ($tags as $tag) {
            if (strlen($tag) < 3) {
                            // there might be a better way to do this!
                            // validation_2 == id of the form
                $_SESSION["pfbc-errors"]['validation_2']["errormsg"]['tag'] = "validation message....";
                return false;
            }
        }
        unset($_SESSION["pfbc-errors"]['validation_2']["errormsg"]['tag']);
    }
    return true;
}

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜