Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Is partial validation against a data model possible?
  • Let's say that I have an API that spits out user data. For instance:

    /api/v1/user/get/5

    {
        "5": {
            "id": "5",
            "name": "Ben",
            "city": "Indianapolis",
            "state": "Indiana",
            "created_at": "1361835599",
            "updated_at": "1362607828",
            "deleted_at": null
        }
    }

    I already have a data model (which extends \Orm\Model) with validation rules. Is it possible to use the validate() method from my model without passing all required fields back to it? That is, if I am only updating the user "city", is there any way to pass only:

    {
    "id":5,
    "city":"Bloomington"
    }

    to my data model for validation? Or would I have to create a new validation method?
  • HarroHarro
    Accepted Answer
    You can, but you'll have to remove the "required" rule after you've created the validation object:

    // create a validation object, and add the model rules to it
    $val = Validation::forge('userapi');
    $val->add_model('Model_Users');

    // remove the required rule from the 'name' field
    $val->field('name')->delete_rule('required');

    // run the validation
    if ($val->run())
    {
       // and so on...
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion