as I needed client-side form validation, I wrote an extension and thought that maybe it could be useful to others.
This extension uses the Validate JQuery plugin and does nothing but turn yii built-in rules into other rules supported by this (excellent) JQuery plugin.
The work to use client-side validation on existing forms is minimal : replace calls to Yii CHtml helpers methods (like activeTextField) with calls to the same method implemented by the extension. Call endForm() at the end, and that’s it.
I've not wrapped all CHtml helpers method and not all Yii built-in rules are supported, but that's just a first release and if it is intresting for others, I'll implement what is missing. I strongly encourage you to take a look to the readme file for more info.
Ok, so try it and play with the samples included in the zip… tell me what you think : every feedback is very welcome
I had thought about this idea (and that’s not difficult to implement with the remote option) however I was really not sure to do it because my understanding of client-side form validation advantages (over server validation) what that it doesn’t load the server (by definition ). If you perform ajax validation, then where is the advantage of client validation, it’s almost like an actual form submission isn’t it ? what do you think ? …
In fact I think that the best solution would be to support most (if not all) of yii built-in validators.
Wouldn't it make sense to have this as a behaviour that you can mix in to your validators if required? I just think it's not possible yet with behaviors as they are now…
They don’t help you in doing the clientside validation itself. But they might be a good way to let others easiliy integrate your extension. A behavior brings new functionality (in this case “client side validation”) that you can “mix in” to existing components (see here). That’s why i thought, they might be apropriate here.
The idea was to have the standard built in validation and add clientside validation globally as behavior to some Validators by configuration if required. But as said above: Not sure if that's possible with the current implementation of behaviors. Maybe Qiang can tell more, if this is really a good idea.
I wasn’t either until recently - but found it not very hard to understand. I’m still looking for good examples when it would make sense to use a behavior. That’s why i thought, your extension might be a candidate. From my understanding they should be ideal to add special functionality to pretty much every thinkable component of the framework.
Problem in your case is though, that for easy configuration the behavior should get added globally to all validator classes instead of validator instances (objects). AFAIK this is not possible (yet…).
I plan to add support for the compareAttribute Yii built-in validator (and maybe the match) and probably do so this week-end. Then I’ll certainly publish the extension in the Extension section of the Yii website, but I would have liked to know if the first version is all right or if it contains some errors ? … or maybe if the same kind of feature (javascript form validation) could be better implemented in Yii… any feedback is very welcome
yes it does take scenario into account … (previous version did already). What happen is that just after the call to form() you can call setScenario(scenationName) so the extension will only select validators which are part of this scenario.
For instance :
<?php
$CS=Yii::app()->jformvalidate;
echo $CS->form();
$CS->setScenario("form7"); // set scenario name here
?>
<?php echo CHtml::errorSummary($model);?>
<div class="container errorSummary" style="display:none">
<p>Please fix the following input errors:</p>
<ul>
</ul>
</div>
<div class="simple">
<?php echo CHtml::activeLabel($model,'email'); ?>
<?php echo $CS->activeTextField($model,'email') ?>
</div>
<div class="action">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php echo $CS->endForm(); ?>
</form>
Actually the archive comes with a sample (controller+model+view) that are using scenario based validation (take a look at the TestForm Model class.
However, I’ve realized it is missing some rule that is common to all yii built-in validator : allowEmpty (shame on me ) This will be implemented in a future release …
I’ll release today a new version to handle correctly optionnal fields. Regarding this nav-bar.php file-not-found I have no idea why Yii does’nt find it … I’ll get back to you later (hopefully with a solution
add support for allowEmpty yii built-in validator attribute
rename to lowercase
(see changelog for details)
@Majin : here is a new release that should suit your needs, as it now handles correctly, optional fields. Regarding the nav-bar not found issue, I think this maybe related to some lower/upper-case problems. My platform is Windows, and file/folder names are case insensitive. You’re using unix . So, in this release I’ve renames sample files and folders so it should be ok … I was not able to test it (I have no unix-like os available) so don’t hesitate to let me know if the problem persist… and same thing if you encounter other problems…
@Raul: yes I think it was case sensitive issue, now first sample screen coming out. But still when I click on each test form I got page not found error :