This plugin runs custom validations on custom fileds which are not in Yii model.
<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>
var $form = $('#form'); $form.yiiValidator('add', { model: "model", attribute: "model_attribute_1", rules: // ... });
var $form = $('#form'); $form.yiiValidator('add', { //... });
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) { //... }
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!", } ] });