Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Tutorial 4: Control Search and Filter Form

Tutorial 4: Control Search and Filter Form

On the Post Home Screen, the search and filter form is Search and Filter forms are added by default. Following steps You can follow steps to control the search Search form of each resource.  

Step 1: Create PostSearchForm class to control search options

 
PostSearchForm.php

Code Block
<?php

...


namespace Apps\Posts\Api\Form;

...


use Apps\Core_MobileApi\Api\Form\SearchForm;

...

 
class PostSearchForm extends SearchForm

...


{

...

 
	public function getSortOptions()

...


	{
		$sortOptions = parent::getSortOptions();

...


		$sortOptions[] = [

...


		'value' => 'is_featured',

...


		'label' => 'Is Featured'

...


		];

...

 
		return $sortOptions;

...

 
	} 
	public function getWhenOptions()

...


	{
		return parent::getWhenOptions();

...


	}

...

 
}

...

Step 2: Create Get Search From API

Improve PostApi class and add by adding the following code
PostApi.php


Code Block
<?php

...


namespace Apps\Posts\Api\Service;

...

 
use Apps\Core_MobileApi\Adapter\MobileApp\MobileApp;

...


use Apps\Core_MobileApi\Adapter\MobileApp\MobileAppSettingInterface;

...


use Apps\Core_MobileApi\Adapter\MobileApp\Screen;

...


use Apps\Core_MobileApi\Adapter\Utility\UrlUtility;

...


use Apps\Core_MobileApi\Api\AbstractResourceApi;

...


use Apps\Posts\Api\Form\PostForm;

...


use Apps\Posts\Api\Form\PostSearchForm;

...


use Apps\Posts\Api\Resource\PostResource;

...

 


class PostApi extends AbstractResourceApi implements MobileAppSettingInterface

...


{

...

 
	public function __naming()

...


	{
		return [
			'post/search-form' => [

...


				'get' => 'getSearchForm'

...


			]
		]; 
	} 
	public function getSearchForm()
	{
		$form = $this->createForm(PostSearchForm::class, [

...


			'title' => 'search',

...


			'method' => 'GET',

...


			'action' => UrlUtility::makeApiUrl('post')

...


			]);

...

 
		return $this->success($form->getFormStructure());

...


	}

...


	/* other code... */

...


}


 

Explain the code above:

  • Method __naming() of PostApi class allows defining more API routes, Route to search Search form much MUST follow standard "{resource_name}/search-form"
  • Method getSearchForm() handle handles API request and returns the response with the form's structure in JSON

 

Step 3: Configure Post Resource setting

Override You can override method getMobileSettings() in PostResource class allow us to configure for allows configuring each resource. The code below configures to will change the placeholder string, modify sort and filter options
PostResource.php

Code Block
<?php

...

 
namespace Apps\Posts\Api\Resource;

...

 
use Apps\Core_MobileApi\Api\Resource\ResourceBase;

...


use Apps\Posts\Api\Form\PostSearchForm;

...

 


class PostResource extends ResourceBase

...


{

...


	public $resource_name = "post";

...


	public $module_name = "post";

...


	public $title;

...


	public $description;

...


	public $text;

...

 
	
	public function getMobileSettings($params = [])

...


	{
		$searchFilter = (new PostSearchForm());

...


		$searchFilter->setLocal($this->getLocalization());

...

 
		return self::createSettingForResource([

...


			'resource_name' => $this->resource_name,

...


			'search_input' => [

...


				'placeholder' => 'Search Posts'

...


			],

...


			'sort_menu' => $searchFilter->getSortOptions(),

...


			'filter_menu' => $searchFilter->getWhenOptions()

...


		]);

...


	}

...


}

...

Step 4: Test result

...

on Mobile App

Image Modified          Image Modified

...

 Step 5: handle the search form submission

When a user uses the search or filter menu, The the Mobile App will submit an API request to Get get All API APIs with all form's parameters. All parameters are auto-resolve resolved and pass passed to the findAll() method as an array.