hgh/yii-jquery-form-validation

Using this package you can validate inputs out of Yii model

dev-master 2019-07-06 14:36 UTC

This package is auto-updated.

Last update: 2022-05-07 10:53:53 UTC


README

This plugin runs custom validations on custom fileds which are not in Yii model.

Usage

php

<div class="form-group field-model-model_attribute_1">
  <?php
    // the format of id name should be like this modelName-attributeName. eg : model is book, attribute of model is name => book-name
    echo Html::label("Label", "model-model_attribute_1"); ?>
    echo Html::textInput("Model[model_attribute_1]", null, [
      "class" => "form-control",
      "id" => "model-model_attribute_1"
    ]);
  ?>
  <div class="help-block"></div>
</div>

jQuery

  var $form = $('#form');
  $form.yiiValidator('add', {
        model: "model",
        attribute: "model_attribute_1",
        rules: // ...
    });

methods

add

var $form = $('#form');
$form.yiiValidator('add', {
  //...
});

Rules

Rules can be a single object or array of objects.
rules : {
  //...
}
rules : [
  {
    //...
  },
  {
    //...
  }
];

Each rule consists of two element :

  rule
  errorMessage

Rule can be selected from below list or be a function.

required
number
email
url
rule : function(value) {
  //...
}

Example

var $form = $('#form');
$form.yiiValidator('add', {
        model: "model",
        attribute: "model_attribute_1",
        rules: [{
            rule: "required",
            errorMessage: "should fill"
        }]
    });
var $form = $('#form');
$form.yiiValidator('add', {
        model: "model",
        attribute: "model_attribute_2",
        rules: [
            {rule : "required", errorMessage : "should fill"},
            {
                rule: function (value) {
                    return value > 0 && value !== "";
                },
                errorMessage: "should more than zero!",
            }
        ]
    });