Validation problem

2008-12-17 Thread Oribium

I am taking a look at cake 1.2, and I run into problems with the
validation
of form data. I took advantage of the console application to bake some
views, a model, and a controller based on an existing MySQL-database.
I
don't yet have a deep understanding of cake, and would happily welcome
hints and pointers which direction I should go.

I am trying to post data from a form to a database. However, whatever
data
I post, I get redirected back to the form, where a label This field
cannot
be left blank is attached to all fields. For instance if I fill the
alphanumeric field last_name with Jones (without qoutes), I will
get
the error message: This field cannot be left blank. Futhermore,
$this-validationErrors contains all fields and the same error
message. I
have verified that the correct data from the form are assigned to the
controller object's data array with Configure::write('debug', 3);

The view is just a simple form like:

echo $form-create('Lead');
echo $form-input('last_name');
echo $form-input('modified_user_id');
... quite a few more fields here ...
echo $form-end('Submit');

There are also a few form fields added with pure html code. These
fields
are not supposed to be posted to the database, neither be validated
with
the standard cake validation feature.

In the model, there is nothing but name and the validation array.

The model's validation array looks like:

var $validate = array(
'last_name' = array('alphanumeric'),
'modified_user_id' = array('alphanumeric'),
--- quite a few more fields here ---
)

The controller's add function:

function add() {
if (!empty($this-data)) {
if ($this-RequestHandler-isPost()) {
$this-Lead-create();
$this-Lead-set($this-data);
if ($this-Lead-save($this-data)) {
$this-Session-setFlash(__('The Lead has been saved', true));
$this-redirect(array('action'='index'));
} else {
$this-Session-setFlash(__('The Lead could not be saved. Please, try
again.', true));
}
}
}
}

I also tried the following code in the controller: if
($this-Lead-validates()) with the same result.

PHP version: 5.1.6
OS: Linux

CakePHP version:
$Id: VERSION.txt 7692 2008-10-02 05:06:48Z nate $

libs/model/model.php version:
$Id: model.php 7690 2008-10-02 04:56:53Z nate $

--~--~-~--~~~---~--~~
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 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: Validation problem

2008-12-17 Thread Oribium

Thank you for you reply, Rob.

I simplified things trying to get the troubleshooting more easy. I
created
a model, and a view with only one field, first_name. I also created
an
add function in the controller which is supposed to validate input
rather
then saving data to the database. The problem remains and I get
redirected
back to the form, where a label Valid characters: letters and numbers
only, please try again is attached to the field.

The view:

?php
div class=leads form
?php echo $form-create('Lead');?
fieldset
legend?php __('Add Lead');?/legend
?php
echo $form-input('first_name'); ?
/fieldset
?php echo $form-end('Submit');
pr($this-validationErrors);
pr($session);
?

The model:

?php
class Lead extends AppModel {

var $name = 'Lead';

var $validate = array(
'first_name' = array(
'alphaNumeric' = array(
'rule' = 'alphaNumeric',
'required' = true,
'message' = 'Valid characters: letters and numbers only, 
please try
again.'
)));
}
?

The add function in the controller:

function add() {
if (!empty($this-data)) {
if ($this-RequestHandler-isPost()) {
$this-Lead-create();
$this-Lead-set($this-data);
Debugger::dump($this-data['Lead']);
Debugger::dump($this-Lead-validationErrors);
Debugger::dump($this-Lead-invalidFields());
Debugger::dump($this-Lead-validate);
if ($this-Lead-validates()) {
$this-_sendMail();
$this-Session-setFlash(__('The Lead 
has been saved', true));

$this-redirect(array('action'='index'));
} else {
$this-Session-setFlash(__('The Lead 
could not be saved. Please,
try
again.', true));
}
}
}
}


Output from the Debugger statements in the add function:

array(
first_name = Paul
)

array()

array(
first_name = Valid characters: letters and numbers only, please
try
again.
)

array(
first_name = array()
)

Dump of the SessionHelper Object:

SessionHelper Object
(
   [helpers] =
   [__active] = 1
   [valid] =
   [error] =
   [_userAgent] = 372acfb5aa1873a6a6ce218959a07d36
   [path] = /
   [lastError] =
   [security] =
   [time] = 1229549419
   [sessionTime] =
   [watchKeys] = Array
   (
   )

   [id] =
   [_log] =
   [base] = /somepath
   [webroot] = /somepath/
   [here] = /somepath/leads/add
   [params] = Array
   (
   [pass] = Array
   (
   )

   [named] = Array
   (
   )

   [controller] = leads
   [action] = add
   [plugin] =
   [form] = Array
   (
   )

   [data] = Array
   (
   [Lead] = Array
   (
   [first_name] = Paul
   )

   )

   [url] = Array
   (
   [url] = leads/add
   )

   [isAjax] =
   [models] = Array
   (
   [0] = Lead
   )

   )

   [action] = add
   [data] = Array
   (
   [Lead] = Array
   (
   [first_name] = Paul
   )

   )

   [themeWeb] =
   [plugin] =
   [validationErrors] = Array
   (
   [Lead] = Array
   (
   [first_name] = Valid characters: letters and
numbers
only, please try again.
   )

   )

)

On 17 Dec, 20:03, Rob webwe...@gmail.com wrote:
 Without looking at your data model, I can't say for sure, but it would
 seem like you may be breaking a rule that is implied by the table.

 That said, you might want to add a message to your $validate like:

             'last_name'  = array(
                 'alphaNumeric' = array(
                     'rule' = 'alphanumeric',
                     'message' = 'Only letters and numbers for last
 name, please try again.'
             ),
         )

 On Dec 17, 2:56 am, Oribium viktor.richard...@gmail.com wrote:

  I am taking a look at cake 1.2, and I run into problems with the
  validation
  of form data. I took advantage of the console application to bake some
  views, a model, and a controller based on an existing MySQL-database.
  I
  don't yet have a deep understanding of cake, and would happily welcome
  hints and pointers which direction I should go.

  I am trying to post data from a form to a database. However, whatever
  data
  I post, I get