Issue with allowEmpty and required in multiple validation rules (1.2)

2008-02-03 Thread cisbrh

Hi everybody,
first of all thank you for the really good work you've doing on cake.
I've used it 2 years ago for a massive application and I had to write
my own postgresql layer... The work done since then especially on 1.2
is amazing.
Anyway. I am trying to use multiple validation rules (1.2) with
allowEmpty and required but I just can't get it working.
I've look at the sources and it looks like the 2 keys must be included
in their own rule declaration ie:
'password' = array(
'notNull' =array(
'allowEmpty' = false,
'required' = true,
'message' = 'Please enter a password.'
),
'between' = array(
'rule' = array('between', 4, 12),
'message' = 'Please check the number of 
characters in your
password.'
).

But that does not work because model will default the keywords value
for 'rule' to 'blank' !!! So what value should I give to 'rule'.
If I put allowEmpty and required outside a rule declaration it breaks
completely ie:
'password' = array(
'allowEmpty' = false,
'required' = true,
'message' = 'Please enter a password.',
'between' = array(
'rule' = array('between', 4, 12),
'message' = 'Please check the number of 
characters in your
password.'
).

so I am a bit stuck.
I guess I could use a custom rule which always return true and let
required and allowEmpty to invalidate if needed but surely there is a
better way ?
Thanks in advance.
Benjamin
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Issue with allowEmpty and required in multiple validation rules (1.2)

2008-02-03 Thread duRqoo

Hello Benjamin, this should work:

'password' = array(
'notNull' = array(
'rule' = array('custom', VALID_NOT_EMPTY),
'required' = true,
'message' = 'Please enter a password.'
),

'between' = array(
'rule' = array('between', 4, 12),
'message' = 'Please check the number of characters in your
password.'
)

allowEmpty defaults to false so u don't need to specify it unless u
want to change it. I think allowEmpty and required options are checked
before the rule specification. So if allowEmpty is false and u submit
no data, cake won't check the rule because validation will return
false anyway.

When you are using multiple validation rules per field, cake will
check all of those rules. That means, in your case, if your first rule
fails, your second rule will fail too and u'll get 'between' rule
error msg even when the password was empty. If you want to change this
behaviour you can simply add 'last'=true option to desired rules,
which will force cake to stop further validation for certain field if
one of them invalidates.

Cheers.

On 3. Feb, 11:06 h., cisbrh [EMAIL PROTECTED] wrote:
 Hi everybody,
 first of all thank you for the really good work you've doing on cake.
 I've used it 2 years ago for a massive application and I had to write
 my own postgresql layer... The work done since then especially on 1.2
 is amazing.
 Anyway. I am trying to use multiple validation rules (1.2) with
 allowEmpty and required but I just can't get it working.
 I've look at the sources and it looks like the 2 keys must be included
 in their own rule declaration ie:
 'password' = array(
 'notNull' =array(
 'allowEmpty' = false,
 'required' = true,
 'message' = 'Please enter a password.'
 ),
 'between' = array(
 'rule' = array('between', 4, 12),
 'message' = 'Please check the number of 
 characters in your
 password.'
 ).

 But that does not work because model will default the keywords value
 for 'rule' to 'blank' !!! So what value should I give to 'rule'.
 If I put allowEmpty and required outside a rule declaration it breaks
 completely ie:
 'password' = array(
 'allowEmpty' = false,
 'required' = true,
 'message' = 'Please enter a password.',
 'between' = array(
 'rule' = array('between', 4, 12),
 'message' = 'Please check the number of 
 characters in your
 password.'
 ).

 so I am a bit stuck.
 I guess I could use a custom rule which always return true and let
 required and allowEmpty to invalidate if needed but surely there is a
 better way ?
 Thanks in advance.
 Benjamin
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Issue with allowEmpty and required in multiple validation rules (1.2)

2008-02-03 Thread cisbrh

Thanks for your answer duRqoo,
see below:

On Feb 3, 11:25 am, duRqoo [EMAIL PROTECTED] wrote:
 Hello Benjamin, this should work:

 'password' = array(
 'notNull' = array(
 'rule' = array('custom', VALID_NOT_EMPTY),

I though constant rules were deprecated in cake 1.2. especially this
one has been replaced by allowEmpty (I am trying to be as much
compatible as I can with 1.2).

 'required' = true,
 'message' = 'Please enter a password.'
 ),

 'between' = array(
 'rule' = array('between', 4, 12),
 'message' = 'Please check the number of characters in your
 password.'
 )

 allowEmpty defaults to false so u don't need to specify it unless u
 want to change it. I think allowEmpty and required options are checked
 before the rule specification. So if allowEmpty is false and u submit
 no data, cake won't check the rule because validation will return
 false anyway.

 When you are using multiple validation rules per field, cake will
 check all of those rules. That means, in your case, if your first rule
 fails, your second rule will fail too and u'll get 'between' rule
 error msg even when the password was empty. If you want to change this
 behaviour you can simply add 'last'=true option to desired rules,
 which will force cake to stop further validation for certain field if
 one of them invalidates.

Yes, it is not showing in the given example but I agree with you and
it is in my code already.

In any case this is all a bit of DIY, hack I just wonder if there is
proper way to do it. But I have been debugging validates for a couple
of hours now and it is just not going to work unless 'rule' is
declared to something that always return true (in my case I have used
rule = array('custom' , '/.*/') ) to avoid using constant rules.
What happend is that if you put your keywords outside a declared rule
and dont' declare 'rule' ,validates assumes 'rule' exists an
overwritte $validator array with array('rule', $validator) but the key
does not exist so it is set to false and it all breaks down...
The other scenario is to put them into a rule declaration but again if
'rule' is not defined it is default to 'blank' which is not very
helpful.
There should be a way of testing required and allowEmpty without
having to declare a specific rule.

Cheers
Benjamin


 Cheers.

 On 3. Feb, 11:06 h., cisbrh [EMAIL PROTECTED] wrote:

  Hi everybody,
  first of all thank you for the really good work you've doing on cake.
  I've used it 2 years ago for a massive application and I had to write
  my own postgresql layer... The work done since then especially on 1.2
  is amazing.
  Anyway. I am trying to use multiple validation rules (1.2) with
  allowEmpty and required but I just can't get it working.
  I've look at the sources and it looks like the 2 keys must be included
  in their own rule declaration ie:
  'password' = array(
  'notNull' =array(
  'allowEmpty' = false,
  'required' = true,
  'message' = 'Please enter a password.'
  ),
  'between' = array(
  'rule' = array('between', 4, 12),
  'message' = 'Please check the number of 
  characters in your
  password.'
  ).

  But that does not work because model will default the keywords value
  for 'rule' to 'blank' !!! So what value should I give to 'rule'.
  If I put allowEmpty and required outside a rule declaration it breaks
  completely ie:
  'password' = array(
  'allowEmpty' = false,
  'required' = true,
  'message' = 'Please enter a password.',
  'between' = array(
  'rule' = array('between', 4, 12),
  'message' = 'Please check the number of 
  characters in your
  password.'
  ).

  so I am a bit stuck.
  I guess I could use a custom rule which always return true and let
  required and allowEmpty to invalidate if needed but surely there is a
  better way ?
  Thanks in advance.
  Benjamin
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups Cake 
PHP group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---