Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Form Questions /

[solved] Passing Errors to Custom Form


Reply


7 Posts   3610 Views

Avatar
zenmonkey

Community Member, 528 Posts

15 January 2010 at 10:53am

Edited: 16/01/2010 4:26am

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>

Avatar
Willr

Forum Moderator, 5513 Posts

15 January 2010 at 12:01pm

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.

Avatar
zenmonkey

Community Member, 528 Posts

16 January 2010 at 4:25am

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

Avatar
PapaBear

Community Member, 26 Posts

17 June 2010 at 2:53pm

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.

Avatar
Willr

Forum Moderator, 5513 Posts

17 June 2010 at 3:01pm

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.

Avatar
PapaBear

Community Member, 26 Posts

17 June 2010 at 3:12pm

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.

Avatar
Rodolfo

Community Member, 11 Posts

19 May 2011 at 11:30pm

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 ?