View previous topic :: View next topic |
Author |
Message |
vego Smarty Rookie
Joined: 24 Feb 2005 Posts: 23
|
Posted: Fri Mar 04, 2005 11:35 pm Post subject: Formcat suggestion |
|
|
- might apply to SmartyValidate as well.
It would be great to be able to define validation criteria as 'strict' or more 'relaxed' - to be able to define criteria that generated a warning, but still let the user submit the form (presents a yes/no dialog perhaps)
For my project, I need to generate warnings to alert the user that something is missing or incorrect with the form, but the user can submit it if s/he so wishes.
By glancing at the source code, I wonder if it could be accomplished by just changing the javascript alert()s to confim() boxes in the various validation criteria plugins? |
|
Back to top |
|
vego Smarty Rookie
Joined: 24 Feb 2005 Posts: 23
|
Posted: Sun Mar 06, 2005 10:48 pm Post subject: |
|
|
For those who might be interested, here's some changes to the code I did which seemed to do the trick. Example from validators/js_validator_notEmpty.php:
change
Code: |
if(!empty($field)){
$str = "\n\n// checks if ".$field." has been filled\n\n".
"if(fc['$field'].value==''){\nalert('$message');\n".
$focusStr."return false;\n}\n";
return $str;
}
|
to
Code: |
if(!empty($field)){
if ($optional) {
$str = "\n\n// checks if ".$field." has been filled\n\n".
"if(fc['$field'].value==''){\nif(!confirm('$message - submit anyway?')) {".
$focusStr."return false;}\n}\n";
} else {
$str = "\n\n// checks if ".$field." has been filled\n\n".
"if(fc['$field'].value==''){\nalert('$message');\n".
$focusStr."return false;\n}\n";
}
return $str;
}
|
and add optional=1 to the validation code in the template, eg:
{formcat check="notEmpty" field="FullName" message="Name missing" optional=1} |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Mar 07, 2005 3:39 pm Post subject: |
|
|
With SmartyValidate (since there is no client side javascript), you could add a checkbox to the form that confirms that they really do want to leave the field empty. You could then test for this in your validator. This is something that could be on the form along side the field, or show up along with the error message upon a form redraw. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Mar 07, 2005 3:45 pm Post subject: |
|
|
btw, the javascript code:
if(!confirm('foo')) return false;
is redundant, it can be shortened to:
return(confirm('foo')); |
|
Back to top |
|
vego Smarty Rookie
Joined: 24 Feb 2005 Posts: 23
|
Posted: Mon Mar 07, 2005 4:16 pm Post subject: |
|
|
mohrt wrote: | btw, the javascript code:
if(!confirm('foo')) return false;
is redundant, it can be shortened to:
return(confirm('foo')); |
Are you sure? Because in the event confirm returns false (we don't want to submit) we want to BOTH return false and execute $focusStr (focus on field).
I do admit I've only skimmed through the code and made some changes that seem to work |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Mar 07, 2005 4:24 pm Post subject: |
|
|
You can still test for that:
return(fc['$field'].value=='' && confirm('foo'));
either way works, but the former is somewhat confusing until you stare at it |
|
Back to top |
|
vego Smarty Rookie
Joined: 24 Feb 2005 Posts: 23
|
Posted: Mon Mar 07, 2005 6:44 pm Post subject: |
|
|
mohrt wrote: | With SmartyValidate (since there is no client side javascript), you could add a checkbox to the form that confirms that they really do want to leave the field empty. You could then test for this in your validator. This is something that could be on the form along side the field, or show up along with the error message upon a form redraw. |
In my project, I have so many fields that would require a checkbox that I think it is not practical. But it might perhaps be possible to add a single checkbox (possibly only visible on redraw after validation failure), and then check for this *before* validation on subsequent submit:
(modified from http://www.phpinsider.com/php/code/SmartyValidate/)
[php:1:0e7c6591c1]
if(($_POST['submit_with_errors']=='y') || SmartyValidate::is_valid($_POST)) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty->display('success.tpl');
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->assign('display_checkbox', true);
$smarty->display('form.tpl');
[/php:1:0e7c6591c1]
template:
Code: |
<form>
...
{if $display_checkbox}
Submit with errors: <input type="checkbox" value="y">
{/if}
</form>
|
...on the other hand, this would not let me distinguish between 'strict' and 'nonstrict' validation. Perhaps a parameter could be passed to smartyvalidate? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Mar 07, 2005 7:11 pm Post subject: |
|
|
All parameters passed through the {validate} function are available to the validator, so you could pass a strict flag and test for it. |
|
Back to top |
|
joey Smarty Regular
Joined: 29 Oct 2003 Posts: 57
|
Posted: Sat Apr 30, 2005 4:16 pm Post subject: |
|
|
vego wrote: | For those who might be interested, here's some changes to the code I did which seemed to do the trick. Example from validators/js_validator_notEmpty.php:
change
Code: |
if(!empty($field)){
$str = "\n\n// checks if ".$field." has been filled\n\n".
"if(fc['$field'].value==''){\nalert('$message');\n".
$focusStr."return false;\n}\n";
return $str;
}
|
to
Code: |
if(!empty($field)){
if ($optional) {
$str = "\n\n// checks if ".$field." has been filled\n\n".
"if(fc['$field'].value==''){\nif(!confirm('$message - submit anyway?')) {".
$focusStr."return false;}\n}\n";
} else {
$str = "\n\n// checks if ".$field." has been filled\n\n".
"if(fc['$field'].value==''){\nalert('$message');\n".
$focusStr."return false;\n}\n";
}
return $str;
}
|
and add optional=1 to the validation code in the template, eg:
{formcat check="notEmpty" field="FullName" message="Name missing" optional=1} |
Thanks for your sugestions.
It's possible for me to add them into new version. _________________ Joey
Senior Developer & Designer
-------------------------------------------
http://www.propagator.net
[SourceForge Project] Formcat with Smarty!
http://www.propagator.net/formcat/ |
|
Back to top |
|
|