mercredi 4 mai 2016

Validation rule without a field, it's possible?

I've implemented the Validation layer in my application. So i have a FooValidation class and a method deleting() that performs some validations on the registry before the deletion.

For now, i'm adding the custom rule for deletion exactly like this:

// FooValidation.php
public function deleting()
{
    $this->validator->addExtension('can_be_deleted', function($attribute, $value, $parameters) {
        return $this->canBeDeleted($value);
    });

    // where 'id' is the primary key of the entity
    $this->validator->setRules(['id' => 'can_be_deleted']);
    $this->validator->setData(['id' => $id]);

    if ($this->validator->fails()) {
        throw new ValidationException($this->validator);
    }
}

I want to know if there is a way to set a kind of "custom error" on the Validator without referencing a specific field. That way i will be able to remove those calls to addExtension(), setRules() and setData() in my method.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1Tn1wVf
via IFTTT

Aucun commentaire:

Enregistrer un commentaire