Jump to:

1999 Posts in 1408 Topics by 613 members

Form Questions

SilverStripe Forums » Form Questions » [solved] Passing Errors to Custom Form

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

Page: 1
Go to End
Author Topic: 3328 Views
  • zenmonkey
    Avatar
    Community Member
    527 Posts

    [solved] Passing Errors to Custom Form Link to this post

    I've created a custom form that uses its own template, however the on submit errors don't seem to pass back into the form;

    Here is the submit action

    function submit($data, $form) {
    $application = new UserApplication();
       
        //Check Against Existing Approved Users
        $SQL_email = Convert::raw2sql($data['Email']);
       
        $existingMember = DataObject::get_one('Member', "Email = '$SQL_email'");
          if($existingMember) {
             if($existingMember->ID != $member->ID) {
                $form->addErrorMessage('Blurb',
                   _t(
                      'RegistrationForm.EMAILEXISTS',
                      'Sorry, that email address already exists. Please choose another.'
                   ),
                   'bad'
                );
                
                Director::redirectBack();
                return;
             }
          }
       
        //Create User Application
    $form->saveInto($application);
    $application->write();
       
        //E-mail Registration Form Contents:
       
        //Set data
       
        Director::redirect(Director::baseURL(). "new-user-registration-thank-you/");
    }

    and here is the template

    <form $FormAttributes class="container_12">
       <% if Message %>
          <p id="{$FormName}_error" class="message $MessageType">$Message</p>
       <% else %>
          <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
       <% end_if %>
       
       <div class="grid_4" id="User">
          <h3>Personal Information</h3>
          <p>Used to set up your ophoriatoys.com account</p>
          <fieldset>
             <div id="FirstName" class="field text">
                <label for="$FormName_FirstName">First name</label>
                <div class="middleColumn">
                $dataFieldByName(FirstName)
                </div>
             </div>
             
             <div id="Surname" class="field text">
                <label for="$FormName_Surname">Last Name</label>
                <div class="middleColumn">
                $dataFieldByName(Surname)
                </div>
             </div>
             
             <div id="Email" class="field email">
                <label for="$FormName_Email">Email</label>
                <div class="middleColumn">
                $dataFieldByName(Email)
                </div>
             </div>
             
             $dataFieldByName(Password)
             
             $dataFieldByName(SecurityID)
             
             $Title
          </fieldset>
    </div>
    <% if Actions %>
             <div class="Actions">
             <% control Actions %>$Field<% end_control %>
             </div>
             <% end_if %>
    </form>

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: [solved] Passing Errors to Custom Form Link to this post

    I think the first parameter of the addErrorForm relates to a div id (or could actually be a FormField name) to output the html error to. Try changing the 'Blurb' field to 'Email' instead and see if the error appears.

  • zenmonkey
    Avatar
    Community Member
    527 Posts

    Re: [solved] Passing Errors to Custom Form Link to this post

    Ahh the first Parameter is the Method Name so 'Blurb' needed to be changed to 'Message'. Thanks for pointing me in the right direction

  • PapaBear
    Avatar
    Community Member
    26 Posts

    Re: [solved] Passing Errors to Custom Form Link to this post

    I am still having trouble with this same issue. I have updated my $form->addErrorMessage line but I am still not getting the errors showing in the form.

       function SignupAction($data, $form) {
          // Check for the validation of member data
          // Email address must be unique.

          $email = Convert::raw2sql($data['Email']);
          if($member = DataObject::get_one("Addict", "`Email` = '$email'")) {
             // Add a error message
             $form->addErrorMessage("Message","Sorry, that email address already exists. Please choose another.","bad");

             // Load errors into session and post back
             Session::set("FormInfo.RegistrationForm_RegistrationForm.data", $data);

             // Redirect back to form
             Director::redirectBack();
             return;
          }

          //Username must be unique
          $username = Convert::raw2sql($data['Username']);
          if($member = DataObject::get_one("Addict", "`Username` = '$username'")) {
             // Add a error message
             $form->addErrorMessage("Message",'Sorry, that Username already exists. Please choose another.',"bad");

             // Load errors into session and post back
             Session::set("FormInfo.RegistrationForm_RegistrationForm.data", $data);

             // Redirect back to form
             Director::redirectBack();
             return;
          }

    And in the template:

    <% if Message %>
    <p id="{$FormName}_error" class="message $MessageType">$Message</p>
    <% else %>
    <p id="{$FormName}_error" class="message $MessageType" style="display: none"></p>
    <% end_if %>

    <p>Fields labeled <span class="required">in bold text</span> are required.</p>
    <fieldset>
    <div id="Email" class="field email required">
    <label class="" for="{$FormName}_Email">Email</label>
    $dataFieldByName(Email)
    </div>

    <div id="Username" class="field text required">
    <label class="" for="{$FormName}_Username">Addict Name: <span class="small">(This is an alias for you, your company or your alter ego. You WILL NOT be able to change this so please choose carefully.)</span></label>
    $dataFieldByName(Username)
    </div>

    Whether I put the first parameter as "Message" or as the appropriate fieldname ("Username" or "Email") I still get nothing updating in the form.

    Here is the form method from the page controller as well in case that is necessary...

       public function RegistrationForm(){
          $data = Session::get("FormInfo.RegistrationForm_RegistrationForm.data");
          $form = new RegistrationForm ( $this, "RegistrationForm" );
          if(is_array($data)) {
             $form->loadDataFrom($data);
          }
          return $form;
       }

    James.

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: [solved] Passing Errors to Custom Form Link to this post

    You shouldn't need to set / get the data yourself, this should be happening automatically. What I think may be happening is your adding the message to the session but then overriding it again went you set the data to the session. So try remove your custom session loading / setting stuff and see if the built in form API handles it.

  • PapaBear
    Avatar
    Community Member
    26 Posts

    Re: [solved] Passing Errors to Custom Form Link to this post

    Hi Willr. Thank you for such a prompt reply.

    I have made the changes you suggested. The form is correctly maintaining its contents but I am still not seeing any error messages coming through.

    Firebug is not reporting any errors. On the other required fields in the form the client-side validation seems to be working (if I skip over a required field I get the warning "Please fill out "this" it is required")

    This bug has been incredibly frustrating as it is holding up the delivery of the site and the client is champing at the bit *grin*

    James.

  • Rodolfo
    Avatar
    Community Member
    11 Posts

    Re: [solved] Passing Errors to Custom Form Link to this post

    Hi PapaBear, were you able to figure out what was the problem?

    I am currently dealing with the same issue.

    My Template

    <form $FormAttributes>
    <% if Message %>
    <p id="{$FormName}_error" class="message $MessageType">$Message</p>
    <% else %>
    <p id="{$FormName}_error" class="message $MessageType" >$Message</p>
    <% end_if %>

    <fieldset>
    <div id="FirstName" class="field text">
    <label class="left" for="$FormName_FirstName">First name</label>
    $dataFieldByName(FirstName)
    </div>

    <div id="Email" class="field email">
    <label class="left" for="$FormName_Email">Email</label>
    $dataFieldByName(Email)
    </div>

    $dataFieldByName(SecurityID)
    </fieldset>

    <% if Actions %>
    <div class="Actions">
    <% control Actions %>$Field<% end_control %>
    </div>
    <% end_if %>
    </form>

    My Form

    <?php
    class MyForm extends Form {

    function __construct($controller, $name) {
    $fields = new FieldSet(
    new TextField('FirstName', 'First name'),
    new EmailField('Email', 'Email address')
    );

    $actions = new FieldSet(
    new FormAction('submit', 'Submit')
    );

    $validator = new RequiredFields(
    'FirstName',
    'Email'
    );

    parent::__construct($controller, $name, $fields, $actions, $validator);
    }

    function forTemplate() {
    return $this->renderWith(array(
    $this->class,
    'Form'
    ));
    }

    function submit($data, $form) {
    $form->AddErrorMessage('Email', "Sorry, that email address already exists. Please choose another.", 'bad');
    Director::redirectBack();
    return;
    }

    }
    ?>

    But no error what so ever is displayed ?

    Any thoughts anyone ?

    3328 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.