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

Please select a value within the list provided. 3 is not a valid option error


Go to End


4 Posts   2807 Views

Avatar
mi32dogs

Community Member, 75 Posts

4 January 2016 at 1:25pm

Hi
On an events page I have a reminder form, where people can select a date from a dropdown field and when they wand to be reminded.

This form renders fine, but when I submit the form get an error message below the Event Date whet say’s “Please select a value within the list provided. 3 is not a valid option”
but the dropdown only has one option “<option selected="selected" value="3">Sun, Jan 31, 2016 at 7:00 PM</option>” so 3 is a valid option.

Any idea what I’m doing wrong?

public function ReminderForm()
	{

		$form = BootstrapForm::create(
			$this,
			__FUNCTION__,
			FieldList::create(

				HiddenField::create('EventID', '', $this->currentEventID),
				DropdownField::create('EventDateID', 'Event Date',
					EventDate::get()->filter(
						array(
							'EventID' => $this->currentEventID,
							'Active' => 1)
					)->map('ID', 'ReminderDate')),
				DropdownField::create('Time', 'Reminder Time')->setSource(
					array(
						'BeforeOnsale' => 'Day Before Onsale',
						'Onsale' => 'Day Off Onsale',
						'BeforeEvent' => 'Day Before Event',
						'Event' => 'Day Off the Event'
					))->addExtraClass('required')->setEmptyString('(Select)'),
				TextField::create('Name', 'Name')->addExtraClass('required'),
				EmailField::create('Email', 'Email Address')->addExtraClass('required email')
			),
			FieldList::create(
				FormAction::create('handleReminder', 'Set Reminder')
					->setStyle('primary'),
				ResetFormAction::create('reset', 'Cancel')
					->setStyle('default')
			),
			RequiredFields::create('Name', 'Email')
		)
			->addExtraClass('form general-validate')
			->setLayout('horizontal')
			->setGridLabelClass('col-sm-3')
			->setGridInputClass('col-sm-9')
			->setGridActionClass('col-sm-offset-3 col-sm-9')
		;

		// load form state
		$data = Session::get("FormData.{$form->getName()}.data");

		return $data ? $form->loadDataFrom($data) : $form;

	}

Avatar
swaiba

Forum Moderator, 1899 Posts

4 January 2016 at 9:25pm

Well the (int)ID => (string)Date Time list is returning the (int) ID to the controller and then trying to assign that, I assume in the abscence of seeing the code, to a SS_Datetime field. Instead you might try...

EventDate::get()->filter(array(
	'EventID' => $this->currentEventID,
	'Active' => 1
))->map('ReminderDate', 'ReminderDate')),

..to set the datetime with a real date time. However this would allow hackers to change the values in the dropdown or using tamper data and assign any date, regardless of it's presence in EventDate. So maybe a link to the ID instead where you are saving it.

Avatar
mi32dogs

Community Member, 75 Posts

5 January 2016 at 6:07pm

Edited: 05/01/2016 6:15pm

Thanks Swaiba,

I eventDateID is saving to a Int,
I tried to map it to ->map('ReminderDate', 'ReminderDate') But I get the same error “Please select a value within the list provided. Sun, Jan 31, 2016 at 7:00PM is not a valid option” whatever I try I get the same error that it is not an option while it is.
The only way it works is if I hard code the array.

Are you not allowed to populate a frontend form from a dataObject?

Just for reference is the form handler and the dataObject it is saving too

public function handleReminder($data, $form)
	{
		// set form state
		Session::set("FormData.{$form->getName()}.data", $data);

		$output = Array();

		// check if there is already a reminder set for this event
		$existing = Reminder::get()->filter(array('EventDateID' => $data['EventDateID'], 'Email' => $data['Email'], 'EventID' => $data['EventID']))->count();
		if ($existing > 0) {
			$output['status'] = 'danger';
			$output['message'] = 'You already set a reminder for this event';

			if (Director::is_ajax()) {
				return json_encode($output);
			} else {
				$form->sessionMessage('You already set a reminder for this event', 'bad');
				return $this->redirectBack();
			}
		}

		$reminder = Reminder::create();
		$reminder->IP = $_SERVER['REMOTE_ADDR'];
		$form->saveInto($reminder);
		$reminder->write();

		// clear form state if success
		Session::clear("FormData.{$form->getName()}.data");

		$output['status'] = 'success';
		$output['message'] = 'You reminder is set';

		if (Director::is_ajax()) {
			return json_encode($output);
		} else {
			$form->sessionMessage('Thanks for your submission, your Reminder has been recorded', 'good');
			return $this->redirectBack();
		}

	}

Sorry I'm not able to post the Reminder DataObject it is getting blocked by cloudflare ( it is hard to get any code posted as it gets blocked a lot of the time)

CloudFlare Ray ID: 25fcaf82c5e3036e • Your IP: 76.175.62.34

Avatar
mi32dogs

Community Member, 75 Posts

7 January 2016 at 11:29am

Ok found the problem, I needed the eventID in the form as that was how I get the dates, so I called the form with the ID as a variable “getReminderform($ID)” form the template, this works fine on the initial load $ID is a variable but on submission it turns into a ss_HTTPRequest object.

So I just had to add a test to see if it is a install load or a form submission.