Thursday, November 19, 2015

On-the-fly Symfony form for searches

Version 1: Typeless form

Inside the controller:
$q = $request->query->get('q');
$data = array('q' => $q); 
$searchForm = $this->get('form.factory')
    ->createNamedBuilder('', 'form', $data, array(
        'csrf_protection' => false,
        'method' => 'GET',
    ))
    ->add('q', 'text', array(
        'required' => false,
    ))
    ->getForm();

if($q) {
    $data = $searchForm->handleRequest($request)->getData();
    $qb
        ->andWhere($qb->expr()->like('name',  ':q'))
        ->setParameter('q', $data['q']);
} 
return $this->render('@Bundle:Entity:index.html.twig', array(
            'searchForm'    => $searchForm->createView()
        ));

Version 2:  using FormType

Inside controller:
$filterForm = $this->createForm(new SomeFilterType(), null, array(
    'method' => 'GET',
));

$filterForm->handleRequest($request);
if ($filterForm->isValid()) {
    $name = $filterForm->get('name')->getNormData();
//
}

Wednesday, November 18, 2015

Failed to download zendframework/zend-stdlib from dist

Problem

# php composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
  - Removing zendframework/zend-stdlib (2.5.1)
  - Installing zendframework/zend-stdlib (2.2.3)
    Downloading: Connecting...    Failed to download zendframework/zend-stdlib from dist: The "https://api.github.com/repos/zendframework/Component_ZendStdlib/zipball/7c87ce4e840957596bf3401fa4ae4fb0355682e2" file could not be downloaded (HTTP/1.1 404 Not Found)
    Now trying to download from source
  - Installing zendframework/zend-stdlib (2.2.3)
    Cloning 7c87ce4e840957596bf3401fa4ae4fb0355682e2


                                                                                                                   
  [RuntimeException]                                                                                               
  Failed to clone git@github.com:zendframework/Component_ZendStdlib.git via git, https, ssh protocols, aborting.   
  - git://github.com/zendframework/Component_ZendStdlib.git                                                        
    error: The requested URL returned error: 403 Forbidden while accessing https://github.com/zendframework/Compo  
  nent_ZendStdlib.git/info/refs  

Steps to solve:

1.  Verify the connection to github

# ssh -T git@github.com  Permission denied (publickey).
2. Verify that ssh agent is running

# eval "$(ssh-agent -s)"  
Agent pid 100 
3. Verify that the key is loaded into the ssh agent

# ssh-add -l
The agent has no identities.
# ssh-add   
Identity added: /home/{user}/.ssh/id_rsa (/home/{user}/.ssh/id_rsa) 
4. Set-up github's credentials for local computer
# git config --global user.name "{githubuser}"
then copy the local key from  /home/{user}/.ssh/id_rssa.pub into {githubuser}'s account (Settings->SSH keys->Add SSH key)

then verify it:
# ssh -T git@github.com
Hi {githubuser}! You've successfully authenticated, but GitHub does not provide shell access.
5. Run composer update for the specific version you want
# php composer.phar require zendframework/zend-stdlib 2.2.3