Πέμπτη 6 Δεκεμβρίου 2018

Laravel Custom Validation "comma separated*

Do you need to validate that something is a comma separated string and each entry should be then validated that it is a valid email, phone etc?

Simply add the following in your  AppServiceProvider's boot function:

        Validator::extend('comma_separated', function ($attribute, $value, $parameters, $validator) {
            $entryRules = $parameters[0];
            $parts = explode(",", $value);
            if ($entryRules) {
                foreach ($parts as $part) {
                    $entryValidator = 
                        validator(
                            ["value" => $part], 
                            ["value" => $entryRules], 
                            [],  
                            ["value" => $validator->getDisplayableAttribute($attribute) . " > Value"]
                        );
                    if ($entryValidator->fails()) {
                        foreach ($entryValidator->errors()->getMessages() as $validationErrors) {
                            if (is_array($validationErrors)) {
                                foreach ($validationErrors as $validationError) {
                                    $validator->errors()->add($attribute, $validationError);
                                }
                            }
                            else {
                                $validator->errors()->add($attribute, $validationErrors);
                            }
                        }
                        return FALSE;
                    }
                }
            }
            return TRUE;
        });

And use it like in the following snippet:

    $something = "something";
    $validator = validator(["value" => $something], ["value" => "comma_separated:email"]);

In my project, I have defined it in the $rules of my model class:

    public static $rules = [
      "datetime_from" => "required|date",
      "datetime_to" => "required|date|after_or_equal:datetime_from",
      "attendees" => "required|comma_separated:email",
      "title" => "required|min:3|max:255",
      "description" => "max:2000",
    ];

Δευτέρα 22 Οκτωβρίου 2018

Διαχείριση προσωπικών δεδομένων & επικοινωνιών.

Όλοι δεχόμαστε άπειρα τηλεφωνήματα για προώθηση προϊοντων και προσφορές του παρόχου μας ή ανταγωνιστών. Αυτό όμως γινόταν ασύστολα μέχρι που εμφανίστηκε το GDPR.

Το GDPR είναι ο φόβος και ο τρόμος των προωθήσεων καθώς πρέπει να έχουν την ενεργητική συνένεση του καταναλωτή ώστε να μπορούν να του προωθούν προϊόντα. Αυτός ο ευρωπαϊκός κανονισμός ανάγκασε όλες τις μεγάλες εταιρείες να δώσουν την δυνατότητα στους πελάτες να επιλέγουν αν και ποιες ενέργειες "παρακολούθησης" και "επικοινωνίας" επιτρέπονται.

Μια πάρα πολύ ωραία υλοποίηση, είναι αυτή που έχει γίνει στην εφαρμογή του Vodafone CU. Πραγματικά με ενθουσίασε το πόσο αναλυτικά έχουν τις επιλογές, αλλά και ότι σου βγάζει ένα τίμιο μήνυμα ότι μπορεί να χρειαστούν 24 ώρες για να ενεργοποιηθούν οι ρυθμίσεις.

Long Story Short:
  1. Εχεις Vodafone CU?
  2. Σε ενοχλούν με διαφημίσεις;
  3. Κατέβασε την εφαρμογή (Google Play, Apple App Store
  4. Ανοιξε την
  5. Συνδεσου
  6. Πηγαινε Μενου > Ρυθμίσεις > Διαχείριση προσωπικών δεδομένων & επικοινωνιών
  7. Επέλεξε τι θέλεις και αποθήκευσε τις ρυθμίσεις σου


Σημειωση: Το έκανα σήμερα, οποτε τις επόμενες ημέρες θα δω αν θα με ενοχλήσουν ή οχι! Ελπίζω να μην με ενοχλήσουν και να επιβεβαιώσει η Vodafone ότι ειναι σοβαρη εταιρεια που σέβεται τον καταναλωτη!

Τετάρτη 11 Ιουλίου 2018

[Laravel Hack] venturecraft/revisionable to include details

As a backend development, you might have been asked to keep a history of changes. A very good library that does so in Laravel, with minimum configuration, is the venturecraft/revisionable. A limitation of this library is that it is limited to whatever is stored in the table that is related to each model. 

There are several situations that this is not enough. For instance, you might have a Model "PriceTable" that is related to many "ServiceTypePrice" where "ServiceTypePrice" is stored in a separate table and they have a One-to-Many relationship.

My simplest solution was to convert define the relationship in the model class using "hasMany" and also create a function that converts the data of hasMany into a readable string. So I override method preSave that Revisionable uses, in order to include the result of getDetails() as if it is was a actual attribute of the table "PriceTable".

It should be noted that I have decided this solution since I have pretty small number of entries in the table "ServieTypePrice" for each "PriceTable", otherwise I would select a more sophisticated solution that I have already though of but I might test and document in a future post!

preSave code included in my "PriceTable" Model class is the following:
 
use \Venturecraft\Revisionable\RevisionableTrait; 
public function preSave() {
        if (!isset($this->revisionEnabled) || $this->revisionEnabled) {
            // if there's no revisionEnabled. Or if there is, if it's true
            $this->originalData = $this->original;
            $this->updatedData = $this->attributes;

            /**
             * Append details
             */
            if ($this->originalData) {
                $originalModel = static::find($this->originalData["id"]);
                $this->originalData["details"] = $originalModel->getDetails();
            }
            $this->updatedData["details"] = $this->getDetails();

            // we can only safely compare basic items,
            // so for now we drop any object based items, like DateTime
            foreach ($this->updatedData as $key => $val) {
                if (gettype($val) == 'object' && !method_exists($val, '__toString')) {
                    unset($this->originalData[$key]);
                    unset($this->updatedData[$key]);
                    array_push($this->dontKeep, $key);
                }
            }
            // the below is ugly, for sure, but it's required so we can save the standard model
            // then use the keep / dontkeep values for later, in the isRevisionable method
            $this->dontKeep = isset($this->dontKeepRevisionOf) ?
                array_merge($this->dontKeepRevisionOf, $this->dontKeep) : $this->dontKeep;
            $this->doKeep = isset($this->keepRevisionOf) ?
                array_merge($this->keepRevisionOf, $this->doKeep) : $this->doKeep;
            unset($this->attributes['dontKeepRevisionOf']);
            unset($this->attributes['keepRevisionOf']);
            $this->dirtyData = $this->getDirty();
            /**
             * Check Details
             */
            if ($this->originalData == NULL || strcmp((string) $this->updatedData["details"], (string) $this->originalData["details"]) !== 0) {
                $this->dirtyData["details"] = $this->updatedData["details"];
            }
            $this->updating = $this->exists;
        }
    }

Σάββατο 27 Ιανουαρίου 2018

Τηλεφωνική απάτη

Πολλές φορές έχω ακούσει και διαβάσει αναφορικά με μια τηλεφωνική απάτη που είναι της μόδας. Πρόκειται για τηλέφωνα εξωτερικού που κάνουν αναπάντητες ώστε να τα καλέσεις, αλλά αν τα καλέσεις έχουν υψηλή χρέωση. Ένας τέτοιος αριθμός είναι και το +38649051439 που δεν μπαίνω στην διαδικασία να δοκιμάσω να καλέσω καθώς είδα ότι το αφήνουν να κρυπαει μόνο μια φορά!

LinkWithin

Blog Widget by LinkWithin

Mobile edition