Skip to content

Latest commit

 

History

History
108 lines (94 loc) · 1.91 KB

README.md

File metadata and controls

108 lines (94 loc) · 1.91 KB

Yii-jQuery-form-validation

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!",
            }
        ]
    });