public function NewEntryForm() { // Create fields $entry = new GuestbookEntry(); $labels = $entry->fieldLabels(); $fields = new FieldList(new TextField('Name', $labels['Name']), new EmailField('Email', $labels['Email']), TextField::create('Website', $labels['Website'])->setAttribute('type', 'url'), new TextareaField("Message", $labels['Message'])); if ($this->EnableEmoticons) { $smileyButtons = $this->SmileyButtons("Form_NewEntryForm_Message"); $smileyField = new LiteralField("Smileys", $smileyButtons); $fields->add($smileyField); } // Create actions $actions = new FieldList(new FormAction('postEntry', _t("GuestbookController.POST", 'Post'))); $validator = new RequiredFields('Name', 'Message'); $form = new Form($this, 'NewEntryForm', $fields, $actions, $validator); $form->setRedirectToFormOnValidationError(true); if ($this->UseSpamProtection) { if (Form::has_extension('FormSpamProtectionExtension')) { $form->enableSpamProtection(); } else { $message = _t('GuestbookController.SPAMPROTECTIONNOTINSTALLED', 'Spam protection has been enabled, but no spam protection module is installed!'); $form->setMessage($message, 'warning'); } } return $form; }
/** * Get the form for the page. Form can be modified by calling {@link updateForm()} * on a UserDefinedForm extension. * * @return Form|false */ public function Form() { $fields = $this->getFormFields(); if (!$fields || !$fields->exists()) { return false; } $actions = $this->getFormActions(); // get the required fields including the validation $required = $this->getRequiredFields(); // generate the conditional logic $this->generateConditionalJavascript(); $form = new Form($this, "Form", $fields, $actions, $required); $form->setRedirectToFormOnValidationError(true); $data = Session::get("FormInfo.{$form->FormName()}.data"); if (is_array($data)) { $form->loadDataFrom($data); } $this->extend('updateForm', $form); if ($this->DisableCsrfSecurityToken) { $form->disableSecurityToken(); } $this->generateValidationJavascript($form); return $form; }
/** * Post a comment form * * @return Form */ public function CommentsForm() { $usePreview = $this->getOption('use_preview'); $nameRequired = _t('CommentInterface.YOURNAME_MESSAGE_REQUIRED', 'Please enter your name'); $emailRequired = _t('CommentInterface.EMAILADDRESS_MESSAGE_REQUIRED', 'Please enter your email address'); $emailInvalid = _t('CommentInterface.EMAILADDRESS_MESSAGE_EMAIL', 'Please enter a valid email address'); $urlInvalid = _t('CommentInterface.COMMENT_MESSAGE_URL', 'Please enter a valid URL'); $commentRequired = _t('CommentInterface.COMMENT_MESSAGE_REQUIRED', 'Please enter your comment'); $fields = new FieldList($dataFields = new CompositeField(TextField::create("Name", _t('CommentInterface.YOURNAME', 'Your name'))->setCustomValidationMessage($nameRequired)->setAttribute('data-msg-required', $nameRequired), EmailField::create("Email", _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)"))->setCustomValidationMessage($emailRequired)->setAttribute('data-msg-required', $emailRequired)->setAttribute('data-msg-email', $emailInvalid)->setAttribute('data-rule-email', true), TextField::create("URL", _t('CommentingController.WEBSITEURL', "Your website URL"))->setAttribute('data-msg-url', $urlInvalid)->setAttribute('data-rule-url', true), TextareaField::create("Comment", _t('CommentingController.COMMENTS', "Comments"))->setCustomValidationMessage($commentRequired)->setAttribute('data-msg-required', $commentRequired)), HiddenField::create("ParentID"), HiddenField::create("ReturnURL"), HiddenField::create("ParentCommentID"), HiddenField::create("BaseClass")); // Preview formatted comment. Makes most sense when shortcodes or // limited HTML is allowed. Populated by JS/Ajax. if ($usePreview) { $fields->insertAfter(ReadonlyField::create('PreviewComment', _t('CommentInterface.PREVIEWLABEL', 'Preview'))->setAttribute('style', 'display: none'), 'Comment'); } $dataFields->addExtraClass('data-fields'); // save actions $actions = new FieldList(new FormAction("doPostComment", _t('CommentInterface.POST', 'Post'))); if ($usePreview) { $actions->push(FormAction::create('doPreviewComment', _t('CommentInterface.PREVIEW', 'Preview'))->addExtraClass('action-minor')->setAttribute('style', 'display: none')); } // required fields for server side $required = new RequiredFields($this->config()->required_fields); // create the comment form $form = new Form($this, 'CommentsForm', $fields, $actions, $required); // if the record exists load the extra required data if ($record = $this->getOwnerRecord()) { // Load member data $member = Member::currentUser(); if (($record->CommentsRequireLogin || $record->PostingRequiredPermission) && $member) { $fields = $form->Fields(); $fields->removeByName('Name'); $fields->removeByName('Email'); $fields->insertBefore(new ReadonlyField("NameView", _t('CommentInterface.YOURNAME', 'Your name'), $member->getName()), 'URL'); $fields->push(new HiddenField("Name", "", $member->getName())); $fields->push(new HiddenField("Email", "", $member->Email)); } // we do not want to read a new URL when the form has already been submitted // which in here, it hasn't been. $form->loadDataFrom(array('ParentID' => $record->ID, 'ReturnURL' => $this->request->getURL(), 'BaseClass' => $this->getBaseClass())); } // Set it so the user gets redirected back down to the form upon form fail $form->setRedirectToFormOnValidationError(true); // load any data from the cookies if ($data = Cookie::get('CommentsForm_UserData')) { $data = Convert::json2array($data); $form->loadDataFrom(array("Name" => isset($data['Name']) ? $data['Name'] : '', "URL" => isset($data['URL']) ? $data['URL'] : '', "Email" => isset($data['Email']) ? $data['Email'] : '')); // allow previous value to fill if comment not stored in cookie (i.e. validation error) $prevComment = Cookie::get('CommentsForm_Comment'); if ($prevComment && $prevComment != '') { $form->loadDataFrom(array("Comment" => $prevComment)); } } if (!empty($member)) { $form->loadDataFrom($member); } // hook to allow further extensions to alter the comments form $this->extend('alterCommentForm', $form); return $form; }
/** * Post a comment form * * @return Form */ public function CommentsForm() { $member = Member::currentUser(); $fields = new FieldList(new TextField("Name", _t('CommentInterface.YOURNAME', 'Your name')), new EmailField("Email", _t('CommentingController.EMAILADDRESS', "Your email address (will not be published)")), new TextField("URL", _t('CommentingController.WEBSITEURL', "Your website URL")), new TextareaField("Comment", _t('CommentingController.COMMENTS', "Comments")), new HiddenField("ParentID"), new HiddenField("ReturnURL"), new HiddenField("BaseClass")); // save actions $actions = new FieldList(new FormAction("doPostComment", _t('CommentInterface.POST', 'Post'))); // required fields for server side $required = new RequiredFields(array('Name', 'Email', 'Comment')); // create the comment form $form = new Form($this, 'CommentsForm', $fields, $actions, $required); // if the record exists load the extra required data if ($record = $this->getOwnerRecord()) { $require_login = Commenting::get_config_value($this->getBaseClass(), 'require_login'); $permission = Commenting::get_config_value($this->getBaseClass(), 'required_permission'); if (($require_login || $permission) && $member) { $fields = $form->Fields(); $fields->removeByName('Name'); $fields->removeByName('Email'); $fields->insertBefore(new ReadonlyField("NameView", _t('CommentInterface.YOURNAME', 'Your name'), $member->getName()), 'URL'); $fields->push(new HiddenField("Name", "", $member->getName())); $fields->push(new HiddenField("Email", "", $member->Email)); $form->setFields($fields); } // we do not want to read a new URL when the form has already been submitted // which in here, it hasn't been. $url = isset($_SERVER['REQUEST_URI']) ? Director::protocolAndHost() . '' . $_SERVER['REQUEST_URI'] : false; $form->loadDataFrom(array('ParentID' => $record->ID, 'ReturnURL' => $url, 'BaseClass' => $this->getBaseClass())); } // Set it so the user gets redirected back down to the form upon form fail $form->setRedirectToFormOnValidationError(true); // load any data from the cookies if ($data = Cookie::get('CommentsForm_UserData')) { $data = Convert::json2array($data); $form->loadDataFrom(array("Name" => isset($data['Name']) ? $data['Name'] : '', "URL" => isset($data['URL']) ? $data['URL'] : '', "Email" => isset($data['Email']) ? $data['Email'] : '', "Comment" => Cookie::get('CommentsForm_Comment'))); } if ($member) { $form->loadDataFrom($member); } // hook to allow further extensions to alter the comments form $this->extend('alterCommentForm', $form); return $form; }