PDA

View Full Version : گسترش yii.validation.js



mojtaba.baghban
چهارشنبه 11 دی 1392, 02:49 صبح
سلام
چطور می‌تونم به yii.validation.js چندتا اعتبارسنجی جدید مثل اعتبارسنجی کد ملی اضافه کنم؟
البته توی یه فایل دیگه که این فایل رو گسترش بده. من نمی‌خوام فایل اصلی دستکاری بشه
ممنون از جواباتون

mohsen_31369
چهارشنبه 11 دی 1392, 11:39 صبح
با سلام
شما میتوانید validation مخصوص به خود را در Yii بنویسید. در فولدر extension کلاس CodeMelli را ایجاد نمایید که از کلاس CValidator ارث می برد.
کلاس CValidator دارای دو متد می باشد که باید آنها را بازنویسی کنید.
متد validateAttribute برای اعتبار سنجی سمت سرور
متد clientValidateAttribute برای اعتبار سنجی سمت کلاینت


class CodeMelli extends CValidator {


/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{

// extract the attribute value from it's model object
$value=$object->$attribute;

// add your code for validation



if(!$validate)
{
$this->addError($object,$attribute,'کد ملی معتبر نیست');
}
}

/**
* Returns the JavaScript needed for performing client-side validation.
* @param CModel $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
* @see CActiveForm::enableClientValidation
*/
public function clientValidateAttribute($object,$attribute)
{

return "


// add your code for client validation (javaScript)


if(!validate) {
messages.push(".CJSON::encode('کد ملی معتبر نیست').");
}
";
}
}


حالا در مدل قسمت rule کد زیر را قرار دهید



/**
* @return array validation rules for model attributes.
*/
public function rules()
{
return array(
array('password', 'ext.CodeMelli', ),
);
}


فقط وقتی یک form ایجاد میکنید مقدار enableClientValidation را برابر true قرار دهید




<?php $form = $this->beginWidget('CActiveForm', array(
'id'=>'form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,

)); ?>


با این کار کدهایی را که به صورت javaScript نوشته اید در صفحه ای که فرم ایجاد می کنید برای اعتبار سنجی سمت کلاینت نیز در صفحه بارگذاری می شود

mojtaba.baghban
چهارشنبه 11 دی 1392, 13:27 عصر
ممنون بابت جوابتون
اینایی رو که گفتین می‌دونم. از اونجا که اعتبار سنجی کد ملی نیاز به حدود ۱۰-۱۲ خط کد جاوااسکریپت داره، نمی‌خوام تابع clientValidateAttribute رو بااین خطوط شلوغش کنم بلکه می‌خوام مثل اعتبارسنجی ایمیل بصورت built in باشه.
بازم ممنون بخاطر وقتی که گذاشتین

mojtaba.baghban
جمعه 13 دی 1392, 22:50 عصر
تقریبا تونستم درستش کنم
یه فایل با نام yii.validation.externals.js ساختم با این محتوا

/**
* Yii validation module.
*
* This JavaScript module provides the validation methods for the built-in validators.
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
* @author Mojtaba Baghban <mojtaba.baghban@gmail.com>
* @since 2.0
*/

yii.validation.externals = (function ($) {
var isEmpty = function (value, trim) {
return value === null || value === undefined || value == []
|| value === '' || trim && $.trim(value) === '';
};

var addMessage = function (messages, message, value) {
messages.push(message.replace(/\{value\}/g, value));
};

return {
persianAlpha: function (value, messages, options) {
if (options.skipOnEmpty && isEmpty(value)) {
return;
}

if (!value.match(options.pattern)) {
addMessage(messages, options.message, value);
return;
}

if (options.min !== undefined && value.length < options.min) {
addMessage(messages, options.tooShort, value);
}
if (options.max !== undefined && value.length > options.max) {
addMessage(messages, options.tooLong, value);
}
if (options.is !== undefined && value.length != options.is) {
addMessage(messages, options.is, value);
}
},

nationalCode: function (value, messages, options) {
if (options.skipOnEmpty && isEmpty(value)) {
return;
}

if (!value.match(options.pattern)) {
addMessage(messages, options.message, value);
return;
}

if (value == '0000000000' || value == '1111111111' || value == '2222222222' ||
value == '3333333333' || value == '4444444444' || value == '5555555555' ||
value == '6666666666' || value == '7777777777' || value == '8888888888' ||
value == '9999999999') {
addMessage(messages, options.message, value);
return;
}

var a;
var b = 0;
var c;
var counter = 1;
var check = true;

a = parseInt(value.substring(9, 10));
while (counter <= 9) {
b = b + parseInt(value.substring(counter - 1, counter)) * (11 - counter);
counter = counter + 1;
}
c = b % 11;
if ((c == 0 || c == 1) && a != c) {
addMessage(messages, options.message, value);
return;
}
if (c > 1 && a != 11 - c) {
addMessage(messages, options.message, value);
}
},

phoneNumber: function (value, messages, options) {
if (options.skipOnEmpty && isEmpty(value)) {
return;
}

if (!value.match(options.pattern)) {
addMessage(messages, options.message, value);
return;
}

if (value.length !== options.length) {
addMessage(messages, options.message, value);
}
}
};
})(jQuery);


و واسه اعتبار سنجی مثلا شماره تلفن از yii.validation.externals.phoneNumber استفاده می‌کنم.
البته ناگفته نماند که از yii2 استفاده می‌کنم
اگه کسی ایده بهتری داره استقبال می‌کنم