Page 1 of 1

Form validation: First input effecting second

Posted: Wed Aug 11, 2010 2:53 pm
by J_Mo
Hi there.

How can I say:

If $formar['title'] is equal to 'Mr' then $formar['Gender'] must be 'Male'
else
If $formar['title'] is equal to 'Mrs' then $formar['Gender'] must be 'Female'
else
If $formar['title'] is equal to 'Ms' then $formar['Gender'] must be 'Male'
else
If $formar['title'] is equal to 'Miss' then $formar['Gender'] must be 'Female'

I am a new to all this php and am struggling!! Thanks
Phil

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 3:08 pm
by shawngoldw

Code: Select all

if(strcmp($formar['title'], "Mr") == 0)
{
    $formar['Gender'] = "Male";
}
This is not tested but should work if everything is balanced properly

Shawn

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 4:25 pm
by John Cartwright
I always prefer lookup tables compared to several if() or a switch() statement.

Code: Select all

$titles = array(
   'mr' => 'male',
   'ms' => 'female',
   'mrs' => 'female',
   'miss' => 'female'
);

$mytitle = strtolower($formar['title']); //lower all letters to keep things consistent in comparison
if (array_key_exists($mytitle, $titles)) {
   $formar['gender'] = $titles[$mytitle];
} else {
   $formar['gender'] = 'N/A';
}

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 4:41 pm
by J_Mo
Apologies (and thanks) but I asked the wrong question....

What I meant was

"If $formar['title'] is equal to 'Mr' and $formar['Gender'] is equal to 'Male' {
stuff }

Sorry about that!!

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 5:00 pm
by shawngoldw
I like John's solution better than my suggestion, but why don't you try to expand on mine to accomplish what you want?

Give it a try and if you can't figure it out, I'll help you out. You should be able to build off of mine to do it though.

Shawn

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 5:51 pm
by J_Mo
Ok, so I have this

Code: Select all

class MyValidator extends CustomValidator
{
    function DoValidate(&$formars,&$error_hash)
    {
      if(strcmp($formars['Title'], "Mr") && strcmp($formars['Gender'], "Female"))
          {
            $error_hash['Title']="TEST";
            return false;
           } 
return true;
}
}
When I fill in Mr & Female in the fields Title & Gender respectively no error is returned??

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 6:08 pm
by J_Mo
Scrap that, I left the ==0 of the end.

Thanks for you help.
Phil

Re: Form validation: First input effecting second

Posted: Wed Aug 11, 2010 7:21 pm
by shawngoldw
No problem, you put the == 0 after both strcmp functions right? Not just the last one? It wasn't clear from your last post.

Re: Form validation: First input effecting second

Posted: Thu Aug 12, 2010 3:36 am
by J_Mo
Yes I did...thanks.

I wonder if you could help with a new problem, which I have no clue about! I have two date inputs, "Payday" & "Nextpayday". They are validated so the date displays like this: dd/mm/yyyy

The problem is that $formars['Payday'] cannot be greater than $formars['Nextpayday']

How can I achieve this? I will try myself but a little help would be well appreciated.
Thanks
Phil

Re: Form validation: First input effecting second

Posted: Thu Aug 12, 2010 3:55 am
by J_Mo
I have this

Code: Select all

$date1 = "dd/mm/yyyy";
$date2 = "dd/mm/yyyy";

if((strtotime($date1['Payday']) - strtotime($date2['Nextpayday'])) < 0){
	$error_hash['Nextpayday']="Next payday must be later than payday'";
            return false;
}
but it isn't working

Re: Form validation: First input effecting second

Posted: Thu Aug 12, 2010 8:28 am
by shawngoldw
Is it because you define $date1 and $date2 but use $date1["payday"] and $date2["Nextpayday'], rather than just $date1 and $date2?