How can i override set_message multiple time in foreach in codeginiter? - cql

foreach($all_fields as $row){
$datatype = $this->sys_model->get_table_data_by_datatype_id('datatype',$row['datatype_id']);
$msg = $this->sys_model->get_validation_message_by_datatype_id('validation_message',$row['datatype_id']);
if($datatype[0]['datatype'] == 'email'){
$this->form_validation->set_rules($row['field_name'],$row['field_name'], 'required|trim|xss_clean|valid_email');
$this->form_validation->set_message('required',$msg[0]['validation_msg']);
}
elseif($datatype[0]['datatype'] == 'number'){
$this->form_validation->set_rules($row['field_name'],$row['field_name'], 'required|numeric');
$this->form_validation->set_message('required',$msg[0]['validation_msg']);
}else{
$this->form_validation->set_rules($row['field_name'],$row['field_name'], 'required');
$this->form_validation->set_message('required',$msg[0]['validation_msg']);
}
}
i want to set validation message dynamically from backend based on datatype but it takes same message for all fields..
How can i set different message for all fields based on datatype ?

create a callback functions for each datatype
function validate_string($str)
{
if(condition) )
{
return true;
}
else
{
$this->form_validation->set_message('validate_string', 'The %s your custom error message');
return false;
}
}
$this->form_validation->set_rules('field1', 'label1', 'validate_string');
$this->form_validation->set_rules('field2', 'label2', 'validate_string');
$this->form_validation->set_rules('field3', 'label3', 'validate_string');
$this->form_validation->set_rules('field4', 'label4', 'validate_string');

Related

How to validate mongoose document by current field in existed doc in mongodb?

I have mongoose schema
const messageSchema = new mongoose.Schema({{
name: String,
code: String //enum ['start', 'waiting', 'complete']
})
for example - I will save an item:
{
name: 'firstItem',
code: 'start'
}
next time - when update this document I want to use validation function
function validateCode(value) {
if (existed.code === 'start' && value === 'waiting') {
return true;
}
if (existed.code === 'waiting' && value === 'complete') {
return true;
}
return false;
}
but How can I call existed item id db in validate function?
Big thx!
I'm fairly certain that validators don't get access to the object that is being updated, so what you are trying to do won't work with validators.
There are however different options for you. For instance you could use a instance method.
For the validation flow you described before, a instance method accomplishing the same would be this:
messageSchema.methods.updateCode = function(value){
const fromStart = this.code === 'start' && value === 'waiting';
const fromWaiting = this.code === 'waiting' && value === 'complete';
if (fromStart || fromWaiting) {
this.code = value;
return this.save();
}
else {
throw Error("Invalid code update");
}
}
With this method you would also be free to choose a more optimized way to handle the "invalid code update" case.
More on instance methods could be found in the mongoose docs

How to write object classificationstore in a product?

There is a class product, it has a layer ProductSpecs (classificationstore). How to write a classificationStore group I need into an object?
The documentation found nothing on this topic. Please, help me
namespace AppBundle\EventListener;
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent;
class ProductPropertyListener
{
public function onPreUpdate (ElementEventInterface $e) {
if($e instanceof AssetEvent) {
// do something with the asset
$foo = $e->getAsset();
} else if ($e instanceof DocumentEvent) {
// do something with the document
$foo = $e->getDocument();
} else if ($e instanceof DataObjectEvent) {
// do something with the object
$foo = $e->getObject();
// getting an object here is not clear what to do with it
}
}
}
Assuming your CS field is called ProductSpecs, you could try this to set active group for object:
$groupConfig = GroupConfig::getByName($groupName, $storeId);
if ($groupConfig) {
$groupId = $groupConfig->getId();
$foo->getProductSpecs()->setActiveGroups([$groupId => true]);
}

PXProcessing SetError not showing in UI Grid

This my process screen:
as you can see it throws errors but it doesnt indicate the error mark on the grid.
After clicking the process button, it just unchecks the checkbox in my records
i want the grid to be like this(with the red 'x' mark):
this is my graph :
public PXCancel<PayrollFilter> Cancel;
public PXSetup<PayrollSetup> PayrollSetup;
public PXFilter<PayrollFilter> Filter;
[PXFilterable]
public PXFilteredProcessingJoin<PayrollEmployeeProcess, PayrollFilter,
InnerJoin<EPEmployee,
On<PayrollEmployee.employeeID, Equal<EPEmployee.bAccountID>>,
InnerJoin<Branch,
On<EPEmployee.parentBAccountID, Equal<Branch.bAccountID>>>>,
Where<PayrollEmployee.payPeriodID, Equal<Current<PayrollFilter.payPeriodID>>,
And<Branch.branchID, Equal<Current<AccessInfo.branchID>>>>> EmployeePayrollProcess;
#region Constructor
public PayrollProcess()
{
PayrollSetup setup = PayrollSetup.Current;
EmployeePayrollProcess.SetSelected<PayrollEmployeeProcess.selected>();
EmployeePayrollProcess.SetProcessDelegate(delegate (List<PayrollEmployeeProcess> employees)
{
if (Filter.Current == null) return;
var payPeriod = Filter.Current.PayPeriodID ?? 0;
var payrollPeriod = Filter.Current.PayrollPeriodID ?? 0;
if (payPeriod == 0 || payrollPeriod == 0) return;
PXLongOperation.StartOperation(this, delegate ()
{
bool errorOccured = false;
foreach (PayrollEmployeeProcess employee in employees)
{
PayrollRegisterEntry graph = PXGraph.CreateInstance<PayrollRegisterEntry>();
try
{
graph.ProcessPayroll(employee, payPeriod, payrollPeriod);
PXProcessing<PayrollEmployeeProcess>.SetInfo("Employee processed");
}
catch (Exception ex)
{
errorOccured = true;
//employees.IndexOf(employee),
PXProcessing<PayrollEmployeeProcess>.SetError(ex);
}
finally
{
graph.Clear();
}
}
if (errorOccured) throw new PXException("At least one employee was not processed.");
});
});
// EmployeePayrollProcess.
}`
can anyone can help me? I'm using Acumatica 6
Throwing an exception in Acumatica sets the error in the header. To set a Row or Field level error you need to set/raise it. There's a few ways to set/raise errors, what they have in common is that they don't use the 'throw' keyword.
For a processing screen with a filter, use the following syntax to raise the error:
PXFilteredProcessing<GridDetailDAC, GridFilterDAC>.SetError(rowIndex, new PXSetPropertyException("Error Message", PXErrorLevel.RowError));
Processing screen without filter:
PXProcessing.SetError(rowIndex, new PXException("Error Message"));

OrientDB ClassCastException

I get the following exception when trying to access a relationship document:
java.lang.ClassCastException: com.orientechnologies.orient.core.id.ORecordId cannot be cast to com.orientechnologies.orient.core.record.impl.ODocument
via:
Collection<ODocument> field = myDoc.field("MY_FIELD_NAME");
if(field != null) {
return field;
} else {
return Collections.emptySet();
}
The strange thing is that is happes not always, most of the time it works like expected.
Depending by what the field contains, you could use the interface OIdentifiable instead of ODocument.
Try using:
Collection<OIdentifiable> field = myDoc.field("MY_FIELD_NAME");
if(field != null) {
return field;
} else {
return Collections.emptySet();
}

Cannot convert lambda expression to type 'int' because it is not a delegate type

In this c# code I need to convert the userName value from string to int type.
Is anyone know please help me. I have got error as a compilation error "Cannot convert lambda expression to type 'int' because it is not a delegate type" like this.
ShoppingCartPartRecord cartRecord = null;
try {
cartRecord = _shoppingCartRepository.Get(r => r.Username == userName);
}
catch (InvalidOperationException ex) {
if (ex.Message == "Sequence contains more than one element") {
var badCarts = _shoppingCartRepository.Table.Where(x => x.Username == userName);
foreach (var shoppingCartPartRecord in badCarts) {
_shoppingCartRepository.Delete(shoppingCartPartRecord);
}
}
}
Thank you in advance.
Without the source to your repository we can only guess at what the methods do.
From the errors you are describing the get function expects either an index into an array or an integer primary key and so is the wrong function
You should be able to change the code as follows to achieve the desired effect
ShoppingCartPartRecord cartRecord = null;
try {
cartRecord = _shoppingCartRepository.Table.Single(r => r.Username == userName);
}
catch (InvalidOperationException ex) {
if (ex.Message == "Sequence contains more than one element") {
var badCarts = _shoppingCartRepository.Table.Where(x => x.Username == userName);
foreach (var shoppingCartPartRecord in badCarts) {
_shoppingCartRepository.Delete(shoppingCartPartRecord);
}
}
}

Resources