PHP validation comparison

emcguire

Member
Guys,

I am trying to validate a form based on comparing 2 (int) values and it just keeps returning false.

In my form I have a databasejoin element with and "account type limit" and a databasejoin element with "# of account types"

So basically when a user selects a certain category, whn submitting I need to check those 2 dbjoin values, if the "# of the account type" is greater than the "account type limit" it needs to return false. I can't get it to validate no matter what. It always returns false (even when it shouldn't)

here is what I have in the validation "PHP code" on the category type field.

PHP:
$tar_lim_value = $formModel->formData['target_limit_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_limit');
$target_limit = $elementModel->getEmailValue($tar_lim_value, $formModel->formDataWithTableName, 0);

$tar_acct_value = $formModel->formData['target_accounts_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_accounts');
$target_accts = $elementModel->getEmailValue($tar_acct_value, $formModel->formDataWithTableName, 0);

if $target_accts > $target_limit {
      return false;
   }
return true;

so in my form, I have the 2 dbjoin elements with the "limit" and the "# of accounts", then on submit, I just need to compare the two, if the "# of accounts" is higher than the "limit" is needs to stop them.

What am I doing wrong here?
 
hmm. maybe I am not getting the values from the dbjoin properly? I tried taking the last "return true; " out and still doesn't work like I think it should (it always validates, if I take it out it always fails validation)

now it just returns true all the time with this code (adding your ($target_accts>$target_limit) )

PHP:
$tar_lim_value = $formModel->formData['target_limit_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_limit');
$target_limit = $elementModel->getEmailValue($tar_lim_value, $formModel->formDataWithTableName, 0);

$tar_acct_value = $formModel->formData['target_accounts_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_accounts');
$target_accts = $elementModel->getEmailValue($tar_acct_value, $formModel->formDataWithTableName, 0);

if ($target_accts > $target_limit) {
      return false;
   }
return true;

I must be missing some other piece here...
 
I've actually never done that, where would I put it? at the end? would it be...

var_dump($target_accts);
 
I think you'll need the full element name to index $formModel->formData[], not the short. The table___ prefix doesn't get stripped off until the data gets written to the database (so stuff running onAfterProcess, this runs onBeforeProcess).

-- hugh
 
var_dump($yourThing);exit;

You need the 'exit' otherwise you'll never see the output, as there's an implicit redirect after submission, so nothing output diring submission makes it to the browser. So you have to halt execution, and just get the dump output in the browser.

A more convenient way of debugging is to use JDump:

https://extensions.joomla.org/extension/j-dump/

... which will popup a window with your debug data on the next page load:

dump($yourThing, 'some description');

-- hugh
 
oh sorry, that is the

var_dump($target_accts);exit;

which should be "1" - that is the value in the dbjoin field

when I change it to

var_dump($target_limit);exit;

which should be a value of 2, it returns the same thing.

array(1) { [0]=> string(1) "8" }
 
I changed the way I was getting the dbjoin values to

PHP:
$target_limit = $formModel->formData['nab_accounts___target_limit'];
$target_accts = $formModel->formData['nab_accounts___target_accounts'];

var_dump($target_limit);exit;

if ($target_accts > $target_limit) {
      return false;
   }

does it need raw? - No, that didnt change
 
Ok I got it to return what I think is the correct values

string(1) "1"
with var_dump($target_accts);exit;

and
string(1) "2"
var_dump($target_limit);exit;

using this

PHP:
$tar_lim_value = $formModel->formData['nab_accounts___target_limit_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_limit');
$target_limit = $elementModel->getEmailValue($tar_lim_value, $formModel->formDataWithTableName,0);

$tar_acct_value = $formModel->formData['nab_accounts___target_accounts_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_accounts');
$target_accts = $elementModel->getEmailValue($tar_acct_value, $formModel->formDataWithTableName,0);


//$target_limit = $formModel->formData['nab_accounts___target_limit_raw'];
//$target_accts = $formModel->formData['nab_accounts___target_accounts_raw'];

var_dump($target_accts);exit;

if ($target_accts > $target_limit) {
      return false;
   }

but it is still not validating properly. its returning false every time.
 
Well ... should work then. :)

Try ...

$retval = (int)$target_limit <= (int)$target_accts;
var_dump($retval);exit;

... if that's true, then just ...

return (int)$target_limit <= (int)$target_accts;
 
Ok,

I think I got it. I had to add 1 to the account total, because I think it runs BeforeProcess. So it was off by one. It would allow the third one to go through and wouldn't be false until the next time. So this is what worked...

PHP:
$tar_lim_value = $formModel->formData['nab_accounts___target_limit_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_limit');
$target_limit = $elementModel->getEmailValue($tar_lim_value, $formModel->formDataWithTableName,0);

$tar_acct_value = $formModel->formData['nab_accounts___target_accounts_raw'][0];
$elementModel = $formModel->getElement('nab_accounts___target_accounts');
$target_accts = $elementModel->getEmailValue($tar_acct_value, $formModel->formDataWithTableName,0);


return (int)$target_accts + 1 <= (int)$target_limit;

Thanks for your help with that. I haven't done very many coded validations like that.
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top