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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

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

EditProfileForm not holding correct values after submit wiht errors in form


Go to End


3 Posts   1304 Views

Avatar
danzzz

Community Member, 175 Posts

3 May 2011 at 1:58am

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

Forum Moderator, 1899 Posts

3 May 2011 at 11:43pm

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

Community Member, 175 Posts

7 May 2011 at 3:06am

thx, did it that way ...