Category validator

Phproberto\Joomla\Entity\Categories\Validation\CategoryValidator

This validator ensures that categories cannot be saved without expected data. It’s automatically executed each time you try to save a category entity but you can also execute it manually.

Index

Usage

use Phproberto\Joomla\Entity\Categories\Category;
use Phproberto\Joomla\Entity\Categories\Validation\CategoryValidator;
use Phproberto\Joomla\Entity\Validation\Exception\ValidationException;

$category = Category::fromData(['title' => 'Category title']);
$validator = new CategoryValidator($category);

try
{
    $validator->validate();
}
catch (ValidationException $e)
{
    echo 'Category cannot be saved: ' . $e->getMessage();
}

// Category already executes validation on save. This would be equivalent to previous code:
$category = Category::fromData(['title' => 'Category title']);

try 
{
    $category->save();
} 
catch (SaveException $e) 
{
    echo 'Error saving category: ' . $e->getMessage();
}

Rules

This validator assigns the following rules:

access (optional)

The value of the access column:

  • It’s optional
  • If specified it must be null or a positive integer.

extension

The value of the extension column:

  • It’s required
  • It cannot be empty.

level (optional)

The value of the level column:

  • It’s optional
  • If specified it must be null or a positive integer.

parent_id (optional)

The value of the parent_id column:

  • It’s optional
  • If specified it must be null or a positive integer.

title

The value of the title column:

  • It’s required
  • It cannot be empty.