0

I try to create the simple api using apigility to save some data. I have the table structure as below. Using apigility admin ui, I created the Resetful post service for below entity. createdAt field is optional but I want make sure the field should filed by current datetime when it's blank in request. Can someone give procedure to do it.

id integer,
name string,
createdAt datetime,
updatedAt timestamp,

Here is how admin field setup looks.

I also like to know how to set the validators to allow user to enter the createdAt as "Y-m-d H:i:s" format. can you guide me please enter image description here

1 Answer 1

1

There are a number of ways you could go about doing this:

1) Make the default to some date or date function in your persistence layer (as in make it default to now() or something in your database).

2) In your resource's create, update, patch, etc, check if the field has been passed in. If not, create a default timestamp to inject. You could also do it further down in your dependency layers, like in your service, mapper, or other resource dependency, where ever it makes sense.

3) Create something like a NullToDefaultDate filter. It will run on your null field and change the null to the default date you provide before it gets into your apigility code. Be sure you're either getting the filtered values from the injected InputFilter through $this->getInputFilter()->getValues(); or similar or you have set up use_raw_data = false in your module.config.php in the zf-content-validation section.

'zf-content-validation' => array( 'YourNamespace\\V1\\Rest\\YourServiceName\\Controller' => array( 'input_filter' => 'YourNamespace\\V1\\Rest\\YourServiceName\\Validator', 'use_raw_data' => false, 'allows_only_fields_in_filter' => true, ) )

If you put in just 'use_raw_data' => false, you'll get the filtered values for the fields you've defined in Apigility, but you'll also get unfiltered additional "garbage" fields that you've not told Apigility about at all. If you don't want those, you can enable allows_only_fields_in_filter which will only give you back fields you've defined, but now your API return an ApiProblem error when a field that was not in the list is passed in. Using $data = $this->getInputFilter()->getValues() in your methods will give you back just the fields you've defined and it will give you the filtered values as well.

For the second part of your question, you want to use the Zend\Validator\Date validator and add a format option of 'Y-m-d H:i:s'.

Apigility Admin Format for Date Validator

Be sure to click Add Option before save or it won't keep it.

Hope this helps.

3
  • I don't see any service, mapper files in side my module. I don't see this NullToDefaultDate filter also not available. can you be brief on it
    – Gowri
    Jan 12, 2016 at 9:30
  • @Gowri NullToDefaultDate filter would be a filter you would need to create. The service, mapper, etc are intended to stand in place of whatever dependencies you've injected in your resource in order to make your API do what it needs to do. If you're doing all the work in the method in the resource, then that's where the changes would go. Jan 13, 2016 at 1:44
  • I created NullToDefaultDate filter that fixed my problem. I have many questions about apiglity. where can I set quick help.
    – Gowri
    Jan 20, 2016 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.