I have a custom field in Incident table called 'u_custom_long_description' In UI policy i have written a script to check if the field is empty or not at the time of closing the incident.
function onCondition() {
if(current.u_custom_long_description == null){
alert('Please provide the description for the close comments');
}
}
and I am getting the following error when i close the incident
onChange script error: ReferenceError: current is not defined function
(){var o=i(m,arguments);return l.apply(n,o)}
You are in a client-side script where the server-side variable current does not exist. To get the value of a field on the form, you can use g_form:
function onCondition() {
if(g_form.getValue('u_custom_long_description') == ''){
alert('Please provide the description for the close comments');
}
}
Also, alert is so 90s! You'd make for a better experience by using something like showFieldMsg. Take a look at this section in the docs about validating field input client side which has an example similar to what you're trying:
https://docs.servicenow.com/bundle/jakarta-servicenow-platform/page/script/client-scripts/concept/client-script-best-practices.html#ariaid-title6
Related
here's a simple custom validator in angular 2
hasUpperCase(control:FormControl):{[s:string]:boolean}{
if (/[A-Z]/.test(control.value) === true { return null }
else{ return {noUpperCase:true} }
}
How can I access the else return, so that I can prompt the user that the input has no upper case?
Thanks!
Get a reference to the form (I can't see from your code how you use forms or what version) (or get a reference to the control directly), look up the control the validator is applied to read it's errors property. It contains all the error objects returned by all failed validators.
I'm trying to clear a Flash Message in CakePHP 3.1.1
I have a function when a user logs in, if his customer data is not complete, he is redirected to a form to complete it. That looks like this:
public function index()
{
//Some code to check whether the customers profile is complete
//If it's not complete, then redirect to the "complete" action with a flash message
if($completion == 0){
$this->Flash->success(__('Please complete your customer profile data.'));
$this->setAction('complete', $custid);
//Otherwise go to their view
} elseif ($completion == 1){
$this->setAction('view', $custid);
}
}
This works fine, and user is redirected to the Complete action/Form with the Flash Message.
Then the Complete action looks like this:
public function complete($id = null)
{
//Get all the customer data input
if ($this->request->is(['patch', 'post', 'put'])) {
$customer = $this->Customers->patchEntity($customer, $this->request->data);
//Set the completion status to complete (1)
$customer->completion_status = 1;
if ($this->Customers->save($customer)) {
$completion = 1;
$this->set('completion', $completion);
//After the Save, redirect to the View action with a new Flash Message
$this->Flash->set(__('Your customer information is complete and has been saved!',['clear'=>'true']));
return $this->redirect(['action' => 'view',$custid]);
} else {
$this->Flash->error(__('The customer could not be saved. Please, try again.'));
}
}
$this->set(compact('customer'));
$this->set('_serialize', ['customer']);
}
It work fine BUT: When the user is redirected to the View action with the Success Flash after saving their data, the Flash from the Index (telling them 'Please complete your customer profile data.') still shows up again.
If the user refreshes on the view, then both Flash messages go away as they should.
How can I clear that initial Flash message when redirecting? I've tried using the clear key but it seems to not be working.
Any advice is greatly appreciated!
Thanks,
DBZ
You can add this in the beginning of your action
$this->request->session()->delete('Flash');
to delete more specific you can do e.g. to delete only the messages from AuthComponent
$this->request->session()->delete('Flash.auth');
One more thing:
you can handle which flash-messages are displayed in the view like:
this->Flash->render('auth');
or
this->Flash->render('error');
If you don't display a flash-message its kept in the session until you display it somewhere or remove it from session.
Flash message are stored in session, so just clear the relevant session key: $this->Session->delete('Flash.flash') or $this->Session->delete('Flash')
Check to make sure that you aren't always getting $completion == 0 (which could match FALSE as well).
I suspect this is why your flash message is always showing.
Cake will automatically delete a flash message after it displays it.
Apparently you are passing a string to clear instead of a boolean:
New in version 3.1: A new key clear was added. This key expects a bool and allows you to delete all messages in the current stack and start a new one.
Try setting true without the quotation marks:
$this->Flash->set(__('Your customer information is complete and has been saved!'),[
'clear'=> true
]);
New in version 3.1: Flash messages now stack. Successive calls to set() or __call() with the same key will append the messages in the $_SESSION. If you want to keep the old behavior (one message even after consecutive calls), set the clear parameter to true when configuring the Component.
Use like this:
$this->loadComponent( 'Flash', ['clear' => true] );
We have a two state field called Primary that is set to either yes or no. When the field is set to no, the user should be able to change it to yes. When the field is set to yes, it should be disabled, so the user can no longer change it.
We have code in the onload event that handles this; that works just fine. The challenging case is the one where a user changes the field from no to yes and then saves the form. This should lock the field so the user can't change it back to no. We attempted to solve this by putting the following code in onsave event:
export function onSave() {
var primaryControl = Xrm.Page.getControl(
d.ConstituentAffiliation.AttributeNames.Primary.toLowerCase());
if (primaryControl) {
if (primaryControl.getAttribute().getValue()) {
primaryControl.setDisabled(true);
}
else {
primaryControl.setDisabled(false);
}
}
}
This partly works. It does disable the field so it can no longer be changed. The save doesn't work, however, because Dynamics CRM appears not to send the values of disabled fields back to the server during the save, so the new value does not actually get saved.
Any ideas would be welcome. :)
It seems the following line solves my problem:
Xrm.Page.getAttribute(d.ConstituentAffiliation.AttributeNames.Primary
.toLowerCase()).setSubmitMode("always");
So the code now reads as follows:
export function onSave() {
var primaryControl = Xrm.Page.getControl( d.ConstituentAffiliation.AttributeNames.Primary.toLowerCase());
if (primaryControl) {
if (primaryControl.getAttribute().getValue()) {
Xrm.Page.getAttribute( d.ConstituentAffiliation.AttributeNames.Primary.toLowerCase() ).setSubmitMode("always");
primaryControl.setDisabled(true);
}
else {
primaryControl.setDisabled(false);
}
}
}
I should credit this blog which was very helpful: http://blogs.msdn.com/b/arpita/archive/2012/02/19/microsoft-dynamics-crm-2011-force-submit-on-a-disabled-field-or-read-only-field.aspx
Looks like you've solved it however I was curious. Have you tried using a Business Rule? This kind of basic functionality is what Business Rules in CRM 2015 can handle quite well.
For example something like this:-
okay, so I am trying to load a user's information into form fields after the user has finished creating an account. This is sort of a "re-check your personal info" but here his previously entered values are already set. How should I do so using ajax. I want to use the prototype ajax to avoid obtrusive coding.
Again,these are the steps
1.User creates an account on signUp.php
2.He is redirected to edit.php where he checks his personal info which is set by default.
if I do this
function setFormData()
{
new Ajax.requestHTML("edit.php")
{ onSuccess:someFunction()
});
}
then how can I set the form values, like
function someFuncton(ajax)
{
$("firstname").value=??
$("lastname").value=??
$("country").value=??
}
you can in the edit.php output an xml, json or string with the values, parse it and use in your someFunction
I have a situation where I'm editing a snippet of data within a larger context. The user submits this data to a specialized action for handling and redirects back to the parent page. Because it's a redirection, validation errors aren't getting automagically set, so I'm trying to work around that.
In the event of an error, I'm writing a validation_errors key to the session with a value of $model->validationErrors. In the form, though, I'd like to tell Cake to set each error so I can leverage my existing styles and not have to make a lot of changes to my $this->Form->input() methods.
Is something like this possible? Essentially, I'm looking to manually achieve the same result you'd get if a regular form was submitted and allowed to drop through with validation errors. I was hoping I could loop over each validation error and set the field error, but that's not making any change at all.
Thanks.
This can be achieved in the controller by
$this->Model->invalidate('fieldName', __('ErrorMessage', true));
If the values are available, you can also call
$this->Model->validates();
to validate all values with the validators defined in the model.
Save the data to the session and revalidate it.
function childAction() {
if(isset($this->data)) {
$this->Session->delete('invalid_data');
if($this->Test->save($this->data)) {
// ...
} else {
$this->Session->write('invalid_data', $this->data);
}
$this->redirect(array('action'=>'parentAction'));
}
}
function parentAction() {
if($this->Session->check('invalid_data')) {
// This will cause $this->Test->validationErrors to be populated
// Assuming your parent page has the form set up properly, the
// errors will be automagically filled. ie: $form->input('Test.field1')
$this->Test->set($this->Session->read('invalid_data'));
$this->Test->validates();
}
}
If you want to do the same with CakePHP 3, use the method "errors".