Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

php - cakephp, validate error

I updated some data by inserting it into the fields and clicking on the update button, but validates() is always returning false. All the fields are filled correctly, and it does not display any error messages - validates() just returns false. Why?

$this->Post->set($this->data);
if ($this->Post->validates())
    echo 'ok';
else
    echo 'error';
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check this post for some tips. The relevant ones are given here.

  1. Save() does not work! Sometimes it happens that save() fails without any obvious reason. Your data array looks fine and you’ve built the form correctly, etc., etc., but no query was executed. It is very possible that save had failed due to validation errors. Maybe you are updating some model and, while the current fields in the form pass the validation, there is a chance that some “other ones” are causing the validation rules to fail. An easy (and helpful) way to see what’s going on with validation is to do pr($this->validationErrors); in your view. By employing this method you’ll see exactly what’s happening with your model’s validation. The other option is to pass false as a second parameter to save(); in order to disable the validation. However, the latter method does not give much of a hint and should not be used to fix a failing problem, but rather to purposely avoid validation.

  2. Save() still does not work! Do you have beforeSave(); in your model or app model? Always double-check for this method, and even more importantly, ensure that it returns true.

  3. Validating on create or update CakePHP has an 'on' key to be used in your $validate array. It allows you to specify whether the rule should be enforced during a new record creation or during an update of an existing record. For example, if you only want to check for a unique email address when you are creating a new User account, you would add 'on' => 'create' to your $validate array. Therefore, this rule will be ignored when you are updating/editing some user.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...