zend-form-validation - zend-form

I have made form in zend and add validation to it as below
$firstname = $this->createElement('text','firstname');
$firstname->setLabel('First Name:')
->setRequired(true);
Validation is working and giving error message-> 'Value is required and can't be empty.'
But I want that It must say -> 'First Name is required and can't be empty.'
I don't know how to do it. Please help.

If you are only going to have one validation, you can do this:
$firstname = $this->createElement('text','firstname');
$firstname->setLabel('First Name:')
->setRequired(true);
->addErrorMessage('First Name is required and can't be empty.');

This should do exactly what you want:
$firstname = $this->createElement('text', 'firstname');
$firstname = $this->getElement('firstname')
->setRequired(true)
->addValidator('NotEmpty', true);
$firstname->getValidator('NotEmpty')
->setMessage('First Name is required and can't be empty.');

Related

How to break string data coming from Api response in Laravel

This my data coming from Api : "TAX_BREAKUP": "ab=4835,ay=1400,at=852,cb=4835,cy=1400,ct=852"
#foreach(explode(',', $data['TAX_BREAKUP']) as $amt)
Adult Priceā‚¹ {{$amt}}
Tax{{$amt}}
#endforeach
An this is code which I am trying to break the string.
If I understand your question, what you want is :
$Tax_arr = explode(',', $data['TAX_BREAKUP']);
foreach($Tax_arr as $amt){
$amt_arr = explode('=', $amt);
echo $amt_arr[0]." is ".$amt_arr[1]."<br>";
}
$amt_arr[0] is the attribute name and $amt_arr[1] is the value

Odoo XML-RPC: changing the active company?

My Odoo user has access to two companies but when I run code like the following (using ripcord as described here) it only shows me data for the default company:
$domain = [];
$domain[] = ['year', '=', '2019'];
$fields = ['account_id', 'date', 'balance2'];
$groupby = ['account_id', 'date'];
$result = $models->execute_kw($a, $b, $c, 'account.budget.report', 'read_group', [$domain, $fields, $groupby], []);
Adding ['company_id', '=', '31'] to the array $domain doesn't work.
How can I change the company I currently want to work with?
Figured it out myself. Looking at how the browser changes the company I need to do this write operation first:
$result = $models->execute_kw($a, $b, $c, 'res.users', 'write', [[75], ['company_id' => 31]]);
75 is my user ID.

Prestashop - Which SQL called after actionProductUpdate?

I try to update a name from ps_product when I update my product's stock. I added a SQL request on the hook actionProductUpdate.
The hook works, because when I set a die; after the SQL, the database is updated.
But I don't know where, another SQL is called and set back to the old value. I use PROFILING and I saw that it's called after....
How can I set this call as the last thing to do after a product update save ?
This is the simplified code :
public function hookActionProductUpdate($params)
{
$product = $params['product'];
$sql = 'UPDATE ' . _DB_PREFIX_ . 'product mp
SET mp.name = ''
WHERE mp.id_product = ' . $product->id;
Db::getInstance()->execute($sql);
}
PS : If I change a another parameter like reference, this time it works !
It is better to use object product to achieve your goal. Something like
public function hookActionObjectProductUpdateAfter($params)
{
$product = new Product($params['object']->id);
foreach(Language::getLanguages(true) as $language) {
$product->name[$language['id_lang']] = 'new name';
}
$product->update();
}
in this case, you don't care where the name is persisted in the DB, and you will never damage it
PS : Take care of params if you want to get 'product'. In actionProductUpdate, it's params['product'] but in hookActionObjectProductUpdateAfter it's params['object'] (as Product). I lost so much time on this...
"name" field is in the "ps_product_lang" table.
You have to rename product for each languages.

get user role by user id in moodle

I want to get user role from user id. I am using loop in my code where i want to show all user except admin. i used below code but its not working.
$context = get_context_instance (CONTEXT_SYSTEM);
$roles = get_user_roles($context, $USER->id, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;
Its provide me blank array as like screenshot. Also below my all code.
https://prnt.sc/gq8p12
$allUsers = $DB->get_records('user');
$SQL = "SELECT * FROM `".$CFG->prefix."config` WHERE `name` LIKE 'siteadmins'";
$getSiteAdmins = $DB->get_record_sql($SQL);
$explodeAdminIds = explode(',', $getSiteAdmins->value);
$context = get_context_instance (CONTEXT_SYSTEM);
if(!empty($allUsers))
{
foreach ($allUsers as $allUser)
{
if(!in_array($allUser->id, $explodeAdminIds))
{
$roles = get_user_roles($context, $allUser->id, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;
echo 'USER ID -- '.$allUser->id.' >>> ';
print_r($roles); echo '<br>';
$name = ''.$allUser->id.'_'.$allUser->firstname.' '.$allUser->lastname.'';
$confirmed = ($allUser->confirmed == 1) ? 'Active' : 'In-active';
$table->data[] = array(
$i,
$name,
'Team Name',
$allUser->email,
$allUser->phone1,
'Role',
$confirmed,
//empty($coachusrarr)?'--':implode(',',$coachusrarr),
//empty($tmpleaderarr)?'--':implode(',',$tmpleaderarr),
//$coach,
);
$i++;
}
}
}
The basic problem is that get_user_roles($context, $userid) will only get you a list of roles assigned at that particular context level. Very few users have roles assigned at the system context, it is much more usual for roles to be assigned at a course level. This allows users to have different roles in different courses (a teacher on one course, might be enrolled as a student on another course).
If you want to get all the roles for a user, then you're going to need to do something like this:
$roleassignments = $DB->get_records('role_assignments', ['userid' => $user->id]);
You can then loop through all the $roleassignments and extract the 'roleid' from them (alternatively, you could use the $DB->get_fieldset command, to extract the roleids directly).
Note also that you should be using context_system::instance() instead of the old get_context_instance(CONTEXT_SYSTEM) (unless you are using a very old and insecure version of Moodle).
For getting the site admins, use get_admins() (or, if you really want to access the config value, use $CFG->siteadmins).
If you want to get a user role for a course by user id then this script will help you.
$context = context_course::instance($COURSE->id);
$roles = get_user_roles($context, $USER->id, true);
$role = key($roles);
$rolename = $roles[$role]->shortname;

SQL Compare Characters in two strings count total identical

So the over all on this is I have two different systems and in both systems I have customers, unfortunately both systems allow you to type in the business name freehand so you end up with the example below.
Column A has a value of "St John Baptist Church"
Column B has a value of "John Baptist St Church"
What I need to come up with is a query that can compare the two columns to find the most closely matched values.
From there I plan to write a web app where I can have someone go through and validate all of the entries. I would enter in some example of what I have done, but unfortunately I honestly dont even know if what I am asking for is even possible. I would think it is though in this day and age I am sure I am not the first one to try to attempt this.
You could try and create a script something like this php script to help you:
$words = array();
$duplicates = array();
function _compare($value, $key, $array) {
global $duplicates;
$diff = array_diff($array, $value);
if (!empty($diff)) {
$duplicates[$key] = array_keys($diff);
}
return $diff;
}
$mysqli = new mysqli('localhost', 'username', 'password', 'database');
$query = "SELECT id, business_name FROM table";
if ($result = $mysqli->query($query)) {
while ($row = $result->fetch_object()) {
$pattern = '#[^\w\s]+#i';
$row->business_name = preg_replace($pattern, '', $row->business_name);
$_words = explode(' ', $row->business_name);
$diff = array_walk($words, '_compare', $_words);
$words[$row->id][] = $_words;
$result->close();
}
}
$mysqli->close();
This is not tested but you need something like this, because I don't think this is possible with SQL alone.
---------- EDIT ----------
Or you could do a research on what the guys in the comment recommend Levenshtein distance in T-SQL
Hope it helps, good luck!