Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Only validate changed fields #273

Open
simplenotezy opened this issue Jul 28, 2015 · 1 comment
Open

Only validate changed fields #273

simplenotezy opened this issue Jul 28, 2015 · 1 comment

Comments

@simplenotezy
Copy link

Say I have a user model like this:

class User {
    public static $rules = [
        'email'         => 'sometimes|required|unique:users,email|email',
        'firstname'         => 'sometimes|required|min:1',
        'lastname'      => 'sometimes|required|min:1',
    ]; 

}

The user is already saved in database, but without e-mail, firstname, or lastname.

The user wish to update ONLY his e-mail. When he sends the request, only containing an e-mail field, the validation will fail, because he did not provide an firstname and lastname.

How can I avoid that? I.e. only validate the fields that are sent in the request; so if firstname is not sent, Ardent will not validate it, and instead only validate the fields that are present.

@simplenotezy
Copy link
Author

My current, very hacky, workaround

/**
 * Only validate the changed fields
 * @return [type] [description]
 */

    public function validateChangedFields() {

        $rules = [];

        foreach($this->getDirty() as $attribute => $value){
            $original= $this->getOriginal($attribute);
            //echo "Changed $attribute from '$original' to '$value'<br/>\r\n";

            $rules[$attribute] = static::$rules[$attribute];
        }

        return $this->validate($rules);
    }

It might explain better what I am trying to achieve.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant