Page 1 of 1

Alert on erroneous field input?

PostPosted: Tue Sep 20, 2011 11:13 am
by lhat
Hi,

In our application, we need an alert to pop up when a user enters a numeric value above a certain threshold in a given field. Ordinarily, I figured this would be handled with a Xataface validator, but we want the record to be save-able even if the offending value has been entered, which won't work with Xataface validators because with these one must first correct the errant value (to avoid matching the condition) in order to pass on to saving the record.

That is, in my delegate class, I tried to do handle this "warning" case by adding an additional condition:

Code: Select all
function fieldname__validate(&$record, $value, &$params){
    if ( $value < 0 || $value > 228 ){
        $params['message'] = 'Sorry: must be between 0 and 228';
        return false;
        }
    elseif ( $value >= 109 ) {
        $params['message'] = 'Values greater than 108 are very unlikely.';
        return true;
        }
    else
        return true;
  }


But though I enter a value >= 109, no message appears; the appropriate message does appear when I change the return to false in my elseif condition, but then I can't save the record until the value is corrected.

Is there a way to do this in the validator or do I need a separate Javascript or Ajax-style warning to pop up when the user enters something beyond the threshold? If the latter is the best approach, I suppose I'd use a widget:att:onclick and then call my external javascript, but I'm uncertain of the syntax for that call, or for that matter, how I might call one of the available external Javascript validation libraries.

Any pointers would be most welcome. . .

Thanks!

Re: Alert on erroneous field input?

PostPosted: Tue Sep 20, 2011 3:35 pm
by shannah
Yes. If this is a soft validation (i.e. it shouldn't stop the user from saving) I'd just do it in javascript.

Re: Alert on erroneous field input?

PostPosted: Wed Sep 21, 2011 12:01 pm
by lhat
Thanks, Steve--

But when I add to my fields.ini the line widget:atts:onclick = "doEntryWarn();" under the appropriate field and in that same directory I have my EntryWarn.js, nought happens when I click in the field--what am I missing? Should I use onchange, or is my field value test not working?

My EntryWarn.js says just this:

Code: Select all
<script type="text/javascript">
<!--
function threshold ( )
{
    if ( document.tablename.fieldname.value >= "109" )
    {
        alert ( "Woot!  Values of 109 and above are very unlikely!" );
    }
}
//-->
</script>


Any help most appreciated!

Re: Alert on erroneous field input?

PostPosted: Wed Sep 21, 2011 12:40 pm
by shannah
Your validation function here is called threshold() but you are calling doEntryWarn() on the trigger.... perhaps I'm missing something?

Re: Alert on erroneous field input?

PostPosted: Wed Sep 21, 2011 12:55 pm
by lhat
I wondered if that might cause woe, but when I eliminated that function from the .js file, still no grease. I had thought that in order to call this function from the external EntryWarn.js I needed to stipulate the latter filename; are you saying I could put this javascript function right into my Delegate Class directly? (i.e., javascript in a php file?). Sorry to be dumb, but I am learning under your patient tutelage... :wink: