kurdt94/yii2-isbn-validator

ISBN form validation for ISBN-10 and ISBN-13 identifiers

Installs: 20

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-main 2023-08-28 13:11 UTC

This package is auto-updated.

Last update: 2024-12-28 15:58:32 UTC


README

This extension adds ISBN-10 / ISBN-13 Form validation as a standalone validator to the Yii2 framework.

Installation

The preferred way to install this extension is through composer.

Either run

$ php composer.phar require kurdt94/yii2-isbn-validator "@dev"

or add

"kurdt94/yii2-isbn-validator": "@dev"

to the require section of your composer.json file.

Usage

This extension extends the yii\validators\Validator class to validate ISBN-10 or ISBN-13 identifiers. The IsbnValidator class can be applied using the alias k-isbn in your model rules. In this example we validate the "isbn_number" field:

use yii\db\ActiveRecord;

class BooksModel extends ActiveRecord {
    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            //...
            [['isbn_number'], 'k-isbn'],
        ];
    }
}

Ajax Validation

Prefered is the use of AjaxValidation. This can be aplied using the ActiveForm enableAjaxValidation property.

echo $form->field($model, 'isbn_number', ['enableAjaxValidation' => true]);

-- Or if you wish to enable AjaxValidation on the entire Form. --

echo $form = ActiveForm::begin(['id' => 'books-form', 'enableAjaxValidation' => true,]);

Don't forget to set up Ajax response in your controller-action. If the request isAjax and we can load($_POST) then return \yii\widgets\ActiveForm::validate($model).

$model = new Books();

if (Yii::$app->request->isAjax && $model->load($_POST))
{
    Yii::$app->response->format = 'json';
    return \yii\widgets\ActiveForm::validate($model);
}

License

This extension is released under the BSD-3-Clause License. Please read LICENSE.md for more details.