Re: Making a field Unique

2008-05-05 Thread simonb

assuming you model is called User and you have all the validation
rules applied.

in your controller you need to specify

var $helpers = array('Html', 'Form');

This gives you access to the helpers in the view.
To create a form in the view

create('User' ,array('action' => 'add'));   //this creates
the 
echo $form->input('User.email'); //this creates an input field inside
a div with all the error messages automagically inserted.
echo $form->end('Submit');
?>

Try it and see what ouput you get in the source. It will make sense
then
On Apr 21, 4:26 pm, Chez17 <[EMAIL PROTECTED]> wrote:
> I have solved the first part of my question, but it seems that nobody
> can answer my second question. How do I access that 'Profile must be
> unique' message in my view? I can't find how to do it anywhere.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-21 Thread Chez17

I have solved the first part of my question, but it seems that nobody
can answer my second question. How do I access that 'Profile must be
unique' message in my view? I can't find how to do it anywhere.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-21 Thread Kepten

In 1.2 you can use Model::isUnique() method.

var $validate = array(
'username' => array(
'Username must be between 6 and 255 characters long.' 
=> array(
'rule' => array('between', 6, 255),
'required' => true,
),
'Name must be unique.' => array(
'rule' => array('isUnique', 'name'),
'on' => 'create'
),
),
'profile' => array(
'Profile must be unique.' => array(
'rule' => array('isUnique', 'profile'),
'on' => 'create'
),
),
);


On Apr 20, 6:15 pm, simonb <[EMAIL PROTECTED]> wrote:
> Assuming you are using 1.2
> I have created this snippet in the model. Could be but in app_model if
> it is required.
>
> //checks to see if the given fieldname is unique with the model
> function checkUnique($data, $fieldName) {
>
> $valid = false;
> $this->recursive = -1;
> if(isset($fieldName) && $this->hasField($fieldName))
> {
> $valid = $this->isUnique(array($fieldName => $data));
> }
> return $valid;
>}
>
> the validate array within the model can be as follows. Notice the
> multiple validation rules for email.
> The on=> 'create' only validates this rule on an insert into the
> database.
> The message will automagically appear in the view. No need for the
> tagErrorMsg. just ensure you are using the html Helper.
>
> var $validate = array(
> //password must be at least 6 characters and consist of 
> only
> alphanumeric
> 'password' => array(
> 'rule'  => array('custom', 
> '/[a-z0-9]{6,}$/i'),
> 'message'   => 'Only letters and numbers, 
> min 6 characters'
>  ),
>  //email address must exist and must be a unique 
> value within the
> Customers.email field.
>  'email' => array(
> 'email_valid' => array(
> 'rule' => 'email',
> 'message' => 'Valid email address required',
> ),
>
> 'email_unique' => array(
> 'rule' => array('checkUnique', 'email'),
> 'message' => 'Email address already used.',
> 'on' => 'create'
> )
>
> ),//end email
>
> On Apr 20, 4:21 pm, Chez17 <[EMAIL PROTECTED]> wrote:
>
>
>
> > I am having issues making sure that a field is unique. I am beginner
> > so please go easy on me. Right now, I am trying to create a simple
> > login system where the user name name and profile name have to be
> > unique. I have been cutting and pasting some stuff together and I
> > can't seem to get this to work out.  Here is a sample of my 'create'
> > view:
>
> > Profile Name:
> > password('User/profile', array('size' => 20)); ?>
> > tagErrorMsg('User/profile', 'You need to enter a
> > profile name.') ?>
>
> > Now, here is my validation function:
>
> > var $validate = array(
> > 'username'  => VALID_NOT_EMPTY,
> > 'password'   => VALID_NOT_EMPTY,
> > 'profile'   => VALID_NOT_EMPTY
> > );
> > When I tried use custom rules and try to make the validate array check
> > for uniqueness, my message gets lost. It will keep displaying the
> > 'You need to enter a profile name' error message, not the one I
> > specify in the validate array. So now I am trying to do some custom
> > validation, but I don't understand the find() function. I couldn't
> > find one concrete example of how to use it. I am too much of a
> > beginner to understand the API that they put out.
>
> > I would like to know how to solve my problem both ways. How can I get
> > the message from the validate function to my view? Also, how can I
> > write a custom find() statement so I can check if a profile name is
> > already in the database?
>
> > Thanks for your time,
> > Da
>
> ve

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-20 Thread Chez17

That didn't work, it requires two arguements.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-20 Thread b logica

As you're using the 1.x branch, take this with a grain of salt because
I've never used it. I believe you can simply put:

$html->tagErrorMsg('User/profile')

If there's no error for 'User/profile' it won't display anything.

On Sun, Apr 20, 2008 at 10:04 PM, Chez17 <[EMAIL PROTECTED]> wrote:
>
>  Thanks for all your help. I have one last question. So now that I have
>  my validate working, how do I print the error message that I specify
>  in the function? Simon said that I don't need to put anything in the
>  view, but then says be sure to use the helper, can someone explain
>  what that means? How do I use the html helper to display the error
>  messages from the validate array?
>
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-20 Thread Chez17

Thanks for all your help. I have one last question. So now that I have
my validate working, how do I print the error message that I specify
in the function? Simon said that I don't need to put anything in the
view, but then says be sure to use the helper, can someone explain
what that means? How do I use the html helper to display the error
messages from the validate array?
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-20 Thread b logica

findByFirstName('foo')

On Sun, Apr 20, 2008 at 9:29 PM, Chez17 <[EMAIL PROTECTED]> wrote:
>
>  Does the findBy() function work for any field? If I have a field like
>  first_name how do you do the findBy() function?
>
>  >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-20 Thread Chez17

Does the findBy() function work for any field? If I have a field like
first_name how do you do the findBy() function?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" 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: Making a field Unique

2008-04-20 Thread simonb

Assuming you are using 1.2
I have created this snippet in the model. Could be but in app_model if
it is required.

//checks to see if the given fieldname is unique with the model
function checkUnique($data, $fieldName) {

$valid = false;
$this->recursive = -1;
if(isset($fieldName) && $this->hasField($fieldName))
{
$valid = $this->isUnique(array($fieldName => $data));
}
return $valid;
   }


the validate array within the model can be as follows. Notice the
multiple validation rules for email.
The on=> 'create' only validates this rule on an insert into the
database.
The message will automagically appear in the view. No need for the
tagErrorMsg. just ensure you are using the html Helper.


var $validate = array(
//password must be at least 6 characters and consist of only
alphanumeric
'password' => array(
'rule'  => array('custom', 
'/[a-z0-9]{6,}$/i'),
'message'   => 'Only letters and numbers, 
min 6 characters'
 ),
 //email address must exist and must be a unique value 
within the
Customers.email field.
 'email' => array(
'email_valid' => array(
'rule' => 'email',
'message' => 'Valid email address required',
),

'email_unique' => array(
'rule' => array('checkUnique', 'email'),
'message' => 'Email address already used.',
'on' => 'create'
)


),//end email



On Apr 20, 4:21 pm, Chez17 <[EMAIL PROTECTED]> wrote:
> I am having issues making sure that a field is unique. I am beginner
> so please go easy on me. Right now, I am trying to create a simple
> login system where the user name name and profile name have to be
> unique. I have been cutting and pasting some stuff together and I
> can't seem to get this to work out.  Here is a sample of my 'create'
> view:
>
> Profile Name:
> password('User/profile', array('size' => 20)); ?>
> tagErrorMsg('User/profile', 'You need to enter a
> profile name.') ?>
>
> Now, here is my validation function:
>
> var $validate = array(
> 'username'  => VALID_NOT_EMPTY,
> 'password'   => VALID_NOT_EMPTY,
> 'profile'   => VALID_NOT_EMPTY
> );
> When I tried use custom rules and try to make the validate array check
> for uniqueness, my message gets lost. It will keep displaying the
> 'You need to enter a profile name' error message, not the one I
> specify in the validate array. So now I am trying to do some custom
> validation, but I don't understand the find() function. I couldn't
> find one concrete example of how to use it. I am too much of a
> beginner to understand the API that they put out.
>
> I would like to know how to solve my problem both ways. How can I get
> the message from the validate function to my view? Also, how can I
> write a custom find() statement so I can check if a profile name is
> already in the database?
>
> Thanks for your time,
> Da

ve

--~--~-~--~~~---~--~~
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: Making a field Unique

2008-04-20 Thread b logica

Personally, I'd make those DEFAULT NULL, not the empty string.

Actually, I'd make most of them NOT NULL.

On Sun, Apr 20, 2008 at 12:02 PM, Joshua McFarren <[EMAIL PROTECTED]> wrote:
>
>  Cake's validation will respect your DB.
>
>  CREATE TABLE `users` (
>   `id` int(11) unsigned NOT NULL auto_increment,
>   `username` varchar(255) NOT NULL default '',
>   `password` varchar(255) NOT NULL default '',
>   `email` varchar(255) NOT NULL default '',
>   `profile` varchar(255) NOT NULL default '',
>   `created` datetime default NULL,
>   `modified` datetime default NULL,
>   PRIMARY KEY  (`id`),
>   UNIQUE KEY `username` (`username`),
>   UNIQUE KEY `profile ` (`profile`)
>  ) ENGINE=MyISAM
>
>
>
>  On Apr 20, 11:21 am, Chez17 <[EMAIL PROTECTED]> wrote:
>  > I am having issues making sure that a field is unique. I am beginner
>  > so please go easy on me. Right now, I am trying to create a simple
>  > login system where the user name name and profile name have to be
>  > unique. I have been cutting and pasting some stuff together and I
>  > can't seem to get this to work out.  Here is a sample of my 'create'
>  > view:
>  >
>  > Profile Name:
>  > password('User/profile', array('size' => 20)); ?>
>  > tagErrorMsg('User/profile', 'You need to enter a
>  > profile name.') ?>
>  >
>  > Now, here is my validation function:
>  >
>  > var $validate = array(
>  > 'username'  => VALID_NOT_EMPTY,
>  > 'password'   => VALID_NOT_EMPTY,
>  > 'profile'   => VALID_NOT_EMPTY
>  > );
>  > When I tried use custom rules and try to make the validate array check
>  > for uniqueness, my message gets lost. It will keep displaying the
>  > 'You need to enter a profile name' error message, not the one I
>  > specify in the validate array. So now I am trying to do some custom
>  > validation, but I don't understand the find() function. I couldn't
>  > find one concrete example of how to use it. I am too much of a
>  > beginner to understand the API that they put out.
>  >
>  > I would like to know how to solve my problem both ways. How can I get
>  > the message from the validate function to my view? Also, how can I
>  > write a custom find() statement so I can check if a profile name is
>  > already in the database?
>  >
>  > Thanks for your time,
>  > Dave
>  >
>

--~--~-~--~~~---~--~~
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: Making a field Unique

2008-04-20 Thread b logica

$result = $this->findByProfile();

On Sun, Apr 20, 2008 at 12:14 PM, Chez17 <[EMAIL PROTECTED]> wrote:
>
>  b logica,
>
>  You stop just short of the problem I need to solve. I dont know how to
>  write the DuplicateProfile function. Thats the find() statement I need
>  help with. I know cakes has the findByUsername function, I was asking
>  how to write something like that for the profile field.
>
>  Josh,
>
>  I tried doing the validation through my db but I kept getting mysql
>  warnings and errors, how do I encorporate that into my view? How do I
>  access the message from the validate array from the view?
>
>  I really could use some help getting started, If anyone could write
>  the find() statement that would let me check if a profile already
>  exists it would be very helpful.
>
>  Thanks for your help.
>
>
> Dave
>  >
>

--~--~-~--~~~---~--~~
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: Making a field Unique

2008-04-20 Thread Chez17

b logica,

You stop just short of the problem I need to solve. I dont know how to
write the DuplicateProfile function. Thats the find() statement I need
help with. I know cakes has the findByUsername function, I was asking
how to write something like that for the profile field.

Josh,

I tried doing the validation through my db but I kept getting mysql
warnings and errors, how do I encorporate that into my view? How do I
access the message from the validate array from the view?

I really could use some help getting started, If anyone could write
the find() statement that would let me check if a profile already
exists it would be very helpful.

Thanks for your help.
Dave
--~--~-~--~~~---~--~~
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: Making a field Unique

2008-04-20 Thread b logica

This may just be a typo in your post but you're using
$html->password() for a regular input. It should be
input('User/profile', array('size' => 20)); ?>

I can't give any pointers for Cake 1.x but if you upgrade to the 1.2x
branch you can do this:

create('User', array('action' => 'add'); ?>
input('User.username', array('size' => 20)); ?>
input('User.profile', array('size' => 20)); ?>
password('User.password', array('size' => 20)); ?>

And then put something like the follwing in your User model.

var $validate = array(
'username' => array(
'valid' => array(
'rule' => array('minLength', 1),
'required' => false,
'allowEmpty' => false,
'message' => '...'
),
'duplicate' => array(
'rule' => 'validateDuplicateUsername',
'on' => 'add',
'message' => '...'
)
),
'profile' => array(
'valid' => array(
'rule' => array('minLength', 1),
'required' => false,
'allowEmpty' => false,
'message' => '...'
),
'duplicate' => array(
'rule' => 'validateDuplicateProfile',
'on' => 'add',
'message' => '...'
)
),
// other fields
);

function validateDuplicateUsername($value)
{
// return false if not empty
$res = $this->findByUsername($value);
}





On Sun, Apr 20, 2008 at 11:21 AM, Chez17 <[EMAIL PROTECTED]> wrote:
>
>  I am having issues making sure that a field is unique. I am beginner
>  so please go easy on me. Right now, I am trying to create a simple
>  login system where the user name name and profile name have to be
>  unique. I have been cutting and pasting some stuff together and I
>  can't seem to get this to work out.  Here is a sample of my 'create'
>  view:
>
>  Profile Name:
>  password('User/profile', array('size' => 20)); ?>
>  tagErrorMsg('User/profile', 'You need to enter a
>  profile name.') ?>
>
>  Now, here is my validation function:
>
>  var $validate = array(
> 'username'  => VALID_NOT_EMPTY,
> 'password'   => VALID_NOT_EMPTY,
> 'profile'   => VALID_NOT_EMPTY
> );
>  When I tried use custom rules and try to make the validate array check
>  for uniqueness, my message gets lost. It will keep displaying the
>  'You need to enter a profile name' error message, not the one I
>  specify in the validate array. So now I am trying to do some custom
>  validation, but I don't understand the find() function. I couldn't
>  find one concrete example of how to use it. I am too much of a
>  beginner to understand the API that they put out.
>
>  I would like to know how to solve my problem both ways. How can I get
>  the message from the validate function to my view? Also, how can I
>  write a custom find() statement so I can check if a profile name is
>  already in the database?
>
>  Thanks for your time,
>  Dave
>  >
>

--~--~-~--~~~---~--~~
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: Making a field Unique

2008-04-20 Thread Joshua McFarren

Cake's validation will respect your DB.

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `username` varchar(255) NOT NULL default '',
  `password` varchar(255) NOT NULL default '',
  `email` varchar(255) NOT NULL default '',
  `profile` varchar(255) NOT NULL default '',
  `created` datetime default NULL,
  `modified` datetime default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `profile ` (`profile`)
) ENGINE=MyISAM

On Apr 20, 11:21 am, Chez17 <[EMAIL PROTECTED]> wrote:
> I am having issues making sure that a field is unique. I am beginner
> so please go easy on me. Right now, I am trying to create a simple
> login system where the user name name and profile name have to be
> unique. I have been cutting and pasting some stuff together and I
> can't seem to get this to work out.  Here is a sample of my 'create'
> view:
>
> Profile Name:
> password('User/profile', array('size' => 20)); ?>
> tagErrorMsg('User/profile', 'You need to enter a
> profile name.') ?>
>
> Now, here is my validation function:
>
> var $validate = array(
>         'username'  => VALID_NOT_EMPTY,
>         'password'   => VALID_NOT_EMPTY,
>         'profile'   => VALID_NOT_EMPTY
>         );
> When I tried use custom rules and try to make the validate array check
> for uniqueness, my message gets lost. It will keep displaying the
> 'You need to enter a profile name' error message, not the one I
> specify in the validate array. So now I am trying to do some custom
> validation, but I don't understand the find() function. I couldn't
> find one concrete example of how to use it. I am too much of a
> beginner to understand the API that they put out.
>
> I would like to know how to solve my problem both ways. How can I get
> the message from the validate function to my view? Also, how can I
> write a custom find() statement so I can check if a profile name is
> already in the database?
>
> Thanks for your time,
> Dave
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---