Hi Mike,

I agree that there is an issue with the beforeValidate() function. I've not seen ticket 932 before, but now that I do, I have noticed that I kind of posted a similar ticket My ticket can be found here and I hope that it won't get closed down like 932 did. However, I feel sorry for supplicating the ticket and normally would have just reopened the old one.

For now I think the easiest fix would be something like this in AppModel:

function validates($data = "" {
    if (isset($data))
       $this->data = "">
    return parent::validates($data);
}

(It's basically the same fix as yours, but it doesn't duplicate any core code.

Best Regards,
Felix Geisendörfer

Mike schrieb:
Validation

Seems like a lot of talk recently about validation:
http://www.snook.ca/archives/php/cakephp_data_va_1/
http://groups.google.com/group/cake-php/browse_thread/thread/985aa4578f79828e/f98b966dffb76a0e
http://groups.google.com/group/cake-php/browse_thread/thread/bec26338072a9e9b/7a47a67e615563ba
http://groups.google.com/group/cake-php/browse_thread/thread/6cedea09efb607fc/4996370ee1598ce9

Like Kabuto in the last thread, "I tried asking in IRC, but nobody
there was able to help
me".  I also had very little joy on IRC.
The Cake developers may be getting sick of these questions, but people
new to Cake may find it confusing.
Anyway, here's how I have implemented validation for 1.1.4.3104

To solve "Strange beforeValidate() behaviour"
https://trac.cakephp.org/ticket/932
copy /cake/app_model.php and put it in the /app/ directory.
Add this method to app/app_model.php


	function invalidFields($data = "" {
		if(!isset($this->data) || empty($this->data)){ //I've added this
			$this->data = ""                        //I've added this
		}                                              //I've added this

		if (!$this->beforeValidate()) {
			return false;
		}

		if (!isset($this->validate)) {
			return true;
		}

		if (!empty($data)) {
			$data = ""
		} elseif (isset($this->data)) {
			$data = ""
		}

		if (isset($data[$this->name])) {
			$data = ""
		}

		foreach($this->validate as $field_name => $validator) {
			if (isset($data[$field_name]) && !preg_match($validator,
$data[$field_name])) {
				$this->invalidate($field_name);
			}
		}
		return $this->validationErrors;
	}


/******   controller    ******/

	function whatevers()
    {
			if ($this->Whatever->validates($this->data))
            {
				// save
				$this->Whatever->save($this->data);
            } else {
				// validation errors
				$this->validateErrors($this->Whatever);
				// used by tagSelect() method in view.  $selected parameter shows
selected option
				$this->set('selected_size',
$this->params['data']['Whatever']['size_id']);
				$this->set('selected_brand',
$this->params['data']['Whatever']['brand_id']);
				$this->render();
			}
    }


/******   model   *******/

class Whatever extends AppModel
{
    var $name = 'Whatever';
	var $validate = array(
		'size_id'=>'/[1-9]/',
		'brand_id'=>'/[1-9]/')
        ;


	function beforeValidate()
	{

		// code for date validation goes here...
		// I use PEAR Date, which I haven't shown here...


		// to invalidate fields:
		$this->invalidate('start_date');  // to enable html helper to add
class="form_error" to the input tag (in the view)
		$this->invalidate('finish_date'); // to enable html helper to add
class="form_error" to the input tag (in the view)

		$this->invalidate('start_date_blank'); // enables tagErrorMsg() to
show the error in the view
		$this->invalidate('finish_date_blank'); // enables tagErrorMsg() to
show the error in the view
		$this->invalidate('date_range_negative'); // enables tagErrorMsg() to
show the error in the view


		return true;
	}

}

/******  view   *******/

<form id="quote" method="post" action="">
	<fieldset>
	  <legend>Whatever</legend>

	  <label for="">Start Date</label>
	 <p><?php echo $html->input('Whatever/start_date')?><br
/>day/month/year</p>
	<?php echo $html->tagErrorMsg('Quote/start_date_blank', 'Start date is
required.'); ?>
	  <label for="">Finish Date</label>
	  <p><?php echo $html->input('Whatever/finish_date')?><br
/>day/month/year</p>
	<?php echo $html->tagErrorMsg('Quote/finish_date_blank', 'Finish date
is required.'); ?>
	<?php echo $html->tagErrorMsg('Quote/date_range_negative', 'Finish
date must be later than the start date.'); ?>
	<p><?php	echo
$html->selectTag('Whatever/size_id',$size_select,$selected_size,NULL,NULL,false,true)
;?></p>
	<p><?php	echo
$html->selectTag('Whatever/brand_id',$brand_select,$selected_brand,NULL,NULL,false,true)
;	?></p>
	<p><input type="submit" value="Show" /></p>
	</fieldset>
</form>




  

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to