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

EditProfileForm not holding correct values after submit wiht errors in form


Reply

3 Posts   748 Views

Avatar
danzzz

3 May 2011 at 1:58am Community Member, 175 Posts

hi,

I have a form where users can edit their profile.

public function EditProfileForm() {

   $fields = new FieldSet(
    new DropdownField('salutation', 'Anrede',singleton('Member')->dbObject('salutation')->enumValues()),
    new TextField('FirstName','Vorname'),
    new TextField('Surname','Nachname'),
    new TextField('street','Straße, Hnr.'),
    new TextField('zip','PLZ'),
    new TextField('city','Ort'),
    new DropdownField('country', 'Land',singleton('Member')->dbObject('country')->enumValues()),
    new TextField('telephone', 'Telefonnummer')
   );

   $birthdayfield = new DateField('birthday','Geburtstag');
   $birthdayfield->setConfig('dmyfields', true );
   $fields->push($birthdayfield);

   $actions = new FieldSet(
    new FormAction('SaveEditProfileForm','Speichern')
   );

   $validator = new RequiredFields('salutation','FirstName','Surname');

   $Form = new Form($this, 'EditProfileForm', $fields, $actions, $validator);

   $Member = Member::CurrentMember();
$Form->loadDataFrom($Member->data());

return $Form;

}

This form works as expacted, but not when there is an error, for example no FirstName ...
There are also other fields on the form, which are not required. An example:
The user fills out all fields and deletes FirstName before submit, so there will be an error because FirstName is required.

The error appears, but all the fields the user filled out are empty again after submit.

How to avoid this? It's bad that the user must start again filing out the form.

I think this line must be changed when it's a post?
$Form->loadDataFrom($Member->data());

thx

daniel

Avatar
swaiba

3 May 2011 at 11:43pm Forum Moderator, 1796 Posts

in EditProfileForm(...

      if(Session::get("FormData"))
      {
         $previous_value = Session::get("FormData");
         Session::clear("FormData");
         $form->loadDataFrom($previous_value);
      }

in SaveEditProfileForm...

      Session::set("FormData", $data);

Avatar
danzzz

7 May 2011 at 3:06am Community Member, 175 Posts

thx, did it that way ...