How to do form validation in drupal? - drupal-6

How I validate form fields? For example, I want to ensure that two text boxes have the same value.

Implements the hook_form_alter() to add a validation callback (throught the #validate) argument.
In this callback you will have at your disposal the two values of the fields, you will simply have to had a statement to check the values and display an error message if the statement is not good.
Example:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'myform') {
$form['#validate'][] = 'myvalidation_function';
}
}
function myvalidation_function($form, &$form_state) {
if ($form_state['values']['field_a'] != $form_state['values']['field_b']) {
form_error('field_a', t('Field A and B must have the same values'));
}
}

Related

LARAVEL 9: How to export data to excel based on filter data (id, month and years) using maatwebsite

I'm making an export data to excel based on employee_id filter, month and year of absence:
.
when the filter is submitted the data appears in the table below:
I have managed to get the data, but when the Download Excel button is clicked, the contents are just empty Excel, like this:
the data does not enter the excel.
My Controller:
public function rekapabsensiExcel(Request $request)
{
$idkaryawan = $request->id_karyawan;
$bulan = $request->query('bulan',Carbon::now()->format('m'));
$tahun = $request->query('tahun',Carbon::now()->format('Y'));
// simpan session
$idkaryawan = $request->session()->get('idkaryawan');
$bulan = $request->session()->get('bulan');
$tahun = $request->session()->get('tahun',);
// dd($idkaryawan,$bulan,$tahun );
if(isset($idkaryawan) && isset($bulan) && isset($tahun))
{
$data = Absensi::where('id_karyawan', $idkaryawan)
->whereMonth('tanggal', $bulan)
->whereYear('tanggal',$tahun)
->get();
// dd($data);
}else{
$data = Absensi::all();
}
return Excel::download(new RekapabsensiExport(['data'=>$data, 'idkaryawan'=>$idkaryawan]),'rekap_absensi_bulanan.xlsx');
}
My RekapAbsensiExport.php:
<?php
namespace App\Exports;
use App\Models\Absensi;
use Maatwebsite\Excel\Concerns\FromCollection;
class RekapabsensiExport implements FromCollection
{
protected $id_karyawan;
// function __construct($id_karyawan) {
// $this->id_karyawan = $id_karyawan;
// }
public function headings(): array {
return [
"No. ID","ID Karyawan","NIK","Tanggal","Jam Kerja","Jam Masuk","Jam Pulang",
"Scan Masuk","Scan Pulang","Normal","Riil","Terlambat","Plg Cepat","Absent",
"Lembur","Jml Jam Kerja","pengecualian","Harus C/I","Harus C/O","Departemen",
"Hari Normal","Akhir Pekan","Hari Libur","Jml Kehadiran","Lembur Hari Normal",
"Lembur Akhir Pekan","Lembur Hari Libur"
];
}
/**
* #return \Illuminate\Support\Collection
*/
public function collection()
{
return Absensi::where('id_karyawan',$this->id_karyawan)->get();
}
}
What part did I go wrong? Please help
Currently, you get no response to this statement because $this->id_karyawan is null as you have not properly passed the value through.
return Absensi::where('id_karyawan', $this->id_karyawan)->get();
Above, you are passing an array of values to the Export class. But your commented-out constructor function is only configured to accept a single parameter. If we stick to single parameters, you could do something like this.
return Excel::download(new RekapabsensiExport($data, $idkaryawan),'rekap_absensi_bulanan.xlsx');
Then your export constructor would look like this.
protected $data;
protected $id_karyawan;
function __construct($data, $id_karyawan) {
$this->data = $data;
$this->id_karyawan = $id_karyawan;
}

How to conditional statements in action output in Bixby

Hi I want to go to another action by putting a conditional statement in the action output.
What should I do?
For example
action (~~) {
description (Validate items passed from javascript)
collect {
input (~~) {
type (~~)
min (~~) max (~~)
}
}
type(~~~)
output (items){
on-empty(items.item1){ // if items.item1 is empty.
replan { // go to case1Action.
intent {
goal : case1Action
value : ~~~~~
}
}
}else{ // else
replan { // go to case2Action.
intent {
goal : case2Action
value : ~~~~~
}
}
}
}
or I want to select the view according to the output value.(Actually this is the purpose of the question)
output (items){
if(items.item1 == "goFirstCase"){
// First case view
}else{
// Second case view
}
}
I think by "select a different view according to the output value" I presume you mean you want to change what shows on the screen? because a "view" actually is comprised of the dialog, layout, and conversation-drivers.
https://bixbydevelopers.com/dev/docs/dev-guide/developers/building-views.views
For majority of use cases, there's really only one result-view that will be used, and any of the three contents of a view can be changed based on your preferred conditions as the above answer suggests.
within a view you can have the three different components defined with these three blocks: message for dialog, render for layout, and conversation-drivers
using your example,
//in a result-view
message {
if (items.item1 == "firstCase") {
template-macro (firstCase-result-dialog) {//enter params}
}
}
render {
if (size(items) > 1) {
list-of (items) {
where-each (item) {
if (item == "firstCase") {
layout-match (item) {
mode (Summary)
}
// OR use layout-macro
layout-macro (firstCase-result-summary-thumbnail-card) {//enter params}
}
}
}
}
}
similar conditional work can be done on conversation-drivers of course.
In your case, you would not need on-empty in action, but use template-macro-def as briefly explained in https://corp.bixbydevelopers.com/dev/docs/dev-guide/developers/refining-dialog.dialog-macros
// template file one
template-macro-def (id1) {
params {
param (x) {
Type (Items) ....
}
// template file two
template-macro-def (id2) {
// param x is type Items
}
//view file
result-view {
match: Items(this)
// after some checking make sure is single item
// it is possible to use other condition like if (this.sub1 == 2)
if (exists(this.sub1)) {
template-macro (id1) {
param (x) { expression (this) }
}
}
else {
template-macro (id2) {
param (x) { expression (this) }
}
}
The above would be the recommended way to handle different views for same concept in Bixby.
on-empty in action would be used for the purpose of either replan or relax some search condition in order to avoid 0 result. Bixby does not support on-empty(key) {} syntax according to https://corp.bixbydevelopers.com/dev/docs/reference/type/action.output.on-empty and on-empty would only apply in your case if output(items) itself is empty and would not check any sub-property of items.
Another option.
instead to use 'on-empty' block, you can use 'throws - error'
in action model,
output (items) {
throws {
error (case1ActionError) {
on-catch {
replan {
intent {
goal : case1Action
value : ~~~~~
}
}
}
}
error (case2ActionError) {
on-catch {
replan {
intent {
goal : case2Action
value : ~~~~~
}
}
}
}
}
}
And, in js code,,
if (error condition1) {
throw fail.checkedError('case 1 error', 'case1ActionError')
} else if (error condition2) {
throw fail.checkedError('case 2 error', 'case2ActionError')
}
For fail, refer to https://bixbydevelopers.com/dev/docs/reference/JavaScriptAPI/fail#failcheckederrormessage-errorid-errorobj

Not sure how to update a struct with new information

A question concerning Bixby Studio.
I am trying to figure out how to accept text-input from the user.
I have a Filter struct with some fields such as SearchField, Genre, Platforms (Gaming consoles), and Themes (A few other entries)
By default, all of these are optional, especially with the search field. However, i would like for the user to be able to visibly see what filters are enabled, and be able to select and change their values (This values can be overwritten by NLP training, but I can't figure out how to disable the field.)
I created a result view for my filters and I've setup input-cells for selecting a specific field to modify. (In this case, SearchField.). I have been successful in redirecting to an input-view, but it seems that no matter what text I put in here, it does not save or apply to my filter.
Looking for some insight into the problem and willing to provide more information as needed.
Some of the things that I have tried in the past, seem to want take the existing context "SearchField" within the filters (which might not exist) and apply it to the new "search field". However, this doesn't work and seems to create a loop.
I've also tried to set the prompt-behavior (AlwaysSelection) in the action model for SetSearchField, but it appears to do nothing.
// Result View for Filters
result-view {
match {
Filter(this)
}
message {
template (Active Filters){
speech (Would you like to change any filters?)
}
}
render {
layout-macro (filter-details) {
param (filter) {
expression (this)
}
}
}
}
// Layout Macro
layout-macro-def(filter-details) {
params {
param (filter) {
type (Filter)
min (Required)
max (One)
}
}
content {
section {
title (Filters)
content {
input-cell {
label (Search Name)
value ("#{value(filter.name)}")
on-click {
intent {
goal: SetSearchField // <-------- Field in question
}
}
}
}
}
}
}
// Input-view for SearchField
input-view {
match {
SearchField(searchField)
}
render {
form {
elements {
text-input {
id (val)
type (SearchField)
required (true)
}
}
on-submit {
goal:SearchField
}
}
}
}
// SetSearchField action
action (SetSearchField) {
description (Sets the name in a search filter)
type (Fetch)
collect {
input (newSearchField) {
type (SearchField)
min (Required)
prompt-behavior (AlwaysSelection)
}
}
output (SearchField)
}
// SetSearchField endpoint
action-endpoint (SetSearchField) {
accepted-inputs (newSearchField)
local-endpoint ("filters/SetSearchField.js")
}
// .js file
module.exports.function = function setName (newSearchField) {
return newSearchField
}
I discovered there is a special way in accessing input form elements for input-views.
Collect input through the form -> elements, then reference them using the viv.core.FormElement(id)
input-view {
match {
SearchField(searchField)
}
render {
form {
on-submit {
goal: SearchField
value: viv.core.FormElement(text)
}
elements {
text-input {
id (text)
type (SearchField)
label (Search for: )
value("#{raw(searchField)}")
}
}
}
}
}

Filtering doesn't work after a while

I'm using in my app this example to let the user search/filter a table.
When you run the application, it works perfect.
The problem is when I left the program opened, minimized, and after a while (10-15min) I try to use it again. It's then when the search/filter doesn't work at all.
Here is the code:
#FXML
private void initialize() {
// 0. Initialize the columns.
firstNameColumn.setCellValueFactory(cellData -> cellData.getValue().firstNameProperty());
lastNameColumn.setCellValueFactory(cellData -> cellData.getValue().lastNameProperty());
// 1. Wrap the ObservableList in a FilteredList (initially display all data).
FilteredList<Person> filteredData = new FilteredList<>(masterData, p -> true);
// 2. Set the filter Predicate whenever the filter changes.
filterField.textProperty().addListener((observable, oldValue, newValue) -> {
filteredData.setPredicate(person -> {
// If filter text is empty, display all persons.
if (newValue == null || newValue.isEmpty()) {
return true;
}
// Compare first name and last name of every person with filter text.
String lowerCaseFilter = newValue.toLowerCase();
if (person.getFirstName().toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches first name.
} else if (person.getLastName().toLowerCase().contains(lowerCaseFilter)) {
return true; // Filter matches last name.
}
return false; // Does not match.
});
});
// 3. Wrap the FilteredList in a SortedList.
SortedList<Person> sortedData = new SortedList<>(filteredData);
// 4. Bind the SortedList comparator to the TableView comparator.
sortedData.comparatorProperty().bind(personTable.comparatorProperty());
// 5. Add sorted (and filtered) data to the table.
personTable.setItems(sortedData);
}
}
I tried the code and it works for me.
However, step 2 must be the last step because the filteredData.setPredicate will be effective only after the second user input.
If someone just add a single letter, then the filtering will not happen.

Groovy: how to factorize code in HibernateCriteriaBuilder?

I'm trying to factorize some groovy code inside a closure.
Here is a sample code illustrating what I want to do (see HibernateCriteriaBuilder)
base code:
def criteria = Account.createCriteria()
def results = criteria {
if(A) {
// full code section when A
}
if(B) {
// full code section when B
}
...
if(N) {
// full code section when N
}
}
Now I want to extract condition block in method to be able to use them in other criteria.
Here is the code I have now:
def criteria = Account.createCriteria()
def results = criteria {
a(criteria)
b(criteria)
...
n(criteria)
}
def a(criteria) { if(A) /* full code section when A */ }
def b(criteria) { if(B) /* full code section when B */ }
...
def n(criteria) { if(N) /* full code section when N */ }
Is there a groovy way to avoid to pass the criteria in the argument of each method ? (in other word, is there a way to get the calling context ?)
And, to extend this to other closure, how should I extract method from groovy closure ?
One possibility is to change your a, b, n methods to closures, then set their delegate property to criteria before invoking them, e.g.
def criteria = Account.createCriteria()
def results = criteria {
a.delegate = criteria
a()
}
def a = { if(A) /* full code section when A */ }
This meets your requirement of avoiding the need to pass the criteria in the argument of each method, but to be honest, I don't really see what this achieves.
The code you posted in your question is more compact and readable, in my opinion

Resources