I want to create a form field for a login script that allows either a username or an email address to be entered.
I have a validator
$loginName = new Zend_Form_Element_Text('loginName');
$loginName->addValidators(array('Alnum', 'EmailAddress');
Which does not work (because it seems to validate Alnum AND EmailAddress). I would like to validate Alnum OR EmailAddress.
Any suggestions on how to do this?
You have two options. You can either use a Regex validator, or you can write a custom validator which itself uses the Alnum and Email validators.
http://framework.zend.com/manual/en/zend.validate.writing_validators.html
The second option would look something like this:
class My_Validate_Field extends Zend_Validate_Abstract
{
protected $_messageTemplates = array(
'alnumemail' => "'%value%' is not an alphanumeric string or email address"
);
public function isValid($value)
{
$alnum = new \Zend_Validate_Alnum();
$email = new \Zend_Validate_EmailAddress();
$valid = $alnum->isValid( $value ) || $email->isValid( $value);
if( !$valid ) {
$this->_error();
}
return $valid;
}
}
Related
I added custom html form in magento2 page. In magento2 default contact form will be there, it will work normally.But my custom email not send. How to send the custom mail with user and admin?
1. Create new html file
/app/code/VendorName/ModuleName/view/frontend/email/modulename/test.html
<div>
Hello, Test email.
</div>
2. Declare your email template, create xml file
/app/code/VendorName/ModuleName/etc/email_templates.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:Magento:module:Magento_Email:etc/email_templates.xsd">
<template id="modulename_test_template" label="Test email" file="modulename/test.html" type="html" module="VendorName_ModuleName" area="frontend"/>
</config>
3. Finally, create a function to send your email.
namespace VendorName\ModuleName\Controller;
use Magento\Framework\App\RequestInterface;
class Sendemail extends \Magento\Framework\App\Action\Action
{
/**
* #var \Magento\Framework\App\Request\Http
*/
protected $_request;
/**
* #var \Magento\Framework\Mail\Template\TransportBuilder
*/
protected $_transportBuilder;
/**
* #var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
public function __construct(
\Magento\Framework\App\Action\Context $context
, \Magento\Framework\App\Request\Http $request
, \Magento\Framework\Mail\Template\TransportBuilder $transportBuilder
, \Magento\Store\Model\StoreManagerInterface $storeManager
)
{
$this->_request = $request;
$this->_transportBuilder = $transportBuilder;
$this->_storeManager = $storeManager;
parent::__construct($context);
}
public function execute()
{
$store = $this->_storeManager->getStore()->getId();
$transport = $this->_transportBuilder->setTemplateIdentifier('modulename_test_template')
->setTemplateOptions(['area' => 'frontend', 'store' => $store])
->setTemplateVars(
[
'store' => $this->_storeManager->getStore(),
]
)
->setFrom('general')
// you can config general email address in Store -> Configuration -> General -> Store Email Addresses
->addTo('customer#email.com', 'Customer Name')
->getTransport();
$transport->sendMessage();
return $this;
}
}
check the flow,you create new contact form,then you can get the form data from HTTPREquestInterface,then reference the above answer for sending email,you can then send the email with custom form
public function execute()
{
$store = $this->_storeManager->getStore()->getId();
$transport = $this->_transportBuilder->setTemplateIdentifier('modulename_test_template')
->setTemplateOptions(['area' => 'frontend', 'store' => $store])
->setTemplateVars(
[
'store' => $this->_storeManager->getStore(),
]
)
->setFrom('general')
// you can config general email address in Store -> Configuration -> General -> Store Email Addresses
->addTo('customer#email.com', 'Customer Name')
->getTransport();
$transport->sendMessage();
return $this;
}
//customer#email.com---can get it from httpRequest
you can use the following method given here to send custom Emails programmatically in Magento 2.
<?php
namespace [Vendor]\[Module]\Helper;
use Magento\Framework\App\Helper\Context;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Store\Model\StoreManagerInterface;
class Data extends AbstractHelper
{
protected $transportBuilder;
protected $storeManager;
protected $inlineTranslation;
public function __construct(
Context $context,
TransportBuilder $transportBuilder,
StoreManagerInterface $storeManager,
StateInterface $state
)
{
$this->transportBuilder = $transportBuilder;
$this->storeManager = $storeManager;
$this->inlineTranslation = $state;
parent::__construct($context);
}
public function sendEmail()
{
// this is an example and you can change template id,fromEmail,toEmail,etc as per your need.
$templateId = 'my_custom_email_template'; // template id
$fromEmail = 'owner#domain.com'; // sender Email id
$fromName = 'Admin'; // sender Name
$toEmail = 'customer#email.com'; // receiver email id
try {
// template variables pass here
$templateVars = [
'msg' => 'test',
'msg1' => 'test1'
];
$storeId = $this->storeManager->getStore()->getId();
$from = ['email' => $fromEmail, 'name' => $fromName];
$this->inlineTranslation->suspend();
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
$templateOptions = [
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' => $storeId
];
$transport = $this->transportBuilder->setTemplateIdentifier($templateId, $storeScope)
->setTemplateOptions($templateOptions)
->setTemplateVars($templateVars)
->setFrom($from)
->addTo($toEmail)
->getTransport();
$transport->sendMessage();
$this->inlineTranslation->resume();
} catch (\Exception $e) {
$this->_logger->info($e->getMessage());
}
}
}
Please First install SMTP marketplace module and set mail after applying code
link :https://github.com/mageplaza/magento-2-smtp
I have a form :
$houserent = new Zend_Form_Element_Text('houserent');
$houserent ->setLabel('House Rent :');
$this ->addElement($houserent);
$tax = new Zend_Form_Element_Text('tax');
$siteName ->setLabel('Tax :');
$this ->addElement($tax);
$internet = new Zend_Form_Element_Text('internet');
$internet->setLabel('Internet :');
$this ->addElement($internet);
and my data table "test" is
id name value
1 house rent 100
2 tax 10
3 internet 10
I want to populate the form using the above data from tha database table. But I don't know how to this. Please help me. Thanks
You can overwrite the populate function in your form.
public function populate($data)
{
foreach($data as $field => $value)
{
$this->{$field}->setValue($value);
}
return $this;
}
where $data is an associated array of name => value.
[edit]
So you form is now:
<?php
class Form_Admin_Settings_Add extends Zend_Form
{
public function init()
{
$houserent = new Zend_Form_Element_Text('houserent');
$houserent->setLabel('House Rent :');
$this->addElement($houserent);
$tax = new Zend_Form_Element_Text('tax');
$siteName->setLabel('Tax :');
$this->addElement($tax);
$internet = new Zend_Form_Element_Text('internet');
$internet->setLabel('Internet :');
$this->addElement($internet);
}
public function populate($data)
{
foreach($data as $field => $value)
{
$this->{$field}->setValue($value);
}
return $this;
}
}
In your controller:
<?php
//instantiate form and model
$form = new Form_Admin_Settings_Add();
$model = new Model_Test();
//get results
$results = $model->fetchAll()->toArray();
$data = array();
//put results into our data array as name => value
foreach($results as $r)
{
$data[$r['name']] = $r['value'];
}
//populate our form
$form->populate(data);
echo $form;
Try some like this:
class Form extends Zend_Form {
public funcition Form(){
$houserent = new Zend_Form_Element_Text('houserent');
$houserent->setLabel('House Rent :')
->setValue($this->_Data['houserent']);
$this->addElement($houserent);
}
public function setData($Data){
$this->_Data = $Data;
return $this;
}
}
I'm becoming crazy with Kendo UI AutoComplete component. I'm using my own functions to access data with jQuery, so I have to set the AutoComplete dataSource.transport.read as a function. The code is something like this.
minLengthAtocomplete = 3;
$('#autocomplete').kendoAutoComplete({
minLength : 3,
filter : "contains",
dataValueField : "key",
dataTextField : "value",
dataSource : new kendo.data.DataSource({
transport : {
read : _OnTransportRead
},
schema : {
/* object schema */
}
})
});
function _OnTransportRead(e) {
var text = $.trim(e.data.filter.filters[0].value);
if (text && text.length >= minLengthAtocomplete) {
_GetUsers(
text,
function onSuccess(data) {
var users = [];
/* sets users with info in data */
e.success(users);
},
function onError(error) {
/* stuff with error */
}
);
}
}
function _GetUsers(userName, onSuccess, onError) {
/* Ajax to get users from DB */
}
This code runs perfectly, but dataSource.transport.read is called only the once. I do a first search with the text 'michae' and AutoComplete component runs its dataSource.transport.read as expected. Then, I add a one more letter to search for 'michael', and dataSource.transport.read is never called again. Is so frustrating!
I tried using autoSync dataSource property, manual dataSource Sync, set new dataSource objects on AutoComplete dataBound, but no luck.
What am I doing wrong? What am I forgetting?
Thanks in advance.
You should enable serverFiltering in order for the data source to make requests every time.
$('#autocomplete').kendoAutoComplete({
minLength : 3,
filter : "contains",
dataValueField : "key",
dataTextField : "value",
dataSource : new kendo.data.DataSource({,
serverFiltering: true,
transport : {
read : _OnTransportRead
},
schema : {
/* object schema */
}
})
});
I want to restrict my users to edit/delete only the comments which they added. I found an example on youtube by a guy named intergral30 and followed his instruction. And now my admin account has the possibility to edit/delete everything, but my user has no access to his own comment.
Here's the code:
Resource
class Application_Model_CommentResource implements Zend_Acl_Resource_Interface{
public $ownerId = null;
public $resourceId = 'comment';
public function getResourceId() {
return $this->resourceId;
}
}
Role
class Application_Model_UserRole implements Zend_Acl_Role_Interface{
public $role = 'guest';
public $id = null;
public function __construct(){
$auth = Zend_Auth::getInstance();
$identity = $auth->getStorage()->read();
$this->id = $identity->id;
$this->role = $identity->role;
}
public function getRoleId(){
return $this->role;
}
}
Assertion
class Application_Model_CommentAssertion implements Zend_Acl_Assert_Interface
{
public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $user=null,
Zend_Acl_Resource_Interface $comment=null, $privilege=null){
// if role is admin, he can always edit a comment
if ($user->getRoleId() == 'admin') {
return true;
}
if ($user->id != null && $comment->ownerId == $user->id){
return true;
} else {
return false;
}
}
}
In my ACL I have a function named setDynemicPermissions, which is called in an access check plugin's preDispatch method.
public function setDynamicPermissions() {
$this->addResource('comment');
$this->addResource('post');
$this->allow('user', 'comment', 'modify', new Application_Model_CommentAssertion());
$this->allow('admin', 'post', 'modify', new Application_Model_PostAssertion());
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$this->_acl->setDynamicPermissions();
}
And I'm calling the ACL-s isAllowed method from my comment model where I return a list of comment objects.
public function getComments($id){
//loading comments from the DB
$userRole = new Application_Model_UserRole();
$commentResource = new Application_Model_CommentResource();
$comments = array();
foreach ($res as $comment) {
$commentResource->ownerId = $comment[userId];
$commentObj = new Application_Model_Comment();
$commentObj->setId($comment[id]);
//setting the data
$commentObj->setLink('');
if (Zend_Registry::get('acl')->isAllowed($userRole->getRoleId(), $commentResource->getResourceId(), 'modify')) {
$commentObj->setLink('Edit'.'Delete');
}
$comments[$comment[id]] = $commentObj;
}
}
Can anyone tell me what have I done wrong?
Or what should I use if I want to give my admins the right to start a post and other users the right to comment on them. Each user should have the chance to edit or delete his own comment and an admin should have all rights.
You seem to be using the dynamic assertions in a wrong manner, as you are still passing the roleId to isAllowed().
What these dynamic assertions really do, is take a complete object and work with it. Zend will determine which rule has to be used by calling getResourceId() and getRoleId() on your objects.
So all you have to do is pass your objects instead of the strings to isAllowed():
public function getComments($id){
//loading comments from the DB
$userRole = new Application_Model_UserRole();
$commentResource = new Application_Model_CommentResource();
$comments = array();
foreach ($res as $comment) {
$commentResource->ownerId = $comment[userId];
$commentObj = new Application_Model_Comment();
$commentObj->setId($comment[id]);
//setting the data
$commentObj->setLink('');
// This line includes the changes
if (Zend_Registry::get('acl')->isAllowed($userRole, $commentResource, 'modify')) {
$commentObj->setLink('Edit'.'Delete');
}
$comments[$comment[id]] = $commentObj;
}
}
But in can be done better
You would not have to implement a total new Application_Model_CommentResource, but instead you can use your actual Application_Model_Comment like this:
// we are using your normal Comment class here
class Application_Model_Comment implements Zend_Acl_Resource_Interface {
public $resourceId = 'comment';
public function getResourceId() {
return $this->resourceId;
}
// all other methods you have implemented
// I think there is something like this among them
public function getOwnerId() {
return $this->ownerId;
}
}
Assertion would then use this object and retrieve the owner to compare it with the actually logged in person:
class Application_Model_CommentAssertion implements Zend_Acl_Assert_Interface {
public function assert(Zend_Acl $acl, Zend_Acl_Role_Interface $user=null,
Zend_Acl_Resource_Interface $comment=null, $privilege=null){
// if role is admin, he can always edit a comment
if ($user->getRoleId() == 'admin') {
return true;
}
// using the method now instead of ->ownerId, but this totally depends
// on how one can get the owner in Application_Model_Comment
if ($user->id != null && $comment->getOwnerId() == $user->id){
return true;
} else {
return false;
}
}
And the usage is like this:
public function getComments($id) {
//loading comments from the DB
$userRole = new Application_Model_UserRole();
$comments = array();
foreach ($res as $comment) {
$commentObj = new Application_Model_Comment();
$commentObj->setId($comment[id]);
//setting the data
$commentObj->setLink('');
// no $commentResource anymore, just pure $comment
if (Zend_Registry::get('acl')->isAllowed($userRole, $comment, 'modify')) {
$commentObj->setLink('Edit'.'Delete');
}
$comments[$comment[id]] = $commentObj;
}
}
Is there a proper way to access other form fields from inside a validator?
Is there another solution than:
context.getViewRoot().findComponent("formid:exampleField:example")?
f.e I want to validate a city field inside a custom validator and checking if country is US.
Re-read your question and I am going to interpret it as this:
"You would like to write a custom validator that checks that if a city field exists, the country field is equal to 'US'"
So, I would look at going about this in the following fashion:
First create a validator interface:
#Documented
#ValidatorClass(value=CountryEqualsUSValidator.class)
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface CountryEqualsUS {
String message() default "The country should be US for the city provided";
}
Then create a validator class:
public class CountryEqualsUSValidator implements Validator<CountryEqualsUS> {
public void initialize(CountryEqualsUS arg0) {
}
public boolean isValid(Object value) {
if(value != null && value instanceof YourBeanClass) {
YourBeanClass yourBeanClass = (YourBeanClass) value;
if(/*some test logic here*/) {
return true;
else {
return false;
}
}
return false;
}
}
Then on the class that you want to validate:
#CountryEqualsUS
public class YourBeanClass {
...
}
Then, finally, on your controller/action class, when the form is submitted, the city is a value for which you want to check the country, add this method and call it:
public boolean doValidation(YourBeanClass yourBeanClass) {
ClassValidator requestValidator = new ClassValidator(yourBeanClass.getClass());
InvalidValue[] validationMessages = requestValidator.getInvalidValues(yourBeanClass);
if (validationMessages != null && validationMessages.length > 0) {
for (int i = 0; i < validationMessages.length; i++) {
//Add a validation message to be displayed to the user
}
return false;
}
return true;
}