Jump to:

2002 Posts in 1422 Topics by 616 members

Form Questions

SilverStripe Forums » Form Questions » [solved] Showing Form Validation Messages above the form (instead of inline)

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1482 Views
  • Silverfish
    Avatar
    Community Member
    59 Posts

    [solved] Showing Form Validation Messages above the form (instead of inline) Link to this post

    Hi There,

    I would like to have a form that displays all validation messages in one box above the form instead of every message inline under their form-field.
    I saw that silverstripe even generates a

    <p id="Form_TestForm_error" class="message " style=""></p>

    paragraph at top of the form, so I wonder if there's a (simple) way to display messages there?

    Thanks for any hint!

    Regards
    S.F.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: [solved] Showing Form Validation Messages above the form (instead of inline) Link to this post

    $form->sessionMessage('hello world', 'good');

    Edit: If you're thinking of disabling js validation, otherwise you'll have some js coding to do, you can get all validation errors from $form->validator->getErrors().

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: [solved] Showing Form Validation Messages above the form (instead of inline) Link to this post

    Thx Devlin 4 your answer.

    Ok, so I know how to add a message to the form. Now have to look for a way to only add it if validation of the form failed and to get all the (custom?) error messages (from requiredFields?) in there.

    I am searching the docs and the net for hors for something like

    if ( $form->is_valid() ) {
    foreach($form->RequriedFields() as $no => $field) {
    messages[] = $field->errorMessage;
    }

    without any results.

    Seems like i have to do the whole validation myself / customized just to get the errore messages somewhere else?

    Still grateful for any hints

    Regads
    S.F.

  • Devlin
    Avatar
    Community Member
    215 Posts

    Re: [solved] Showing Form Validation Messages above the form (instead of inline) Link to this post

    The form won't call the submit method unless all required fields are correct validated. To do what you want the 'simple way', you can just parse the error messages in your page controller and call it via your template.

    class FormPage extends Page{
       function showFormErrors() {
          $errors = Session::get('FormInfo.FormName.errors');
          $_errors = array();
          if( !empty($errors) && is_array($errors) ) foreach($errors AS $error) {
             $_errors[] = $error['message'];
          }
          Session::clear('FormInfo.FormName.errors');
          return !empty($_errors) ? "<ul><li>".implode("</li><li>", $_errors)."</li></ul>" : false;
       }
    }

    Or if have a form subclass you can add your own validation method.

    class MyForm extends Form{
       function validate() {
          parent::validate();

          if ($this->validator) {
             $errors = $this->validator->getErrors();
             $data = $this->getData();

             // edit
             $_errors = array();
             if( !empty($errors) && is_array($errors) ) foreach($errors AS $error) {
                $_errors[] = $error['message'];
             }

             if( !empty($_errors) ) {
                $this->sessionMessage(implode(', ', $_errors), 'bad');
                return false;
             }
          }
          return true;
       }
    }

    * Code not tested

  • Silverfish
    Avatar
    Community Member
    59 Posts

    Re: [solved] Showing Form Validation Messages above the form (instead of inline) Link to this post

    Hi Delvin,

    since I also have to do some custom validation I chose the 2nd, subclassing method. Works perfect!

    Thanks a lot for saving the rest of my Day!

    Regards
    SF

    1482 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.