Merx

Validate Checkout

To make sure you get all the data you need from your customers, Merx uses Kirby’s page validation. You can define the required fields and set validate options for each field you want to validate. See Kirby’s documentation for all available validators.

You don’t have to check if the customers cart is valid.

Custom Order Blueprint

You can copy the default order blueprint or start from scratch. Add your own fields and add the validation options you like.

Example

Let’s assume you need a given name which has to have at least two characters but at the most three words.

site/blueprints/pages/order.yml

title: Order

fields:
  givenName:
    type: text
    required: true
    validate:
      minLength: 2
      maxWords: 3
$merx->initializePayment([
  'givenName' => 'Elizabeth Alexandra Mary',
]); // ok

$merx->initializePayment([
  'givenName' => 'Karl-Theodor Maria Nikolaus Johann Jacob Philipp Franz Joseph Sylvester',
]); // not ok (throws Exception)

Conditional Errors

Sometimes you need something like if field A is set, check field B. This can’t be solved with the blueprint validation.

To get conditional field errors, extend the errors method of the OrderPage class with the help of the addFieldError method.

site/models/order.php

class OrderPage extends OrderPageAbstract {
  public function errors(): array
  {
    $errors = parent::errors();
    if (!$this->useInvoiceAddressAsShippingAddress()->toBool()) {
      $rule = ['minLength' => 3, 'maxLength' => 255];
      $shippingAddressGivenNameError = Merx::getFieldError($this->shippingAddressGivenName(), $rule);
      $errors = array_merge($errors, $shippingAddressGivenNameError);
    }
    return $errors;
  }
}

Display Errors

The initializePayment method throws an error if the validation fails.

try {
  $redirect = merx()->initializePayment($data);
} catch (Kirby\Exception\Exception $ex) {
  // do something with $ex.
}

$ex->getDetails() for example will get you something like this.

"name": {
  "label": "Name",
  "message": {
    "required": "Please enter something"
  }
}

See the documentation of initializePayment for all possible Exceptions.