Raoul, is there a way to validate decimal values?
Raoul, is there a way to validate decimal values?
Nevermind, I figure it out. I set the integerOnly value to false in my model and the validation on client side works perfectly for the specific attribute
I was wondering, is it possible to add custom validation?
yes it's possible to add a custom validation rule… but of course, it is not as simple as using a built-in (and supported) yii validator.
You have 2 options :
Hope this helps …
Raoul, thanx for pointing me in the right direction. I’ll have a look into this
Hi bjorntheart,
here are some more info on how to create a custom validator enabled both on the client and server side.
1. Create the Javascript code
This code will be evaluated on the client and should implement the type of validation you want. This JS function will have to be registred against the JQuery plugin (the one that does the validation job on the client). You may find an set of additional js function in the file extensions/jformvalidate/js/additional-methods.js.
So, your own validator function should be registred like follow :
jQuery.validator.addMethod("myCustomValidator", function(value, element, params) { /* put your code here and return true if validation succeeds, false */ /* if validation failes */ return true; }, "sorry but myCustomValidator failed ! ");
2. Create the php code
Ok, no surprise here, you have no other choice
In this example, I will code the validator as a Model class method.
<?php public function myCustomValidator($attribute,$params) { // do whatever you want here and in case of validation failure, // add an error to the model instance $this->addError('$attribute','myCustomValidator failed for attribute '.$attribute); } ?>
3. declare validation rules
In this particular case of a new custom Validator, you have to declare 2 validators in the rules array, one if the the JS version and another for the PHP version. If we keep the same example as before, here is how a field named ‘custom1’ should be delcared in order to be validated both client and server side, by your custom validator :
<?php // this declares the JS version of the validator array('custom1', 'application.extensions.jformvalidate.ECustomJsValidator' , 'rules' => array( 'myCustomValidator' => array( 'param1' => 'value1', 'param2' => 'value2' ) ), 'messages' => array( 'myCustomValidator' ) ), // this declares the PHP version of the validator array('custom1', 'myCustomValidator','param1' => 'value1', 'param2' => 'value2'), ?>
… ok ok, this is not very nice as you have to repeat twice the same thing (more or less) but in the current version of this extension, this is the only way
Hope this helps …
ciao
Wow, thanx for that Raoul, this will surely help me alot! Thanx again
I have found a bug when using the extension with ajax forms where the form is not closed and reused if the action was not successful.
The way i set my ajax forms up i return a success or failure code when the action fails to validate server side, say due to incorrect username or password, when the form is resubmitted the restoreName function is actually eating any field it modifes so on the first submit say the YII_CSRFTOKEN is correct but the next run through the function with the next submit it eats more of the string and thus causes the token to be invalid causeing an exception.
There are several methods that this could be fixed but I would say that since you modify the names and need to restore them perhaps rather than having the users of the extension call the restoreName function if we need to modify the submit behavior would be rather have us pass a function that your submit function could call so you can do what needs to be setup and restored.
Example:
submitHandler: function(form) { $.fn.EJFValidate.restoreName(); var retval = true; if($(form).data('userSubmit') !== undefined) retval = $(form).data('userSubmit')(form); //do whatever else is needed like reencode the names return retval; }
I just noticed in your code that you have a uniqueName function if I add that after the ajaxsubmit the next submit is fine. You probably should either mention it in the docs or go with something like above so we the users of the extension don't have to worry about what the extension is doing to make things work.
Hi rfenner,
and thanks for pointing at this issue. Actually, in the current version of the extension, ajax form submission is not supported, and this is something I've planned to do in future release… you analysis of the problem, is good, and what is even better is the solution you provide. Great work !! … I will release a new version soon.
Here we go … !
Version 1.0.6 is released.
Check http://www.yiiframew…/jformvalidate/ more
In this new version, the major improvement is the support for ajax submit. The Yii core class CHtml::ajaxSubmitButton() is now wraped and can be invoked by EHtml::ajaxSubmitButton(). Check the demo form13 and form14 samples.
@rfenner : in the end, I could not implement your solution as the submitHandler is not invoked when the form is submited with ajax.
Try it, and do not hesitate to send feedback.
PHP Error Description Undefined variable: postedData Source File D:wampwwwvalidprotectedcontrollersJsvformController.php(60) 00058: $this->render($viewName,array( 00059: 'model'=>$testForm, 00060: 'postedData'=>$postedData, 00061: 'navbar' => $this->renderPartial('navbar',null,true) 00062: ) Stack Trace #0 D:wampwwwyiiframeworkwebactionsCInlineAction.php(32): JsvformController->actionTestForm() #1 D:wampwwwyiiframeworkwebCController.php(300): CInlineAction->run() #2 D:wampwwwyiiframeworkwebCController.php(278): JsvformController->runAction() #3 D:wampwwwyiiframeworkwebCController.php(257): JsvformController->runActionWithFilters() #4 D:wampwwwyiiframeworkwebCWebApplication.php(332): JsvformController->run() #5 D:wampwwwyiiframeworkwebCWebApplication.php(120): CWebApplication->runController() #6 D:wampwwwyiiframeworkbaseCApplication.php(133): CWebApplication->processRequest() #7 D:wampwwwvalidindex.php(11): CWebApplication->run()
Hi manfred,
there is indeed an error in the sample App… can you tell me what sample form you ran to get this error message ?
Thx
Oops, I was too fascinated with myself.
$postedData error appear when I try to check ?r=Jsvform/TestForm.
Whenever it is defaultAction or it is some id's in the GET
ok, I'm not able to test it right now, but maybe, on line 39, add:
$postedData = null;
in file JsvFormController.php and tell me if it helps. I’ll check that again tomorrow (for now, time to sleep )
Oh, I’ve already try and it doesn’t work. Wake up Raoul!
Sow a problem with ActiveLabelEx mentioned before, but apparently it isn't fixed in 1.0.6. Here's fix:
--- a/src/php/protected/extensions/jformvalidate/EJFValidate.php +++ b/src/php/protected/extensions/jformvalidate/EJFValidate.php @@ -116,6 +116,7 @@ class EJFValidate extends CApplicationComponent { */ public function setScenario($name){ $this->_scenario = $name; + CHtml::$scenario = $name; } /** * Initiates the process of collecting informations on validation rules until endForm() is called.
Appears scenario name wasn't forwarded up to CHtml neither in EJFValidate->setScenario() nor in HTml::setScenario(). Then maybe it'd be more logical to put this into EHtml, but I'm using it like this, so it's more convenient:
$JFV = Yii::app()->jformvalidate; $JFV->setScenario('internal'); $JFV->setOptions(array( 'errorContainer'=> 'div.ui-state-error', 'wrapper' => 'span', 'errorLabelContainer' => 'div.ui-state-error ul', 'errorClass' => 'ui-state-error', 'onkeyup' => false, 'onfocusout' => false )); echo $JFV->form();
cheers and thanks for great extension!
Mindeh
thanks mindeh,
I’ll check that your fix will be added to the next release … right now, I’m on holidays
Hello all Yii framework users.
I have found this framework a few days ago and… I already have some problems .
I checked jquerry validation plugin documentation, JSV extension docs and analysed demos but cant find solution for my problem. How can I validate fields before submitting the form? Its working when onkeyup event occurs but only after form has been submitted at least once. When I set onkeyup to true in options array it generates "validator.settings["on" + event.type].call is not a function" error. Same with onfocusout. Any suggestion or tips ?
As this is my first post in here i would like to say that I’m very grateful for work of creators and maintainers of Yii framework and all related extensions and bits of code . Thank you all.
Hi Sidewinder,
and sorry for the late reply but I’m on holidays right now, away from my PC
If I understand well, what you want is an automatic validation as the user navigates between form fields … correct ? I’ll check at the JQuery Validation Plugin and try to setup an example for such case, as soon as I’m back at work.
(again sorry for the delay)
ciao
No rush Raoul have a good time there .
Yes that’s exactly what I want. Automatic validation when user navigates the fields, but also in some cases when user types some data into the field. But usually onBlur or onChange should do the job… Thx in advance and don’t think about your work too much .
Hi sidewinder
(yes I’m back from holidays ) after some search on your problem I found that the JQuery Validate Plugin documentation is incorrect when it states that the onfocusout and onkeyup options are boolean type. In fact, valid values for these two options should be FALSE or a function !
Ok, this is not clear, but the point is, I found a way to make it work like you want. So, when you set Validation options here is what you should write in order to activate onfocusout validation.
<?php
$CS=Yii::app()->jformvalidate;
echo $CS->beginForm();
$CS->setScenario("form7");
$CS->setOptions(array(
// some general settings
'errorContainer'=> "div.container",
'wrapper' => 'li',
'errorLabelContainer' => "div.container ul",
'errorClass' => "invalid",
// here is the trick !!
'onfocusout' => 'function(element) { this.element(element);}'
));
?>
See the ‘onfocusout’ => ‘function(element) { this.element(element);}’ ? well, it will work also with the onkeyup option.
Hope this helps.
ciao
[color="#FF0000"][size="3"]To all JVFValidate users
Important : mindeh raises an error in 1.0.6 release (see post above). The fix provided in his post will be included in the next release but in the meantime, if you’re using scenario through the setScenario method, please insert it manually.[/size][/color]