Complete newbie to web2py... I want to make the check box at the bottom of the form appear conditionally only if a user is an admin-user. How can I address the name of the field so that I can change if a a non admin user doesn't see it?
I'm using a for loop to have a looping variable to reference, not sure that i need it...
## create all tables needed by Technical Request Form
db.define_table('technical_request',
Field('uuid', 'string', default=uuid.uuid4(), readable=False, writable=False),
Field('firstname', requires=IS_NOT_EMPTY(error_message='All Fields are Required!'), label="First Name"),
Field('lastname', requires=IS_NOT_EMPTY(error_message='All Fields are Required!'), label="Last Name"),
Field('phone', requires=IS_NOT_EMPTY(error_message='All Fields are Required!'), label="Phone Number"),
Field('email', requires=IS_NOT_EMPTY(error_message='All Fields are Required!'), label="Email"),
Field('issue_name', requires=IS_NOT_EMPTY(error_message='All Fields are Required!'),label="Subject"),
Field('about_issue', 'text', requires=IS_NOT_EMPTY(error_message='All Fields are Required!'), label="Description of issue "),
Field('issue', 'upload', label="Attach Files "),
Field('request_processed', 'boolean', default=False, readable = False),
auth.signature)
for field in db.technical_request:
if field.name == 'request_processed' and auth.has_group_membership("systemadmin"):
field.readable = True
This is the check box I want to hide ....
Follow up question *
Is there a way I can conditionally add the Field?
No need to loop -- you can just access the field directly via its name:
db.technical_request.request_processed.readable = auth.has_group_membership("systemadmin")
Or just do it when defining the field:
Field('request_processed', 'boolean', default=False,
readable=auth.has_group_membership("systemadmin"))
Related
The docs for check_box_tag are as follows:
check_box_tag(name, value = "1", checked = false, options = {}) public
Creates a check box form input tag.
What is the value argument?
I have provided many different values to the value parameter in my own checkbox form, yet the checkbox seems to work the same (it works, I just want to know what value is doing?).
It seems some others are also confounded by it.
Example
These do the same thing in my application (they both work perfectly, which I wouldn't have expected):
<%= check_box_tag :approved, "hi there", user.approved? %>
<%= check_box_tag :approved, "1", user.approved? %>
The value argument refers to the boolean value in the db.
If the checkbox is unchecked then the value will be '0' and the boolean will be set to false
If you check the checkbox then the value will be '1' and the boolean will be set to true
This is the way checkboxes work in Rails and it doesn't mind if you want to pass a custom value like value='ilovemydog' or something else, it will always refer to '0' or '1'.
I don't have much experience with ERB but the method gets converted to simple HTML as
<input id="accept" name="accept" type="checkbox" value="1" />
The value attribute is used when using the input tag with forms, now when the form is submitted and the checkbox is checked, the value is passed as "accept=1"
view:
<%= select_tag 'template_id', options_from_collection_for_select(#templates, "id", "title", #template.id), class: "form-control", id: "template_select" %>
$('#template_select').on('change', function() {
<% sections = Section.where(is_template: true, template_id: selected_template_id ) %>
});
How can I use the selected item id from the tag in my sql query's template_id?
TLDR - look in the DOM for value attribute and query it with JS
You could try finding HTML input tag (with a hidden attribute) in the DOM which in turn should contain value attribute and then simply query it with JavaScript.
If the input tag is not visible near the select tag, just look for a value attribute near/inside the select tag which should contain the selected template id.
I'm working on app based on Symfony 4 with Select2 library.
In my src/Form/PostType.php file I declared field tag, where user should be able to set one of predeclared Tag or add new one (by type tag name and press enter).
$builder
->add('tags', EntityType::class, [
'class' => Tag::class,
'choice_label' => 'name',
'mapped' => false,
'expanded' => false,
'multiple' => true,
'required' => false,
]);
From the frontend side I'm using select2 library to handle with displaying tags field.
In below example fist tag was chosen from the existed entity in database, the second one should be saved in this second.
Any idea what should I changed into filed declaration to make this field valid also for new tags?
Controller is ready, only issue is to pass form validation :)
EDIT:
Relations in ORM looks like this:
class Company {
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Tag", mappedBy="companies")
*/
private $tags;
}
class Tag
{
/**
* #ORM\ManyToMany(targetEntity="App\Entity\Company", inversedBy="tags")
*/
private $companies;
}
and there is no other validation than in code above
You have set the field to be mapped = false. If a field is unmapped you have to handle form validation manually. Can you share your Entities code , any validation code if it's written?
i currently building a react app with ant-design UI. I have a problem regarding form. So far, i already able to create and submit normal form. But as i tried to create a more complex form, i faced these problem:
I want to create a form with input like this:
code: string,
name: string,
order: {
itemid: number,
qty: number
}
the 'order' part is an array, which means i can have more than 1 order in one form. I want to create that part of the form by using component in antd, where i can render the column like this:
{ title: L('Item Id'), width: 150, render: (text: string, item: any, index: any) => (
<Form.Item {...formItemLayout}>
{getFieldDecorator('order[' + index + '].itemid', {
rules: [{
required: true, message: 'Please select id!'
}]
})(
<ItemIdSelectInput/>
)}
</Form.Item>
) }
Can i use the table like that?
Also, as far as i know, component must have a 'datasource' in order to be used, while i want to create a 'Create' form where has no initial value (empty). Is there any reference or a better way to create this kind of form in antd? Thanks in advance!
I have two entities: Event and Person.
I have both gsp pages that allows me to create events and persons. In Event page, I have the possibility to select an existing person, or I can insert name, surname and phone number.
I would have a button that, if that three fields are filled, allows me to jump to create page of Person, with that three fields filled with data inserted in Event page.
I've tried with tag as follows:
<g:link controller="patient" action="create"
params="[name: eventInstance.patientName, surname: eventInstance.patientSurname,
phone_1: eventInstance.phoneNumber]">link</g:link>
but in this way parameters are empty.
Any suggestion?
EDIT:
I've tried the following:
<g:actionSubmit value="createPatient" action="createPatient"></g:actionSubmit>
and the action is:
def createPatient()
{
def eventInstance = new Event(params)
println("params are "+ params)
redirect(controller: "patient", action: "create", params: [name: "name", surname:
"surname", phone_1: "025-84569"] )
}
In this way, the Create action is performed and I see the gsp page with form prefilled with parameter passed. The fact is that params has not the values that I want to pass to create.gsp (values needed are patientName, patientSurname and phoneNumber). Infact, the println() shows me:
params are [_action_createPatient:createPatient, recurInterval:1, recurCount:,
recurType:DAILY, endTime:, recurEndOption:never, recurUntil:, startTime:, title:,
patient_textField:, healthService.id:1, healthService:[id:1], patientName:,
phoneNumber:, description:, _recurDaysOfWeek:[, , , , , , ], patientSurname:,
patient_id:, action:save, controller:event]