Adding custom validation in zend framework - zend-framework

I have a mysql table named users having fields: username, password, email
I have controller/action like this 'user/update' for adding new user and 'user/update/id/{user_id}'
I have same Zend_Form for both controller. For adding new user, I've checked whether the username already exist or not using below code:
$username->addValidator('Db_NoRecordExists', true, array('table' => 'user', 'field' => 'username'));
This works well when new user is added but when editing the user, even when the user leaves the username field as it is, i get username already exist error.
Is their a validation that zend provide similar to Db_NoRecordExists for editing the field.
In editing case,i want query like:
SELECT COUNT(*) FROM users WHERE username='$username' AND id!=$update_id
How can i do this?

You can convert your sql query into Zend_Db_Select
$select = Zend_Db_Table::getDefaultAdapter()->select()->from('users')->where('username =?',$username)
->where('id != ?',$update_id);
$validator = new Zend_Validate_Db_NoRecordExists($select);

Zend_Validate_Db_NoRecordExists has an exclude option for this case:
$validator = new Zend_Validate_Db_NoRecordExists(
array(
'table' => 'users',
'field' => 'username',
'exclude' => array(
'field' => 'id',
'value' => $user_id
)
)
);

Related

Adding new fields to Admin User Info in magento

By default when you try to add a new User in magento, it accepts only the following fields:
Username, FirstName, LastName, Email, New Password and Password Confirmation.
I want to add some more field here, e.g. About Me, PhoneNumber, etc where we will add additional information about this role specific users we add to magento.
Has anyone already done this? How should I go about doing this?
I know additional fields will need to be created in the database. The adminhtml userinfo.phtml doesn't mention fields like Username, FirstName etc, so where it is picking it up from?
Please advice.
Thanks,
Neet
You can start with following tutorial to create registration field
http://www.magentocommerce.com/wiki/5_-_modules_and_development/customers_and_accounts/registration_fields
a) Adding new fields to Admin User Info in magento
b) Adding new fields to Admin User Info in Magento
You will need to add a column to the admin_user table. Make a table with the following code:
<?php
$installer->getConnection()->addColumn($installer->getTable('admin/user'),
'location', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 256,
'nullable' => true,
'default' => null
));
?>
Then, if you want to add/edit this field from the backend you need to
rewrite the method Mage_Adminhtml_Block_System_Account_Edit_Form::_prepareForm and add a new
element in there:
<?php
$fieldset->addField('location', 'select', array(
'name' => 'location',
'label' => Mage::helper('adminhtml')->__('Location'),
'id' => 'location',
'title' => Mage::helper('adminhtml')->__('Location'),
'class' => 'input-select',
'style' => 'width: 80px',
'options' => array('1' =>
Mage::helper('adminhtml')->__('Russia'), '0' =>
Mage::helper('adminhtml')->__('India')),
));
?>
Clear the cache and run the code. You can add new fields to Admin User Info in Magento.

Symfony2: Create form binded to an entity with a field which is not a property

The following (simple) question from me:
$user = new User();
$user->setEventid(2);
$user->setT(new \DateTime('now'));
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$form = $this->createFormBuilder($user)
->add('email', 'email')
->add('terms', 'checkbox', array(
'label' => 'Read terms?',
'required' => true))
->getForm();
This is my code. Email is property of the user, terms not.
I want them users to save their email and check the terms-checkbox but i just need to save the email, not if they check the box - if they don't they cant submit the form.
You know what i mean? I'm shure its pretty simple to achieve this ;)
Second Question: How can i give the rendered from-tag an id-attribute for further handeling in jquery?
To add a field that doesn't exist in the entity to a form, use this:
->add('terms', 'checkbox', array(
'label' => 'Read terms?',
'required' => true,
'mapped' => false, // this works since Symfony 2.1
'property_path' => false, // this works since Symfony 2.0
))
Symfony generates an ID for each form element; just open the rendered page source and see the ID of the element you need.
If you don't have a 'terms' field in your Entity, it won't be save.
For your second question, it's very simple to set an id to a form in twig, make simply :
{{ form_label(form.name, 'Your Name', { 'attr': {'id': 'foo'} }) }}

Duplicate Validation on Combined Fields in zend form

Hi there I have a table in which combination of three fields is unique. I want to put the check of duplication on this combination. Table looks like
I know how to validate single field, But how to validate the combination is not know. To validate one field I use the following function
public function isValid($data) {
// Options for name field validation
$options = array(
'adapter' => Zend_Db_Table::getDefaultAdapter(),
'table' => 'currencies',
'field' => 'name',
'message'=> ('this currency name already exists in our DB'),
);
// Exclude if a id is given (edit action)
if (isset($data['id'])) {
$options['exclude'] = array('field' => 'id', 'value' => $data['id']);
}
// Validate that name is not already in use
$this->getElement('name')
->addValidator('Db_NoRecordExists', false, $options
);
return parent::isValid($data);
}
Will any body guide me how can I validate duplication on combined fields?
There is no ready to use validator for this, as far as I know. You have either to write your own, or do a check with SQL-query with three conditions (one for each field).
you have to Apply a validation on name element of zend form.
Here is code for add validation on name field.
$name->addValidator(
'Db_NoRecordExists',
true,
array(
'table' => 'currencies',
'field' => 'name',
'messages' => array( "recordFound" => "This Currency Name already exists in our DB") ,
)
);
And you must set required true.

Zend_Validate_Db_RecordExists against 2 fields

I usualy use Zend_Validate_Db_RecordExists to update or insert a record. This works fine with one field to check against. How to do it if you have two fields to check?
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector,day_of_week'
)
);
if ($validator->isValid($fields_values['id_sector'],$fields_values['day_of_week'])){
//true
}
I tried it with an array and comma separated list, nothing works... Any help is welcome.
Regards
Andrea
To do this you would have to extend the Zend_Validate_Db_RecordExists class.
It doesn't currently know how to check for the existence of more than one field.
You could just use two different validator instances to check the two fields separately. This is the only work around that I can see right now besides extending it.
If you choose to extend it then you'll have to find some way of passing in all the fields to the constructor ( array seems like a good choice ), and then you'll have to dig into the method that creates the sql query. In this method you'll have to loop over the array of fields that were passed in to the constructor.
You should look into using the exclude parameter. Something like this should do what you want:
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector',
'exclude' => array(
'field' => 'day_of_week',
'value' => $fields_values['day_of_week']
)
);
The exclude field will effectively add to the automatically generated WHERE part to create something equivalent to this:
WHERE `id_sector` = $fields_values['id_sector'] AND `day_of_week` = $fields_values['day_of_week']
Its kind of a hack in that we're using it for the opposite of what it was intended, but its working for me similar to this (I'm using it with Db_NoRecordExists).
Source: Zend_Validate_Db_NoRecordExists example
Sorry for the late reply.
The best option that worked for me is this:
// create an instance of the Zend_Validate_Db_RecordExists class
// pass in the database table name and the first field (as usual)...
$validator = new Zend_Validate_Db_RecordExists(array(
'table' => 'tablename',
'field' => 'first_field'
));
// reset the where clause used by Zend_Validate_Db_RecordExists
$validator->getSelect()->reset('where');
// set again the first field and the second field.
// :value is a named parameter that will be substituted
// by the value passed to the isValid method
$validator->getSelect()->where('first_field = ?', $first_field);
$validator->getSelect()->where('second_field = :value', $second_field);
// add your new record exist based on 2 fields validator to your element.
$element = new Zend_Form_Element_Text('element');
$element->addValidator($validator);
// add the validated element to the form.
$form->addElement($element);
I hope that will help someone :)
Although, I would strongly recommend a neater solution which would be to extend the Zend_Validate_Db_RecordExists class with the above code.
Enjoy!!
Rosario
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
'validators' => array('EmailAddress', $obj= new Zend_Validate_Db_NoRecordExists(array('adapter'=>$dbAdapter,
'field'=>'email',
'table'=>'user',
'exclude'=>array('field'=>'email','value'=>$this->_options['email'], 'field'=>'is_deleted', 'value'=>'1')
))),
For those using Zend 2, If you want to check if user with given id and email exists in table users, It is possible this way.
First, you create the select object that will be use as parameter for the Zend\Validator\Db\RecordExists object
$select = new Zend\Db\Sql\Select();
$select->from('users')
->where->equalTo('id', $user_id)
->where->equalTo('email', $email);
Now, create RecordExists object and check the existence this way
$validator = new Zend\Validator\Db\RecordExists($select);
$validator->setAdapter($dbAdapter);
if ($validator->isValid($username)) {
echo 'This user is valid';
} else {
//get and display errors
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
This sample is from ZF2 official doc
You can use the 'exclude' in this parameter pass the second clause that you want to filter through.
$clause = 'table.field2 = value';
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => 'table',
'field' => 'field1',
'exclude' => $clause
)
);
if ($validator->isValid('value') {
true;
}
I am using zend framework v.3 and validation via InputFilter(), it uses same validation rules as zend framework 2.
In my case I need to check, if location exists in db (by 'id' field) and has needed company's id ('company_id' field).
I implemented it in next way:
$clause = new Operator('company_id', Operator::OP_EQ, $companyId);
$inputFilter->add([
'name' => 'location_id',
'required' => false,
'filters' => [
['name' => 'StringTrim'],
['name' => 'ToInt'],
],
'validators' => [
[
'name' => 'Int',
],
[
'name' => 'dbRecordExists',
'options' => [
'adapter' => $dbAdapterCore,
'table' => 'locations',
'field' => 'id',
'exclude' => $clause,
'messages' => [
'noRecordFound' => "Location does not exist.",
],
]
],
],
]);
In this case validation will pass, only if 'locations' table has item with columns id == $value and company_id == $companyId, like next:
select * from location where id = ? AND company_id = ?

zend mapping one to many fetchall

I have 2 tables user and user_comment where user has many user_comments, i laid down the mapping being
User
$_dependentTables = array('User_Comments);
and
User_Comments
$_referenceMap = array(
'User' => array(
'columns' => 'id',
'refTableClass' => 'User',
'refColumns' => 'id'
)
);
Is there a way for me to do user->fetchAll() and get the user_comments without doing loop query (in cakephp it will do one query on user_comments where in (ids) then format it back to an array but i cant use cake). Is this possible in zend with me doing it manually? Thanks
Try this one
$sql=$this->getAdapter()->select()
->from("user_comment")
->join("user", "user.id=user_comment.userid")
->where("user_comment.id=?",$userId);
$result=$this->getAdapter()->query($sql)->fetchAll();
This might help u....