hgh / yii-jquery-form-validation
Using this package you can validate inputs out of Yii model
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Language:JavaScript
Requires
- components/jquery: *
- yiisoft/yii2: ~2.0.0
This package is auto-updated.
Last update: 2024-12-07 16:30:14 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!", } ] });