Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to add notification after submitted the "Post Discussion", "Post Comment" buttons

luasoftluasoft New
edited January 2015 in Vanilla 2.0 - 2.8

I'm writing a plugin, there, i have added a textbox in the New Discussion form.

for example:

public function PostController_AfterDiscussionFormOptions_Handler($Sender) {
      echo '<input type="text" name="Tag" id="Tag" >';
}

and then, i will check the data after submitted.

like this:

public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
      $Tag = $_REQUEST['Tag'];
      if ($this->IsValid($Tag)) {
         // insert into database
      } else {
         // show error message
         // $error = 'Tag is not empty';
      }
}

I want after submitted, my error message and default vanilla error message will be displayed together.

like this:

Body is required.

Category is required.

Name is required.

Tag is not empty <- my error message

How can i do that?

Thanks!

Comments

  • R_JR_J Ex-Fanboy Munich Admin

    Easy:

    $Sender->Validation->AddValidationResult('Tag', T('your error message'));
    

    But... ;)

    1.) Look at the DiscussionModel when the BeforeSave event is fired:

            $this->EventArguments['FormPostValues'] = &$FormPostValues;
            $this->EventArguments['DiscussionID'] = $DiscussionID;
            $this->FireEvent('BeforeSaveDiscussion');
    

    The values of the form are in $Sender->EventArguments['FormPostValues'] and so you can reference your field as $Sender->EventArguments['FormPostValues']['Tag'].

    2.) I don't know what your requirements are, but you can do some simple validations right out of the box (look at class.validation.php):
    $Sender->Validation->ApplyRule('Tag', 'Required', T('Custom error message')); If you leave out the custom error message, Vanilla shows the default "Tag is required."

    3.) You can also build your own custom "Rule":

    ...
    public function DiscussionModel_BeforeSaveDiscussion_Handler($Sender) {
        $Sender->Validation->AddRule('Tag', 'function:TagValidation');
    }
    ...
    } // <- end of class!
    
    function TagValidation($Tag) {
        if ($Tag == 'SuperTag!')
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
  • great! that's what i need.
    Thanks @R_J‌ ;)

  • You should really use the form object not hard code

    echo $Sender->Form->TextBox('Tag');
    

    This will ensure consistency.

    grep is your friend.

  • also don’t do things like

    $Tag = $_REQUEST['Tag'];
    

    There is was ways of checking to post if you, need to get the post value(s). Work with the framework.

    grep is your friend.

  • luasoftluasoft New
    edited January 2015

    @x00 said:
    You should really use the form object not hard code

    echo $Sender->Form->TextBox('Tag');
    

    This will ensure consistency.

    but it's not working, got the error:

    Undefined property: DiscussionModel::$Form

    Call to a member function TextBox() on null

    i used:

    $Sender->EventArguments['FormPostValues']['Tag'];
    

    and it's ok.

    thank you.

  • Most of the time $Sender is a Gdn_Controller, which has a form, but sometimes it is a Gdn_Model or something different.

    In that case, you can always use Gdn::Controller() to get the current controller.

    Gdn::Controller()->Form->TextBox('Tag');
    
  • @Bleistivt said:
    Most of the time $Sender is a Gdn_Controller, which has a form, but sometimes it is a Gdn_Model or something different.

    In that case, you can always use Gdn::Controller() to get the current controller.

    Gdn::Controller()->Form->TextBox('Tag');
    

    I think the problem is he used the form builder as a getter. The form builder was supposed to be used the the controller/view hook PostController_AfterDiscussionFormOptions_Handler, not for the post back.

    It is this line that is wrong echo '<input type="text" name="Tag" id="Tag" >';

    grep is your friend.

Sign In or Register to comment.