Thursday, October 23, 2014

Re: validation criteria at least 3 fileds are not empty

I checked to cookbook and find out that validation handles singe fields. So I used beforeSave for this.

public function beforeSave(Event $event, Entity $entity, ArrayObject $options){
   
if((!empty($entity->name) + !empty($entity->contactname) + !empty($entity->zip_id)
         
+ !empty($entity->address) + !empty($entity->phone) + !empty($entity->email)
         
+ !empty($entity->birth->time)) >= 3){
       
return true;
   
}
   
else{
       
//Error: __('At least 3 info should be filled');
       
return false;
   
}
}

But I have a problem and a question.

The above gives me this error message:
Warning (4096): Argument 1 passed to App\Model\Table\ContactsTable::beforeSave() must be an instance of App\Model\Table\Event, instance of Cake\Event\Event given, called in /home/rrd/public_html/sanga/vendor/cakephp/cakephp/src/Event/EventManager.php on line 266 and defined [APP/Model/Table/ContactsTable.php, line 85]

What is the proper way to do it?

The question is how to define an error message what will be flashed to the user?




2014. október 18., szombat 15:41:28 UTC+2 időpontban John Andersen a következőt írta:
Assuming you are still using CakePHP 3.0

I have not seen any built-in rules for this kind of validation, so my best guess is to roll your own.
See section "Custom Validation Rules" under "Validation" in the CakePHP 3.0 book. That should help you on the way.

Enjoy, John

On Saturday, 18 October 2014 16:21:57 UTC+3, Radharadhya Dasa wrote:
Hi,

Is there a way to check if at least 3 fileds are not empty? I have 8 fileds and any of those could be empty, but at least 3 of them should be not empty. Is there any build in validation rule, or how to do it? Should I do it on beforeSave?

rrd

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: