Sometimes I just want to submit a "normal" form, but have the input fields in React (of styling reasons).
But the form doesn't seem to "see" the values of the input fields when submitting.
<form action="comments" method="post">
<label>
Name:
<input
className={styles.input}
defaultValue="Bob"
type="text"
ref={this.input} />
</label>
<input type="submit" value="Submit" />
</form>
Is there a way to accomplish something like the above, i.e. posting a form without having to create an onSubmit event handler that referes to every single field in the form?
Ps. I'm aware that the default React way is to include state, but that increases the boilerplate code even more.
use name tag in your input
e.g.
<input name="comment" type="text" defaultValue="Bob"/>
Related
I have a form, and the form has multiple inputs that are all bound to different variables. Before submitting the form, I need to do validity checks, pristine checks, etc. For example, I want my submit button to be disabled if every part of the form is pristine, or if something is invalid.
Using Angular 5, I am trying to get access to the .pristine, .valid, and .invalid flags for each input field, but the values are either undefined or "cannot get .pristine of undefined".
I am able to get these flags on the entire form itself, but this doesn't help, because I want to know how to get it for each individual input.
Here is my current code (I've removed a number of my inputs to simplify the example).
<form #editDetailsForm="ngForm" name="editDetailsForm" >
<label for="name"> Name </label>
<input type="text" id="name" name="name" maxlength="40" [(ngModel)]="myName" required />
<label for="description"> Description </label>
<textarea id="description" name="description" maxlength="250" [(ngModel)]="myDescription" required ></textarea>
<button id="submit" type="button"
[disabled]="saveButtonDisabled(editDetailsForm.invalid, editDetailsForm.name.invalid, editDetailsForm.description.invalid)"
(click)="updateDetails()" >
Save
</button>
</form>
If you see, I bind disabled attribute on the Save button to saveButtonDisabled() function, where I want to pass in information about each input's validity. The first argument, editDetailsForm.invalid returns a true or false, but the other values return undefined.
How do I check validity of these individual inputs?
EDIT: I realize I can derive all of this info inside my component because all of the input values are bound. However, it'd be easier just to check a flag or two.
I'm not sure I totally understand what you want to do, but this is how you get access to the form controls .pristine, .invlaid
<input type="text" id="name" name="name" #name="ngModel" maxlength="40" [(ngModel)]="myName" required />
The #name="ngModel" sets a template reference to the FormControl angular creates
Then you should be able to do something like this:
<input type="text" id="name" name="name" #name="ngModel" maxlength="40" [(ngModel)]="myName" required />
<div *ngIf="name.pristine">
Name is Pristine
</div>
Just to clarify, the individual form fields bubble up to the form itself. So if any field has been touched, then the whole form will be pristine == false.
You can access the input controls using the .controls property, like:
<button id="submit" type="button"
[disabled]="editDetailsForm.controls.name?.invalid || editDetailsForm.controls.description?.invalid">
Created a stackblitz. https://stackblitz.com/edit/angular-5ir4k7
Added template reference variable for ngModel and validate using isValid.
I am disabling the inputs using the isFetching prop,
but this is getting reduntant as I have to keep this in every input field.
Is there a way to disable the entire form?
Like a disable property in <form> tag or something?
<form>
<input type="text" disabled={this.props.isFetching} />
<input type="text" disabled={this.props.isFetching} />
</form>
I think this should solve your problem https://stackoverflow.com/a/17186342/3298693.
You should insert your form inside an element <fieldset disabled="disabled">. This will make the whole form disabled.
I had the same issue and this worked for me:
<fieldset disabled={true}>
Where true would be some "prop.setting"...
Just use <input type="text" disabled> wherever you want the input text to be disabled. It hardly takes some time.
When I submit form by using below function it is submitting but values are not passed through this function. I use all functions but nothing found:
document.getElementById("postad").submit();
Form is given below.
<form action="register.php" id="postad" method="post">
<input class="textfield2" type="text" id="post_title" style="width:640px;" placeholder="Ad Title" onBlur="check('post_title')" />
<input class="button" type="button" name="save" value="Publish" onclick="send();" />
</form>
Your form contains two form controls. Neither will be a successful control (i.e. one that appears in the submitted data), but for different reasons.
Only form controls with name attributes can be successful. Your text input doesn't have a name. (It also doesn't have a default value, so you need to type in it first).
Buttons can only be successful if they are the submit button used to submit the form. Your button isn't a submit button and you use JavaScript to submit the form.
There is no name attribute in your input text fields
<input name="post_title" class="textfield2" type="text" id="post_title" style="width:640px;" placeholder="Ad Title" onBlur="check('post_title')" />
.........^
I have applied ParsleyJS validation to a form that has multiple submit buttons that perform various tasks. I only want the validation to occur for one of the submit buttons, is this possible? if so, how?
For a pared down example:
<form name="aspnetForm" method="post" action="page.aspx" id="aspnetForm" data-validate="parsley">
<input type="text" id="text_for_example" data-required="true"/>
<input type="text" id="text_for_example2" data-required="true"/>
<input type="text" id="text_for_example3" />
<input type="submit" id="ClearsTheTextBoxes"/>
<input type="submit" id="SavesData" />
</form>
Ideally I want it to validate only on the "SavesData" submit, not on the "ClearsTheTextsBoxes". is this possible using ParsleyJS?
Thanks!
Note:
I cannot change the type any of the submit buttons to function differently; please do not suggest this. The "ClearsTheTextsBoxes" must remain a submit button.
to do so, you'll have to remove data-validate="parsley" from your form tag, and add a custom js function on click on the desired button. Then in this function, simply to a $('aspenetForm').parsley('validate');
Best
I'm relatively new to AngularJS and I am trying to sumbit a regular form. I have basic form that looks like this:
<form method="post" enctype="multipart/form-data">
input type="text" class="title span5" name="post_title" placeholder="A catchy title here..." value="" />
<input type="file" name="post_image" />
<input type="submit" class="btn btn-success" value="Create Post" />
</form>
But I noticed that AngularJS adds its own values to the form.
<form method="post" enctpye="multipart/form-data" class="ng-pristine ng-valid">
And I am ununable to submit the form. How can I disable the automatic validation that Angular JS is adding to the app?
Quoted from the documentation:
For this reason, Angular prevents the default action (form submission to the server) unless the element has an action attribute specified.
IMHO, you should read the doc to gain some general understanding of single page application, and the reason why angular's preventing the default behavior.
UPDATE : This does NOT work ... well at least not in a way you'd like it to. Adding ng-non-bindable to the form or any input breaks ALL binding. So, your ng-model in the inputs won't work anymore.
ng-non-bindable is possibly your best choice. It will prevent AngularJS from doing ANY validation. So, you'll be responsible for showing invalid and checking validity.
https://stackoverflow.com/a/19387233/75644