[EXTENSION] wvActiveForm

wvActiveForm is a CActiveForm descendant that validates the input on client with jQuery, using the rules defined at the model, while also supporting custom rules.

http://www.yiiframework.com/extension/wvactiveform/

Validators:

* Required


* Number


* String (length)


* Email


* Default value (displays value that disappear on field click)


* No whitespace


* URL


* Manual rules

Rules:

* jQuery.Validate


* jQuery.Numeric


* jQuery.DefaulValue


* jQuery.keyFilter

CValidator supported:

* CRequiredValidator


* CStringValidator


* CNumberValidator


* CEmailValidator


* CUrlValidator

Instead of reimplementing all the rules you could use jformvalidate in your extension.

Yes I saw it, but to use it one must modify his source code, I wanted a solution that needed no code changes, and uses CActiveForm.

Hi Rangel,

it seems like a promising extension ! however, I tested it following your instructions, and I get the error :




Fatal error: Call to undefined function lcfirst() in H:\home\Project\yii.1.2\protected\extensions\wvactiveform\wvFormRules.php  on line 48



ciao

B)

ps: you’re right about jformvalidate not supporting CActiveForm Widget, and I’ve been thinking about releasing a new version that would support CActiveForm Widget…

Hmm you’re right, lcfirst function only exists in PHP 5.3+, I’ve released a new version without this call.

About jformvalidate, as I started working with Yii 1.1, all my code was already using CActiveForm, so I want a method of upgrading all my applications with a minimum effort, that’s why I created this extension.

Also, I wanted to make it easy for 3rd party developers create his own rules as extensions, again always thinking of minimum code changes.

Hello there! :)

I have some problem with latest wvActiveForm from svn, which reproduced by definition rule with option: ‘allowEmpty’ => false.

For example:


'telephonePresent' => array(

  'telephone',

  'length',

  'min' => 2,

  'max' => 100,

  'allowEmpty' => false,

),

I think option ‘allowEmpty’ is semantically equals to ‘require’ rule (do you think? :) ), and expected to see on page something like:


'Order[telephone]': {

            'required': true,

            'minlength': 2,

            'maxlength': 100

        },

But there is:


'Order[telephone]': {

            'minlength': 2,

            'maxlength': 100

        },

Thanks!

Possible solution within attachment (maybe we need move $allowEmpty and related processing to base class).

Hmm I think the main problem is, Yii doesn’t take allowEmpty in account when checking for required fields (CModel::isAttributeRequired), and also doesn’t add the ‘*’ on the field’s label:




	public function isAttributeRequired($attribute)

	{

		$model=$this->resolveAttribute($attribute);

		foreach($model->getValidators($attribute) as $validator)

		{

			if($validator instanceof CRequiredValidator)

				return true;

		}

		return false;

	}



This "allowEmpty" conflicts with the required validator, but on the case of a form, the field IS required when allowEmpty is false, so I think your patch is correct.

I added it to the SVN.

Hello again! :)

I’ve explored a new bug (or feature :) ). For example, we defined such rule




array(

  'test',

  'length',

  'min' => 4,

  'max' => 20,

  'allowEmpty' => false,

  'message' => Yii::t('error', 'Please enter valid {attribute}.'),

  'tooShort' => Yii::t('error', 'Please enter valid {attribute}.'),

  'tooLong' => Yii::t('error', '{attribute} is too long.'),

  'on' => 'update',

),



The problem is shown, when another language set in config. We see default english-labeled attribute.

To solve the problem, please fix wvActiveForm.php:




public function addValidationForValidator($model, $id, $name, $attributeName, CValidator $validator)

{

  $attributeName = $model->getAttributeLabel($attributeName); // added



Hmm are you sure about this? Are you calling this method directly? Because it is only called in the "checkValidation" method, and it already does this.




	protected function checkValidation($model,$attribute,&$htmlOptions=array(),

		$fieldOptions=array())

	{

...

		CHtml::resolveNameID($model,$attribute,$htmlOptions);

		$attributeName = $model->getAttributeLabel($attribute);

...

		foreach ($model->getValidators($attribute) as $validator)

			$this->addValidationForValidator($htmlOptions['id'], $htmlOptions['name'], $attributeName, $validator);

...

	}



Yeah, it seems i was wrong :) Thanks :)

Does it works with form builder? I tested it with form builder but it doesn’t works out of the box, doesn’t know where to modify though, any ideas?

What form builder? If it uses CActiveForm it should work, if it uses CForm, then it won’t.

When I try to use qtip layout, I got a javascript error.


'layout'=>'qtip'

Error: $(this).data("qtip") is undefined

Source-code: jquery.qtip-1.0.0-rc3-dm.js

Line: 38