how to sign up with different fields in kohana - kohana-3

The Model_Auth_User class in Kohana uses 'username', 'email','password' to create a new user
what if i want it to take only 'email', 'password' and also modify the validation to validate 'email_confirm' instead of 'password_confirm'

Finally i did it, All what I have to doe is to comment some lines which add the rules of validating user input
open C:\xampp\htdocs\kohana\modules\orm\classes\Model\Auth\User.php
and comment lines from 33:38 inclusive as following:
public function rules()
{
return array(
//as we don't have a username we don't need to validate it!
// 'username' => array(
// array('not_empty'),
// array('max_length', array(':value', 32)),
// array(array($this, 'unique'), array('username', ':value')),
// ),
'password' => array(
array('not_empty'),
),
'email' => array(
array('not_empty'),
array('email'),
array(array($this, 'unique'), array('email', ':value')),
),
);
}
You only keep the rules for validating what you need

Avoid changing of the system folder contents. Otherwise your changes will be lost after the next upgrade.
The more correct approach is to override the validation rules.
In file application/classes/Model/user.php:
<?php
class Model_User extends Model_Auth_User
{
public function rules()
{
$rules = parent::rules();
unset($rules['username']);
return $rules;
}
}
?>

Related

How to use select query for creating form in drupal 8?

I have a table in my database. I want to use the data of that table and create form of that data. I created a module and made a form.php page. Wrote select query in function but how to make form from that data?
I believe you want to create listing page of the table data. Here is how you can create a tabular listing with pagination and column sorting.
Suppose you have a table named "students" with the below fields.
id, name, email
1. Create a controller in your module in the below path.
modules/your_module/Src/Controller/StudentsController.php
<?php
namespace Drupal\your_module\Controller;
use Drupal\Core\Controller\ControllerBase;
class StudentsController extends ControllerBase {
public function __construct() {
}
public function list() {
$header = array(
array('data' => t('ID'), 'field' => 'st.id'),
array('data' => t('Name'), 'field' => 'st.name'),
array('data' => t('Email'), 'field' => 'st.email'),
);
$query = db_select('students', 'st')
->fields('st', array('id', 'name', 'email'))
->extend('Drupal\Core\Database\Query\TableSortExtender')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->orderByHeader($header);
$data = $query->execute();
$rows = array();
foreach ($data as $row) {
$rows[] = array('data' => (array) $row);
}
$build['table_pager'][] = array(
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
);
$build['table_pager'][] = array(
'#type' => 'pager',
);
return $build;
}
}
So your controller action is ready, now you have to add routing in order to create path to this listing page.
2. Create routing.yml file inside your module folder as the file name mentioned below, and the code below.
modules/your_module/your_module.routing.yml
students.list:
path: 'admin/config/students/list'
defaults:
_controller: 'Drupal\your_module\Controller\StudentsController::list'
_title: 'Students List'
requirements:
_permission: 'access students list'
3. To create permissions, you can create the followin ing file in your module.
modules/your_module/your_module.permissions.yml
access students list:
title: 'Access students list page'
Clear CMS cache
Go to People => Permissions, Enable the permission for relevant user roles.
Then, browse your page "admin/config/students/list"

Symfony2 SonataAdminBundle Password field encryption

I have FOSUserBundle to manage my users, and SonataAdminBundle to manage my website... I have a problem, whenever I try to change/add a password to any user, the password isn't encoded into sha512, but it does when the user register itself inside fosuserbundle registration page...
So there isn't any problem with Symfony2 configuration neither fosuserbundle config, it may be inside SonataAdminBundle somewhere, or maybe into my admin class...
<?php
// src/Acme/DemoBundle/Admin/PostAdmin.php
namespace Web\DificilBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Web;
class UserAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('firstname')
->add('lastname')
->add('username')
->add('email')
->add('password', 'password') // -> I WANT THIS TO BE ENCODED INTO SHA512!
->add('roles','choice',array('choices'=>$this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'),'multiple'=>true ));
//->add('body')
;
}
// Fields to be shown on filter forms
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('firstname')
->add('lastname')
->add('username')
->add('email')
->add('password')
;
}
// Fields to be shown on lists
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->add('firstname')
->add('lastname')
->add('username')
->add('password')
->add('email')
->add('facebookid')
->add('roles');
//->add('password', 'password')
;
}
}
Found a solution for everyone who has the same problem as me, just on your admin class, where you define your Create/Update form, use this and your password will be perfectly encrypted and ready to log into your new user ;)
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle'))
->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle'))
->add('plainPassword', 'repeated', array(
'type' => 'password',
'options' => array('translation_domain' => 'FOSUserBundle'),
'first_options' => array('label' => 'form.password'),
'second_options' => array('label' => 'form.password_confirmation'),
'invalid_message' => 'fos_user.password.mismatch',
))
;
}

Yii CGrid View view all after search

I need some help, I've been figuring this for almost an hour and still can't find a way to fix this.
I have a search form to filter data from my CGridView, and I also need a button to view all the records after I finish using search, but I can't figure how to do it. Here's my code for jquery refresh button:
Yii::app()->clientScript->registerScript('initRefresh',<<<JS
$('#refresh-button').on('click',function() {
$('#app-asset-categories-grid').yiiGridView('update');
return false;
});
JS
,CClientScript::POS_READY);
Here's my code for jquery search form:
Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$('#app-asset-categories-grid').yiiGridView('update', {
data: $(this).serialize()
});
return false;
});
");
And finally, my grid:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'id'=>'app-asset-categories-grid',
'columns' => array(
'category_id',
'label',
'description',
'add_date',
'modification_date',
array(
'name'=>'Status',
'filter'=>array('1'=>'Active', '0'=>'Inactive'),
'value'=>'($data->status=="1")?"Active":"Inactive"'
),
array(
'name'=>'Deletion Status',
'filter'=>array('1'=>'Deactivate','0'=>'Active'),
'value'=>'($data->deletion_status=="1")?"Deactivated":"Activated"'
),
array(
'class'=>'CButtonColumn',
),
),
));
I just wanted a refresh button to display all the data again after I use my search form. I need your help guys, thanks.
You need to handle update action in model and set pagination parameter false. An example how to do it:
Add hidden field in search form _search.php
<?php echo CHtml::hiddenField('show_all', 0, array("id"=>"show_all")); ?>
Then handle it in model's search() function (your grid uses it to get data)
like this:
$criteria=new CDbCriteria;
// some criterias
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination' => (isset($_GET["show_all"]) && $_GET["show_all"])?false:array(), // add this line
));
Then set update hidden field when you need it.
$('#refresh-button').on('click',function() {
$('#show_all').val(1); // set it to show all
$('#app-asset-categories-grid').yiiGridView('update');
$('#show_all').val(0); // set to 0 for searching with pagination
return false;
});
Thats it.

Custom error message for validation rules in Kohana 3.2

I have a controller=product.php,a model=category.php, and a custom error message file category.php. When I am submitting the empty form , I am getting error messages for rule "not_empty, but I am not getting error message for rule "unique_categoryname"
Please help!
class Controller_Product extends Controller_Application
{
public function action_addcategory()
{
$errors='';
$cat = new Model_Category();
$validation=Validation::factory($this->request->post())
->rule('cat_name',array($cat,'unique_categoryname'));
if ($validation->check())
{
if (HTTP_Request::POST == $this->request->method())
{
try
{
$cat_name=$_POST['cat_name'];
$cat_description=$_POST['cat_description'];
if (isset($_FILES['cat_image']))
{
$filename = $cat->upload_photo($_FILES['cat_image']);
}
$cat->InsertCategory($cat_name,$cat_description,$filename);
Request::current()->redirect('product/listcategory');
}
catch (ORM_Validation_Exception $ex)
{
$errors = $ex->errors('models');
}
}
}
$view = new View('product/addcategory');
$view->set("categories",$cat);
$view->set('errors',$errors);
$this->template->content=$view;
}
}
class Model_Category extends ORM
{
public function rules()
{
return array(
'cat_name' => array(
array('not_empty'),
array('min_length', array(':value', 4)),
array('max_length', array(':value', 32)),
),
'cat_description' => array(
array('not_empty'),
array('min_length', array(':value', 10)),
),
);
}
}
//Custom error messages page : messages/models/category.php
return array(
'cat_name' => array(
'not_empty' => 'You must provide a category name.',
'min_length' => 'The category name must be at least :param2 characters long.',
'max_length' => 'The category name must be less than :param2 characters long.',
'unique_categoryname'=> 'Category Name already exists, please change',
),
'cat_description' => array(
'not_empty' => 'You must provide category description .',
'min_length' => 'The category description must be at least :param2 characters long.',
),
);
You need to pass your $validation to the ORM save/update method.
Assuming InsertCategory is a wrapper for saving, you should pass it there:
$cat->InsertCategory($cat_name, $cat_description, $filename, $validation);
Then in InsertCategory method:
public function InsertCategory(....)
{
// .....
$this->save($validation);
}
Be aware that there is a bug in Kohana 3.2 and external validation messages go to _external.php file.
You can find more information here: http://kohanaframework.org/3.2/guide/orm/validation#external-validation

Kohana 3.2: Custom error message for a custom validation rule?

I am using a custom method for a validation rule in my model (using Kohana 3.2). I am following the format listed on the documentation.
// Calls A_Class::a_method($value);
array(array('A_Class', 'a_method')),
But I can't seem to figure out how to add a custom error message if the rule fails.
Any help?
For this example we will assume a modal "user" and validating the field "username"
/application/classes/model/user.php
class Model_User extends ORM
{
public function rules()
{
return array(
'username' => array(
array('not_empty'),
array('A_Class::a_method', array(':value')),
)
);
}
}
A_Class
public static function a_method($value)
{
// Validate and return TRUE or FALSE
}
/application/messages/forms/user.php
Added a forms folder so show we can select message file to load with errors. Message file matches model name (user)
return array(
'username' => array(
'not_empty' => 'Custom error message for not_empty method',
'A_Class::a_method' => 'Custom error message for you own validation rule...'
),
);
Now in your controller to validate and display the error messages
class Controller_User extends Controller
{
// User model instance
$model = ORM::factory('user');
// Set some data to the model
$model->username - 'bob';
// Try to validate and save
try
{
$model->save()
}
catch (ORM_Validation_Exception $e)
{
// Loads messages from forms/user.php
$errors = $e->errors('forms');
// See the custom error messages
echo Debug::vars($errors);
)
)

Resources