I am beginning with struts2 programming and I wondered how I could do the following thing. I have this struts code in a form :
<s:textfield name="aName"/>
And I had this html code before using struts2 :
<input id="aLogin" type="text" class="form-control" name="username" value="" placeholder="something" required autofocus>
How could I "merge" these two lines to do the same html code but using my struts2 code ?
In Struts tags, class ans style becomes cssClass and cssStyle; in old versions of Struts, required was an attribute indicating when to put the * mark, now changed to requiredLabel to avoid overriding the HTML5 required attribute. Other HTML5 attributes like placeholder and autofocus can be set because Textfield tag Allows Dynamic Attributes.
Then in your case:
<s:textfield id = "aLogin"
name = "aName"
cssClass = "form-control"
value = ""
placeholder = "something"
required
autofocus />
For other info, refer to the official <s:textfield/> documentation
name should be same as that of bean variable,therefore,
<s:textfield name="username"/>
this will work, only if you have setUsername method in your action class.
Related
I have a webpage that I need to fix for ADA compliance. There are a few elements that share ids. The form on the page uses request() to grab information, but it doesn't appear to be the Request object. I'm trying to figure out if this request function? object? uses the name or id attribute. I'm hoping that it's the name attribute.
Here's an example of the ASP code:
if request("submit") = "Submit" then
session("firstName") = request("firstName")
session("middleInitial") = request("middleInitial")
session("lastName") = request("lastName")
end if
Here's the HTML:
<label>First Name: <input type="text" name="firstName" id="firstName" value="<%=session("firstName")%>"></label>
<label>Middle Initial: <input type="text" name="middleInitial" id="middleInitial" value="<%=session("middleInitial")%>"></label>
<label>Last Name: <input type="text" name="lastName" id="lastName" value="<%=session("lastName")%>"></label>
The id is only used client side (primarily for linking, JS, CSS and the for attribute).
The name is used to describe the data that will be encoded in the form submission (so this is what is available to your server side code).
Using Angular 2 (2.0.0), what is the recommended way to dynamically mark a field as required, using Angular Forms?
In all of their examples, the required attribute is just added like:
<input type="text" class="form-control" id="name" required>
What if the model I'm binding to has an IsRequired property, that will be true/false?
If I use something like:
<input [(ngModel)]="field.Value" type="text" value="{{field.Value}}" [attr.required]="field.IsRequired"/>
That renders on the page like (note the ="true"):
<input type="text" required="true" />
For some reason, Angular doesn't appear to recognize this attribute when it has an actual value (the ="true") so when this field is blank, my form itself still is valid:
<form class="ng-untouched ng-pristine ng-valid">
So it would appear that I must use required and not required="true", but how can I add that attribute in dynamically?
What also doesn't work:
<input type="text" {{ getRequiredAttr(field) }} />
Thought I might be able to have a function that returns my string "required" based on the field, that just gives templating errors.
Is there a way to accomplish this and render only required for my attribute? Or a way to make Angular recognize this attribute when it has a value of true/false?
FWIW - I've verified that I can use *ngIf to write two near-identical <input type='text' /> controls based on my IsRequired property and hardcode one with the required attribute but that seems pretty hacky. Hoping there's a better way!
Why do you have to make it so complicated when you can simply do this,
[required]="isFieldRequired() ? 'required' : null"
The basic forms stuff is great for simple forms, but when you need more control like what you have here, that is when you need to start using the more advanced form stuff. What that would look like in your case would be something like this.
#Component({
selector: 'something',
template: `
<form #myForm="ngForm">
<input [(ngModel)]="field.Value" [formContol]="myFieldControl" type="text" [value]="field.Value">
</form>
`
})
export class MyComponent{
public field: any = {Value: 'hello', isRequired: false};
public myFieldControl: FormControl = new FormControl('', [this.dynamicRequiredValidator.bind(this)]);
public dynamicRequiredValidator(control: FormControl):{[key: string]: boolean}{
if(field.IsRequired && !control.value){
return {required: true};
}
return {};
}
}
Note: You will probably need to import the ReactiveFormsModule into your #NgModule. This comes from #angular/forms as well.
There is also another way you can do this with a directive shown here.
I would like to clarify one thing about MVC5 Razor Views.
I was told that I don't have to use HTML Helpers (#Html.EditorFor, #Html.TextBoxFor) in my razor views and it will still work.
Controller Code
[HttpPost]
public ActionResult Create(Models.TestObj obj)
{
ViewBag.TestStr = string.Format("You typed: {0} {1}", obj.FirstName, obj.LastName);
return View(obj);
}
Razor View
#using (Html.BeginForm())
{
<div>
#Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { #class = "form-control", style = "width: 120px;" } })
<input class="form-control text-box single-line" id="LastName" name="LastName" style="width: 120px;" type="text">
<input type="submit" name="submit" value="Filter" / >
</div>
#ViewBag.TestStr
}
But when I actually test it like the above, the value typed in 'LastName' textbox is not preserved. I can catch both textbox values in my Controller. But after postback, lost the value in 'LastName' textbox. The textbox which is created by using HTMLHelper didn't lose the value though.
Am I doing something wrong or is it supposed to be like that? Do I have to use HtmlHelpers in RazorViews in MVC5 if I want to keep the submitted values?
What's a "postback"? It sounds like you're trying to think of this in terms of WebForms, where the server-side controls have a lot of plumbing doing things behind the scenes.
Simply put, there is no code in your view which actually puts a value in that input. When using the HTML helpers, while the generated client-side markup is the same (and, thus, the post to the next controller action is the same), one thing that's different is that the HTML helpers will bind the control to the model that's supplied to the view.
HTML, by itself, won't do that. You can, however, do that manually:
<input value="#Model.LastName" class="form-control text-box single-line" id="LastName" name="LastName" style="width: 120px;" type="text">
^--- right here
Your not binding the second input to the value of LastName, just setting its name attribute. If you inspect the html you generating, you will note the first one has a value attribute whereas the second does not.
You need to use the EditorFor() (or TextBoxFor()) as you did for the FirstName which generates the correct value attribute (and other data-val-* attributes necessary for using client side validation).
Side note: You could also use
<input name="LastName" ..... value="#Model.LastName"` />
however this will not take into account ModelState values
You need to add the textbox in your model, and the link it in the view index.cshtml like this: m = m.Variable_name_from_your_model
Check out this site:
https://mvcstepbystep.wordpress.com/how-to-update-a-c-mvc-textbox-from-jquery/
i'm currently using readonly for html inputs,.. but in some of html version (I think so) it can be editable,.. is there any other option instead of readonly to make it noneditable?
my input tag is
<input type = "text" id = "plan_amount" readonly>
`
Thanks in advance
You can use disabled attribute: <input disabled>.
And you can add an <input type="hidden"> if you want to POST the value.
when I add the name attribute to a field form in Yii , the field content is not saved in DB
the following works,
echo $form->textField($model,'country'); ?>
it generates the html code
<input name="RegistrationForm[country]" id="RegistrationForm_country" type="text" maxlength="50" />
the following does not work,
echo $form->textField($model,'country', array('name'=>'country'); ?>
it generates the html code
<input name="country" id="country" type="text" maxlength="50" />
Any idea?
The field name="RegistrationForm[country]" is required if you are using
$model->attributes = $_POST['RegistrationForm'];
to set the attributes in the controller.
If you want to use a custom name like name="country", you will have to manually set the value of the model yourself:
$model->attributes = $_POST['RegistrationForm'];
$model->country = $_POST['country'];