Jump to content

Edit History

Roxayl

Roxayl

If you wish to display errors next to the input (at least on the V1 product page), you could try to display your error message the same way other the default form field validation errors are displayed:

public function hookActionObjectProductUpdateBefore(array $params): void
{
  /** @var \Product $product */
  $product = $params['object'];

  if ($notValidated) {
    $field = 'step1_reference'; // see the input name/id.
    $message = 'Your error message, whatever.';

    // Clean any output buffering done through the Symfony controller.
    ob_end_clean();
    // The product page JS code triggers an error message if a 400 HTTP code is returned.
    header('HTTP/1.1 400 Bad Request');
    // Display errors. You could display validation errors for multiple fields too. Apparently, no need to explicitly define the Content-Type.
    echo json_encode([$field => [$message]);
    exit;
  }
}

Which creates the following output:

image.png.24ca1872cef63fcca018697513e10417.png

There's probably a more elegant way do to that, idk. You should probably check that you are in the correct context before displaying your errors like that (e.g. AJAX validation through ProductController, etc etc).

Roxayl

Roxayl

If you wish to display errors next to the input (at least on the V1 product page), you could try to display your error message the same way other the default form field validation errors are displayed:

public function hookActionObjectProductUpdateBefore(array $params): void
{
  /** @var \Product $product */
  $product = $params['object'];

  if ($notValidated) {
    $field = 'step1_reference';
    $message = 'Your error message, whatever.';

    ob_end_clean();
    header('HTTP/1.1 400 Bad Request');
    echo json_encode([$field => [$message]);
    exit;
  }
}

Which creates the following output:

image.png.24ca1872cef63fcca018697513e10417.png

There's probably a more elegant way do to that, idk. You should probably check that you are in the correct context before displaying your errors like that (e.g. AJAX validation through ProductController, etc etc).

Roxayl

Roxayl

If you wish to display errors next to the input (at least on the V1 product page), you could try to display your error message the same way other the default form field validation errors are displayed:

public function hookActionObjectProductUpdateBefore(array $params): void
{
  /** @var \Product $product */
  $product = $params['object'];

  if ($notValidated) {
    $field = 'step1_reference';
    $message = 'Your error message, whatever.';

    ob_end_clean();
    header('HTTP/1.1 400 Bad Request');
    echo json_encode([$field => [$message]);
    exit;
  }
}

Which creates the following output (sorry for the French locale):

image.png.24ca1872cef63fcca018697513e10417.png

There's probably a more elegant way do to that, idk.

×
×
  • Create New...