Wednesday, September 2, 2015

Re: Compare 2 date in cake3



Il giorno mercoledì 2 settembre 2015 10:20:53 UTC+2, Anthony GRASSIOT ha scritto:
Could you show your controller finction ?
these validation are used when using newEntity() or patchEntities'), not save(). 


the other validation condition works fine ; here the method:

    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create')
            ->add('social_user_id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('social_user_id')
            ->allowEmpty('name')
            ->allowEmpty('address')
            ->allowEmpty('city')
            ->add('lat', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('lat')
            ->add('lng', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('lng')
            ->add('start_date', 'valid', ['rule' => 'date'])
->add('end_date', [
            'valid'=> ['rule' => 'date'] ,
            'compare' => [
'rule' => function ($value, $context) {
$date_end = Time::createFromFormat('d/m/Y',$value);
$date_start = Time::createFromFormat('d/m/Y',$context['data']['start_date']);
return ($date_start < $date_end);
                    }
                    ]
             ]
    
            );
            
        return $validator;
    } 

In controller:
....
$calendar = $this->Calendars->patchEntity($calendar, $calendar_data);
.....
$calendar->start_date = implode("-",array_reverse(explode("/", $this->request->data['start_date'])));
$calendar->end_date = implode("-",array_reverse(explode("/", $this->request->data['end_date'])));
...
if(!$this->Calendars->save($calendar))
...

--
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: