lundi 2 mars 2015

Create URL from non-empty form parameters with CActiveForm

I am relatively new to both Yii and PHP and I have some problems while composing user-friendly URLs.


I want to create an URL from user-entered parameters in a form which is an extension of CFormModel. I intially chose the GET method instead of POST because the user should be able to bookmark the URL in order to return to the same search results later on.The user must specify (at least) one parameter for the search but since there are many possible parameters in the form, I want to shorten the URL by including only non-empty parameters and their values, e.g.,


http://localhost/search/results?name=John&country=Ireland


instead of


http://localhost/search/results?name=John&family=&country=Ireland&yt0=Search


(If someone knows how to exclude the button ID and label “yt0=Search”, that would quite nice, too.) I understand that passing all GET parameters to the URL is the standard behaviour of HTML forms and cannot be changed by using only PHP. Now I had the idea to add a JavaScript function that checks for all form parameters whether their values are empty after the form is submitted. If a parameter value is empty, the name of the parameter is set to an empty string (as suggested here) which effectively removes the empty parameters from the URL:



function myFunction()
{
var myForm = document.getElementById('form-id');
var allInputs = myForm.getElementsByTagName('input');
var input, i;

for(i = 0; input = allInputs[i]; i++) {
if(input.getAttribute('name') && !input.value) {
input.setAttribute('name', '');
}
}
}


However, I am not sure where to call this function (as opposed to “onsubmit” for a standard HTML form) and how to reference the form parameters since I am not yet familiar with CFormModel/CActiveForm. Any help would be greatly appreciated!


This is the (simplified) form model:



class SearchForm extends CFormModel {

private $_parameters = array (
'firstName' => array (
'type' => 'text',
'config'=>array('name'=>'name'),
),
'familyName' => array (
'type' => 'text',
'config'=>array('name'=>'family'),
),
'country' => array (
'type' => 'text',
'config'=>array('name'=>'country')
),
);

public $firstName;
public $familyName;
public $country;

public function getParameters() {
return $this->_parameters;
}
}


This is the relevant part of the view:



$elements = $model->getParameters ();

$form = $this->beginWidget ( 'CActiveForm', array (
'method'=>'get',
'enableAjaxValidation' => false
)
);


This is the action part of the controller:



public function actionResults() {
$model = new SearchForm ();

$filters = array ();
if (isset ($_REQUEST['name'])){
$filters['firstName'] = $_REQUEST['name'];
}
if (isset ($_REQUEST['family'])){
$filters['familyName'] = $_REQUEST['family'];
}
if (isset ($_REQUEST['country'])){
$filters['country'] = $_REQUEST['country'];
}

if ($filters) {
$model->attributes = $filters;

if ($model->validate ()) {
// search action
}
}
}


(I asked a similar but less specific question two weeks ago here.)


Aucun commentaire:

Enregistrer un commentaire