Shopping cart extension, ElementForm fix, URL field validation

This commit is contained in:
Tony Air 2018-12-05 14:16:26 +01:00
parent ff87ba7f16
commit fdc3bb1465
5 changed files with 64 additions and 19 deletions

View File

@ -12,6 +12,10 @@ SilverShop\Model\Address:
extensions: extensions:
- Site\Extensions\AddressExtension - Site\Extensions\AddressExtension
SilverShop\Cart\ShoppingCartController:
extensions:
- Site\Extensions\ShoppingCartControllerExtension
Site\Templates\DeferedRequirements: Site\Templates\DeferedRequirements:
custom_requirements: custom_requirements:
SilverShop\Page\AccountPageController: SilverShop\Page\AccountPageController:

View File

@ -43,6 +43,7 @@ const FormValidateField = (($) => {
const $field = $el.closest('.field'); const $field = $el.closest('.field');
const extraChecks = $el.data(`${NAME}-extra`); const extraChecks = $el.data(`${NAME}-extra`);
let valid = true; let valid = true;
let msg = null;
// browser checks + required // browser checks + required
if (!ui._element.checkValidity() || if (!ui._element.checkValidity() ||
@ -51,6 +52,12 @@ const FormValidateField = (($) => {
valid = false; valid = false;
} }
// validate URL
if ($el.hasClass('url') && $el.val().trim().length && !this.valideURL($el.val())) {
valid = false;
msg = 'URL must start with http:// or https://. For example: https://your-domain.com/';
}
// extra checks // extra checks
if (extraChecks) { if (extraChecks) {
extraChecks.forEach((check) => { extraChecks.forEach((check) => {
@ -63,21 +70,32 @@ const FormValidateField = (($) => {
return true; return true;
} }
this.setError(scrollTo); this.setError(scrollTo, msg);
return false; return false;
} }
setError(scrollTo = true) { valideURL(str) {
const regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
if (!regex.test(str)) {
return false;
} else {
return true;
}
}
setError(scrollTo = true, msg = null) {
const ui = this; const ui = this;
const $field = $(ui._element).closest('.field'); const $field = $(ui._element).closest('.field');
const pos = $field.offset().top; const pos = $field.offset().top;
$field.addClass('error'); $field.addClass('error');
if (msg) {
$field.append('<div class="message alert alert-error alert-danger">' + msg + '</div>');
}
if (scrollTo) { if (scrollTo) {
$field.focus(); $field.focus();
$Html.scrollTop(pos - 100); $Html.scrollTop(pos - 100);
} }
} }

View File

@ -0,0 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: tony
* Date: 6/23/18
* Time: 1:23 PM
*/
namespace Site\Extensions;
use SilverStripe\ORM\DataExtension;
class ShoppingCartControllerExtension extends DataExtension
{
public function updateAddResponse($request, $response, $product, $quantity)
{
\PageController::setSiteWideMessage('+'.$quantity.' item(s) was added into the cart', 'success', $request);
}
public function updateRemoveResponse($request, $response, $product, $quantity)
{
\PageController::setSiteWideMessage(''.$quantity.' item(s) was removed from the cart', 'success', $request);
}
}

View File

@ -9,24 +9,27 @@ use SilverStripe\ORM\FieldType\DBDatetime;
class PageController extends ContentController class PageController extends ContentController
{ {
public function setSiteWideMessage($message, $type) public static function setSiteWideMessage($message, $type, $request = null)
{ {
$this->getRequest()->getSession()->set( $request = $request ? $request : Controller::curr()->getRequest();
$request->getSession()->set(
'SiteWideMessage', 'SiteWideMessage',
[ ArrayData::create([
'Message' => $message, 'Message' => $message,
'Type' => $type, 'Type' => $type,
] ])
); );
} }
public function getSiteWideMessage() public function getSiteWideMessage()
{ {
$session = $this->getRequest()->getSession(); if (!$this->site_message) {
$message = $session->get('SiteWideMessage'); $session = $this->getRequest()->getSession();
$session->clear('SiteWideMessage'); $this->site_message = $session->get('SiteWideMessage');
$session->clear('SiteWideMessage');
}
return $message; return $this->site_message;
} }
public function CurrentTime() public function CurrentTime()

View File

@ -1,10 +1,6 @@
<div class="form-element__form $ExtraClass"> <div class="form-element__form $ExtraClass">
<div class="row"> <% if $Title && $ShowTitle %>
<div class="col-md-6"> <h2 class="form-element__title">$Title</h2>
<% if $Title && $ShowTitle %> <% end_if %>
<h2 class="form-element__title">$Title</h2> $Form
<% end_if %>
$Form
</div>
</div>
</div> </div>