Criteria::fromInput

Hi helpers and gurus, I was trying vokuru and added some other elements in users search form and ofcourse in Users model too. In searching, I am not getting my "city" dropdown in the conditional parameters when I submit the form. Profiles does show up in the conditions and other text elements that I added to the original vokuru are also working. Only issue is with one dropdown i.e. city selector

DB changes added string column "mobile number" added int column "city_id"

Testing: vardump($this->persistent->searchParams); die(vardump($this->request->getPost()));

getPost() does include city_id, but $this->persistent->searchParams does not include it.

Any help will be appreciated.



3.0k

Hello

Can you post model and form code ?

Users Model

<?php namespace Vokuro\Models;

use Phalcon\Validation; use Phalcon\Validation\Validator\Email as EmailValidator; use Phalcon\Validation\Validator\Uniqueness;

class Users extends \Phalcon\Mvc\Model {

/**
 *
 * @var integer
 * @Primary
 * @Identity
 * @Column(type="integer", length=10, nullable=false)
 */
public $id;

/**
 *
 * @var string
 * @Column(type="string", length=255, nullable=false)
 */
public $name;

/**
 *
 * @var string
 * @Column(type="string", length=255, nullable=false)
 */
public $email;

/**
 *
 * @var string
 * @Column(type="string", length=60, nullable=false)
 */
public $password;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=true)
 */
public $mustChangePassword;

/**
 *
 * @var integer
 * @Column(type="integer", length=10, nullable=false)
 */
public $profilesId;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=false)
 */
public $banned;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=false)
 */
public $suspended;

/**
 *
 * @var string
 * @Column(type="string", length=1, nullable=true)
 */
public $active;

/**
 *
 * @var string
 * @Column(type="string", length=12, nullable=true)
 */
public $oc_number;

/**
 *
 * @var string
 * @Column(type="string", length=14, nullable=false)
 */
public $mobile_no;

/**
 *
 * @var string
 * @Column(type="string", length=1000, nullable=false)
 */
public $postal_address;

/**
 *
 * @var string
 * @Column(type="string", length=13, nullable=false)
 */
public $cnic;

/**
 *
 * @var string
 * @Column(type="string", nullable=false)
 */
public $dob;

/** * * @var integer * @Column(type="integer", length=10, nullable=false) */ public $city_id;

/** * Before create the user assign a password */ public function beforeValidationOnCreate() { if (empty($this->password)) {

        // Generate a plain temporary password
      $tempPassword = preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(openssl_random_pseudo_bytes(12)));

      // The user must change its password in first login
      $this->mustChangePassword = 'Y';

      // Use this password as default
      $this->password = $this->getDI()
          ->getSecurity()
          ->hash($tempPassword);
  } else {
      // The user must not change its password in first login
      $this->mustChangePassword = 'N';
  }

  // The account must be confirmed via e-mail
  // Only require this if emails are turned on in the config, otherwise account is automatically active
  if ($this->getDI()->get('config')->useMail) {
      $this->active = 'N';
  } else {
      $this->active = 'Y';
  }

  // The account is not suspended by default
  $this->suspended = 'N';

  // The account is not banned by default
  $this->banned = 'N';
}

/** * Send a confirmation e-mail to the user if the account is not active */ public function afterSave() { // Only send the confirmation email if emails are turned on in the config if ($this->getDI()->get('config')->useMail) {

        if ($this->active == 'N') {

          $emailConfirmation = new EmailConfirmations();

          $emailConfirmation->usersId = $this->id;

          if ($emailConfirmation->save()) {
              $this->getDI()
                  ->getFlash()
                  ->notice('A confirmation mail has been sent to ' . $this->email);
          }
      }
  }
}

/**
 * Validate that emails are unique across users
 */
public function validation()
{
  $validator = new Validation();

  $validator->add('email', new Uniqueness([
      "message" => "The email is already registered"
  ]));

  return $this->validate($validator);
}

public function initialize()
{
  $this->belongsTo('profilesId', __NAMESPACE__ . '\Profiles', 'id', [
      'alias' => 'profile',
      'reusable' => true
  ]);

  $this->hasMany('id', __NAMESPACE__ . '\SuccessLogins', 'usersId', [
      'alias' => 'successLogins',
      'foreignKey' => [
          'message' => 'User cannot be deleted because he/she has activity in the system'
      ]
  ]);

  $this->hasMany('id', __NAMESPACE__ . '\PasswordChanges', 'usersId', [
      'alias' => 'passwordChanges',
      'foreignKey' => [
          'message' => 'User cannot be deleted because he/she has activity in the system'
      ]
  ]);

  $this->hasMany('id', __NAMESPACE__ . '\ResetPasswords', 'usersId', [
      'alias' => 'resetPasswords',
      'foreignKey' => [
          'message' => 'User cannot be deleted because he/she has activity in the system'
      ]
  ]);

 $this->belongsTo( 'city_id', __NAMESPACE__ . '\Cities', 'id', [
     'alias' => 'city',
     'reusable' => true
 ]);
}

}

and UsersForm

<?php namespace Vokuro\Forms;

use Phalcon\Forms\Form; use Phalcon\Forms\Element\Text; use Phalcon\Forms\Element\Hidden; use Phalcon\Forms\Element\Select; use Phalcon\Forms\Element\Date as DateElement; use Phalcon\Validation\Validator\PresenceOf; use Phalcon\Validation\Validator\Email; use Phalcon\Validation\Validator\Date as DateValidator; use Vokuro\Models\Profiles; use Vokuro\Models\Cities;

class UsersForm extends Form {

public function initialize($entity = null, $options = null)
{

  // In edition the id is hidden
  if (isset($options['edit']) && $options['edit']) {
      $id = new Hidden('id');
  } else {
      $id = new Text('id');
  }

  $this->add($id);

  $name = new Text('name', [
      'placeholder' => 'Name'
  ]);

  $name->addValidators([
      new PresenceOf([
          'message' => 'The name is required'
      ])
  ]);

  $this->add($name);

  $email = new Text('email', [
      'placeholder' => 'Email'
  ]);

  $email->addValidators([
      new PresenceOf([
          'message' => 'The e-mail is required'
      ]),
      new Email([
          'message' => 'The e-mail is not valid'
      ])
  ]);

  $this->add($email);

  $profiles = Profiles::find([
      'active = :active:',
      'bind' => [
          'active' => 'Y'
      ]
  ]);

  $this->add(new Select('profilesId', $profiles, [
      'using' => [
          'id',
          'name'
      ],
      'useEmpty' => true,
      'emptyText' => '...',
      'emptyValue' => ''
  ]));

  $this->add(new Select('banned', [
      'Y' => 'Yes',
      'N' => 'No'
  ]));

  $this->add(new Select('suspended', [
      'Y' => 'Yes',
      'N' => 'No'
  ]));

  $this->add(new Select('active', [
      'Y' => 'Yes',
      'N' => 'No'
  ]));

 $ocNo = new Text('oc_number', [
      'placeholder' => 'OneCard Number'
  ]);

 $this->add($ocNo);

 $mobNo = new Text('mobile_no', [
      'placeholder' => 'Mobile'
  ]);

  $mobNo->addValidators([
      new PresenceOf([
          'message' => 'Mobile no is required'
      ])
  ]);

  $this->add($mobNo);

 $postalAddress = new Text('postal_address', [
      'placeholder' => 'Address'
  ]);

  $postalAddress->addValidators([
      new PresenceOf([
          'message' => 'Address is required'
      ])
  ]);

  $this->add($postalAddress);
     /*  
 $cities = Cities::find([
      'city = :city:',
      'bind' => [
          'active' => 'Y'
      ]
  ]);
 */
 $this->add(new Select('city_id', Cities::find(), array( 'using' => array( 'id', 'city' ), 
     'useEmpty' => true,
      'emptyText' => 'Select City',
      'emptyValue' => ''
 )));

 $cnic = new Text('cnic', [
      'placeholder' => 'CNIC#'
  ]);

  $cnic->addValidators([
      new PresenceOf([
          'message' => 'CNIC is required'
      ])
  ]);

  $this->add($cnic);

 $dob = new DateElement('dob', [
      'placeholder' => 'Date of Birth'
  ]);

  $dob->addValidators([
      new PresenceOf([
          'message' => 'Date of Birth is required'
      ]),
     new DateValidator(
         [
             "message" => "The date is invalid",
         ]
     )
  ]);

  $this->add($dob);
}

}

Thanks



3.0k
edited Aug '17

hello

Not be sure but i suspect camelCase problem

You can try this in form

 $this->add(new Select('cityId', Cities::find(), array( 'using' => array( 'id', 'city' ), 
     'useEmpty' => true,
      'emptyText' => 'Select City',
      'emptyValue' => ''
 )));

You have to change the name in your template city_id to cityId

other way See this thread

Most probably, the issue was I was not using the same name i.e. city_id in controller.

Anyways, its solved now.