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:
- Site\Extensions\AddressExtension
SilverShop\Cart\ShoppingCartController:
extensions:
- Site\Extensions\ShoppingCartControllerExtension
Site\Templates\DeferedRequirements:
custom_requirements:
SilverShop\Page\AccountPageController:

View File

@ -43,6 +43,7 @@ const FormValidateField = (($) => {
const $field = $el.closest('.field');
const extraChecks = $el.data(`${NAME}-extra`);
let valid = true;
let msg = null;
// browser checks + required
if (!ui._element.checkValidity() ||
@ -51,6 +52,12 @@ const FormValidateField = (($) => {
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
if (extraChecks) {
extraChecks.forEach((check) => {
@ -63,21 +70,32 @@ const FormValidateField = (($) => {
return true;
}
this.setError(scrollTo);
this.setError(scrollTo, msg);
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 $field = $(ui._element).closest('.field');
const pos = $field.offset().top;
$field.addClass('error');
if (msg) {
$field.append('<div class="message alert alert-error alert-danger">' + msg + '</div>');
}
if (scrollTo) {
$field.focus();
$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
{
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',
[
ArrayData::create([
'Message' => $message,
'Type' => $type,
]
])
);
}
public function getSiteWideMessage()
{
$session = $this->getRequest()->getSession();
$message = $session->get('SiteWideMessage');
$session->clear('SiteWideMessage');
if (!$this->site_message) {
$session = $this->getRequest()->getSession();
$this->site_message = $session->get('SiteWideMessage');
$session->clear('SiteWideMessage');
}
return $message;
return $this->site_message;
}
public function CurrentTime()

View File

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