Re: cake1.2 model validation
You shouldn't need to do that. The process is you call Model::save() Model::save calls validates validates calls invalidFields invalidFields iterates the validate array and set validationErrors via invalidate (in your ValidFunc you make a call to invalidate therby also setting validationErrors) invalidFields returns validationErrors to validates validates makes sure that validationErrors count is 0 validates returns false to save wich then returns false to your if ($model-save()) Then all happens as normal :- validationErrors is passed onto view in controller::render (line 639)) as long as the Model is one of the $uses. If you are instatiating the Model yourself it wont forward on the validationErrors Geoff -- http://lemoncake.wordpress.com On Jun 28, 3:22 pm, phpjoy [EMAIL PROTECTED] wrote: works like a charm.. here's the final code: var $validate = array( 'directory' = array('rule' = 'ValidFunc') ); function ValidFunc($value) { $this-data['ModelName']['fieldname']='new' .$value; case 1: $this-invalidate('fieldname', 'message1'); return true; case 2: $this-invalidate('fieldname', 'message2'); return true; } now here's the magic trick, in order to make it visible in the form in the view i should..: $this-data = $this-ModelName-data; otherwise it doesn't get changed in the controller and then displayed in the view. is there a way to change it from the model itself? On Jun 28, 1:05 am, francky06l [EMAIL PROTECTED] wrote: Yes, nice hack somehow :-) Thanks Geoff On Jun 28, 12:56 am, Geoff Ford [EMAIL PROTECTED] wrote: Just took at look at the code and your right franky, after you return false from your custom function invalidFields() then calls $this- invalidate() itself overwriting the message you just wrote. However this is not the case if you return true. This may be a bit wrong but you can get around this like so function ValidFun($ruleParams){ $fieldName = array_keys($ruleParams); $fieldName = $fieldName[0]; if ($conditions){ // all validation passed } else { // validation failed $this-invalidate($fieldName, 'Your Error Message'); } return true; } By always returning true invalidFields() will not set the error message itself, but the Model::validationErrors will still contain the invalid field with your error message and and so Model::validates() will return the correct value of false. On Jun 28, 8:45 am, francky06l [EMAIL PROTECTED] wrote: I am not sure that will work (I might be wrong) but it seems to me that the model-invalidate is called after your validFunc returning false (this call will oerwrite your message). I haven't tested, neither check the code deeply enough to know if the call to invalidate (into the validateField method) checks for an existing messages prior to call invalidate.. Let me know :-) Thanks On Jun 28, 12:30 am, Geoff Ford [EMAIL PROTECTED] wrote: Oops just reread the question. To set the error message from within the ValidFunc you can use the Model::invalidate($field, $value = null); On Jun 28, 8:26 am, Geoff Ford [EMAIL PROTECTED] wrote: if believe that you can change the error message with var $validate = array( 'directory' = array('error_mesasage' = array(array('rule' = 'ValidFunc', 'message'='message')) ); On Jun 28, 5:32 am, francky06l [EMAIL PROTECTED] wrote: I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ You received this message
cake1.2 model validation
ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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: cake1.2 model validation
I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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: cake1.2 model validation
if believe that you can change the error message with var $validate = array( 'directory' = array('error_mesasage' = array(array('rule' = 'ValidFunc', 'message'='message')) ); On Jun 28, 5:32 am, francky06l [EMAIL PROTECTED] wrote: I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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: cake1.2 model validation
I am not sure that will work (I might be wrong) but it seems to me that the model-invalidate is called after your validFunc returning false (this call will oerwrite your message). I haven't tested, neither check the code deeply enough to know if the call to invalidate (into the validateField method) checks for an existing messages prior to call invalidate.. Let me know :-) Thanks On Jun 28, 12:30 am, Geoff Ford [EMAIL PROTECTED] wrote: Oops just reread the question. To set the error message from within the ValidFunc you can use the Model::invalidate($field, $value = null); On Jun 28, 8:26 am, Geoff Ford [EMAIL PROTECTED] wrote: if believe that you can change the error message with var $validate = array( 'directory' = array('error_mesasage' = array(array('rule' = 'ValidFunc', 'message'='message')) ); On Jun 28, 5:32 am, francky06l [EMAIL PROTECTED] wrote: I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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: cake1.2 model validation
Just took at look at the code and your right franky, after you return false from your custom function invalidFields() then calls $this- invalidate() itself overwriting the message you just wrote. However this is not the case if you return true. This may be a bit wrong but you can get around this like so function ValidFun($ruleParams){ $fieldName = array_keys($ruleParams); $fieldName = $fieldName[0]; if ($conditions){ // all validation passed } else { // validation failed $this-invalidate($fieldName, 'Your Error Message'); } return true; } By always returning true invalidFields() will not set the error message itself, but the Model::validationErrors will still contain the invalid field with your error message and and so Model::validates() will return the correct value of false. On Jun 28, 8:45 am, francky06l [EMAIL PROTECTED] wrote: I am not sure that will work (I might be wrong) but it seems to me that the model-invalidate is called after your validFunc returning false (this call will oerwrite your message). I haven't tested, neither check the code deeply enough to know if the call to invalidate (into the validateField method) checks for an existing messages prior to call invalidate.. Let me know :-) Thanks On Jun 28, 12:30 am, Geoff Ford [EMAIL PROTECTED] wrote: Oops just reread the question. To set the error message from within the ValidFunc you can use the Model::invalidate($field, $value = null); On Jun 28, 8:26 am, Geoff Ford [EMAIL PROTECTED] wrote: if believe that you can change the error message with var $validate = array( 'directory' = array('error_mesasage' = array(array('rule' = 'ValidFunc', 'message'='message')) ); On Jun 28, 5:32 am, francky06l [EMAIL PROTECTED] wrote: I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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: cake1.2 model validation
Yes, nice hack somehow :-) Thanks Geoff On Jun 28, 12:56 am, Geoff Ford [EMAIL PROTECTED] wrote: Just took at look at the code and your right franky, after you return false from your custom function invalidFields() then calls $this- invalidate() itself overwriting the message you just wrote. However this is not the case if you return true. This may be a bit wrong but you can get around this like so function ValidFun($ruleParams){ $fieldName = array_keys($ruleParams); $fieldName = $fieldName[0]; if ($conditions){ // all validation passed } else { // validation failed $this-invalidate($fieldName, 'Your Error Message'); } return true; } By always returning true invalidFields() will not set the error message itself, but the Model::validationErrors will still contain the invalid field with your error message and and so Model::validates() will return the correct value of false. On Jun 28, 8:45 am, francky06l [EMAIL PROTECTED] wrote: I am not sure that will work (I might be wrong) but it seems to me that the model-invalidate is called after your validFunc returning false (this call will oerwrite your message). I haven't tested, neither check the code deeply enough to know if the call to invalidate (into the validateField method) checks for an existing messages prior to call invalidate.. Let me know :-) Thanks On Jun 28, 12:30 am, Geoff Ford [EMAIL PROTECTED] wrote: Oops just reread the question. To set the error message from within the ValidFunc you can use the Model::invalidate($field, $value = null); On Jun 28, 8:26 am, Geoff Ford [EMAIL PROTECTED] wrote: if believe that you can change the error message with var $validate = array( 'directory' = array('error_mesasage' = array(array('rule' = 'ValidFunc', 'message'='message')) ); On Jun 28, 5:32 am, francky06l [EMAIL PROTECTED] wrote: I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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: cake1.2 model validation
works like a charm.. here's the final code: var $validate = array( 'directory' = array('rule' = 'ValidFunc') ); function ValidFunc($value) { $this-data['ModelName']['fieldname']='new' .$value; case 1: $this-invalidate('fieldname', 'message1'); return true; case 2: $this-invalidate('fieldname', 'message2'); return true; } now here's the magic trick, in order to make it visible in the form in the view i should..: $this-data = $this-ModelName-data; otherwise it doesn't get changed in the controller and then displayed in the view. is there a way to change it from the model itself? On Jun 28, 1:05 am, francky06l [EMAIL PROTECTED] wrote: Yes, nice hack somehow :-) Thanks Geoff On Jun 28, 12:56 am, Geoff Ford [EMAIL PROTECTED] wrote: Just took at look at the code and your right franky, after you return false from your custom function invalidFields() then calls $this- invalidate() itself overwriting the message you just wrote. However this is not the case if you return true. This may be a bit wrong but you can get around this like so function ValidFun($ruleParams){ $fieldName = array_keys($ruleParams); $fieldName = $fieldName[0]; if ($conditions){ // all validation passed } else { // validation failed $this-invalidate($fieldName, 'Your Error Message'); } return true; } By always returning true invalidFields() will not set the error message itself, but the Model::validationErrors will still contain the invalid field with your error message and and so Model::validates() will return the correct value of false. On Jun 28, 8:45 am, francky06l [EMAIL PROTECTED] wrote: I am not sure that will work (I might be wrong) but it seems to me that the model-invalidate is called after your validFunc returning false (this call will oerwrite your message). I haven't tested, neither check the code deeply enough to know if the call to invalidate (into the validateField method) checks for an existing messages prior to call invalidate.. Let me know :-) Thanks On Jun 28, 12:30 am, Geoff Ford [EMAIL PROTECTED] wrote: Oops just reread the question. To set the error message from within the ValidFunc you can use the Model::invalidate($field, $value = null); On Jun 28, 8:26 am, Geoff Ford [EMAIL PROTECTED] wrote: if believe that you can change the error message with var $validate = array( 'directory' = array('error_mesasage' = array(array('rule' = 'ValidFunc', 'message'='message')) ); On Jun 28, 5:32 am, francky06l [EMAIL PROTECTED] wrote: I guess you can't do this using the model validation, but if your validation function is in the current model, you could replace the value $this-data['Model']['field'] and return true (in this way, this will not be considered as an error). If your goal is to correct the value, show it with a message, the above is still valid (for the value) but I have no idea how to change the message. You can have a look to the bakery (multiple validation), Mariano has done work about validation messages into the view-side, maybe there you could interact with it. On Jun 27, 9:11 pm, phpjoy [EMAIL PROTECTED] wrote: ahoy, i have the following validation in a model: var $validate = array( 'directory' = array('rule' = 'ValidFunc', 'message'='message') ); function ValidFunc($value) { $newvalue = 'new' .$value; $value = $newvalue; case 1: /*message=1*/ return false; case 2: /*message=2*/ return false; } there are 2 things i'm struggling to do: 1) change the validation message from the ValidFunc, 2) change $value for the form to $newvalue. anyone knows how i can do that? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---