I have a basic model :
class MyModel(models.Model):
my_field = models.CharField()
I have a basic form for this model :
class MyFrom(forms.ModelForm):
class Meta:
model = MyModel
And I have a function that does a basic lookup (a lot more complex in reality, regex etc. won't do) :
POSSIBLE_VALUES = ['aa', 'bb', 'cc', 'dd']
def lookup(some_value):
if some_value in POSSIBLE_VALUES:
# the value is OK, return a string
return some_value
else:
# constructs the 'did you mean' list of suggestions
didyoumean = [pv for pv in POSSIBLE_VALUES if pv in some_value]
# returns a list which might be empty
return didyoumean
Now, the scenario that I want is:
On the site I enter a value in the "my_field" input field and hit submit button.
If the value passes the lookup I should automatically perform the form's action.
If I get multiple possible values then I should display them to the user and no other action is performed.
If I get no answers (an empty list) I should get an error message.
Some additional requirements:
I would prefer the "did you mean" list to be displayed without having to reload the page.
If a user clicks on one of the suggestions I want to perform the form's action without an additional lookup - the value has already been checked.
I want to keep all the logic outside the view and keep it in the form or in the model. This is a must.
I want to avoid hardcoded js in the template and push it into the form if possible. It's not a must.
So I assume that it would all be distributed between this fields validation and a custom widget that would handle the "did you mean" list rendering. I just can't put it all together.
Your help is required :)
EDIT. Ad. 2 in requirements.
That is a basic feature I described. In a more advanced one I want this form to have more fields and so the "did you mean" list should be displayed along with all other fields errors (if any). Then clicking on a hint would just set the my_field's value to it's value without reloading the form. A user would have to correct other errors as well so I can't go to form's action right away. Might there be just some some flag to switch between those two options ("basic" and "advanced").
I would prefer the "did you mean" list to be displayed without
having to reload the page.
Create custom widget, which renders with JS code for checking possible values as user enters it
If a user clicks on one of the suggestions I want to perform the
form's action without an additional
lookup - the value has already been
checked.
Again, that widget, when clicked, should just submit the form.
I want to keep all the logic outside the view and keep it in the
form or in the model. This is a must.
On the form you'll have clean() method to validate everything. If, say, some bogus data passes with submit from p. 2 - you still raise validation error.
I want to avoid hardcoded js in the template and push it into the form if
possible. It's not a must.
Solved with custom widget, details.
Related
Blazor validation not support individual field validation, its only validate all fields at a time in context. if i load my page at once in separated tab or stepper, validate controls partially not all fields. Is it possible? Please give me the solution. But i achieved in jquery by $('#ShippingInfo').validate().element('#ShippingAddress_StateID');
But blazor validate all the fields when press the submit button.
eg.,
I have a module like wizard using stepper component(it may be 4 steps). Here First step having some set of controls like customer details and 2nd step having some set of controls like get contact details and so on. All the controls are corresponding to one model class with annotations but validation by step by step by Next button using stepper component. How do i achieve this?
Option 1
Divide your view model into several submodels "MySubmodel1", "MySubmodel2", etc.
Then in your wizard-like form, have multiple <EditForm> elements, each specifying the corresponding submodel, ie. in the first <EditForm> set Model=#MySubmodel1, in the second <EditForm> set Model=#MySubmodel2, etc.
Option 2
This option is based on conditional validation of your view model. You will use just 1 viewmodel for the complete wizard-like form. Add a property "StepNumber" to your view model that will identify current step (step 1, step 2, ...), and define conditional validation of corresponding fields based on the actual step.
This can be achieved eg.
by using the custom Validation attributes like "RequiredIf" discussed eg. here on StackOverflow
or
by implementing the IValidatableObject (discussed eg. here) - in the Validate() method, write a code that will yield return ValidationResult for view model fields that do not have actual state as required by current step.
Finally, in your form, adjust current step when handling your Previous/Next step buttons.
Objective:
From a Django Form, modelchoice field i am making the widget display multiple checkboxes. But for each checkbox I would like to display exactly one textbox and then submit. I need to know if the checkbox is not selected, it's id still and the possible textbox value. How do I acheive this, if it is Ajax. please elaborate. as I am fairly new to django and haven't worked much with ajax.
So you have to possible approaches here,
Simpler(but a very tardy approach):
Submit the form after user's checkbox input, process the input in views.py and accordingly serve the other part of the form on a different template. This will lead you to reloading the page and changing URL's for the same form. This approach is fine if you are doing it only to learn Django at first.
The better approach. You can use on page JavaScript/JQuery to determine whether the checkbox is ticked or no and the accordingly show the checkbox. You can do something like
if(document.getElementById('yourCheckBoxID').checked)
{
$("#FormFieldID").show();
}
else
{
$("#FormFieldID").hide();
}
If you are doing the latter, do remember to NOT set the input field as "required", as it may throw up errors when not showing the text field. Use some sort of JS form validation if you have to.
Hope this helps!
I have a following problem, which I am not sure, how to solve. I am building app's setting, where user can specify list of accounts (left TableViewConstoller). As you can see on the screen, there is a Save button, which stores new account into the database.
Everything works, except for empty Name value, which cause an exception. I need to solve it in way, where Save button would be enabled only if data pass validation (Name is not empty).
I was thinking about some event, which would monitor Account.Name property and based on it's value, Save button would be enabled/disabled.
Problem is, that I have no idea, how to achieve that.
Thanks for any suggestions.
For the data to pass validation it would need an event to be triggered first, so why don't you simply hook into the touchupinside event of the 'save' button perform some validation against the data that is being submitted and if it fails display an alert to the user? Seems like a cleaner approach to me, which would also include visual prompting. otherwise you would need the button to be set to disabled from the start and then perform a check every time one of the tableviewcells is interacted with.
The validation would be fairly simple: (obviously use an '&&' operand to validate against more than one empty textbox)
If(nameTextBox.Text.Trim().Length != 0)
{
// Great store the data
}
else
{
UIAlertView alert = new UIAlertView () {
Title = "alert title", Message = "this is a simple alert"
};
alert.AddButton("OK");
alert.Show ();
}
If you have a code example I may be able to be more specific.
Edit:
It's also worth considering your future requirements, should you be able to handle no name being passed, if the user doesn't have one already stored should you force them to supply one, or if the user already has a name and passes through no name change, should you simply keep the existing name and proceed to update all of the other fields that the user may have interacted with. It always pays to take some time to architect out your requirements for any given feature.
On an EWF page, is it possible to alter the content of a form item during validation (when a validation fails)? For an example: say you have a text box that you want to be spell checked before it gets entered into the database. You use the modification's GetSpellCheckedWordTextFormItem to get the form item, and you want to replace what the user enters ("teh") with a likely suggestion ("the") when the validation fails to find a word it knows. Then the user sees the validation error ("Is this the word you meant?"), looks at it and corrects it or not, then re-submits.
Is there a way to do that? If so, how?
The specific answer to your question is no, you can't alter any form values if validation fails. To implement this, you'd need to let the validation succeed and let the data get modified. As part of the validation/modification, you could set a piece of page state that causes the next loadData pass to display the "is this the word you meant?" message near the spell-checked form item. Of course, you would have already saved the corrected text.
Alternatively, you could use PostBack.CreateIntermediate to make a post-back that only runs the spell-check, puts the corrected text in page state, and displays "is this the word you meant?". You'd set that post-back to fire when the user tabs out of the text box, and then you'd have the main post-back grab the corrected text from page state and save it in the database or other durable storage.
I have a view which is containing 5 forms submitting separately but to the same model. The aim of this is allow the user to fill all the forms in the same page and be able to quickly compare the values he puts in for each form. Each form has most of the fields in common with another one. If you want a idea of the stuff, imagine that you are a soccer trainer and you want to be able on the same page to set up your team tactics for each half time. So I'll have a HalfTime model with two forms on the same page, one for each half time
So when I submit Halftime 1 form and that the validation fails, the validation errors of HalfTime1 is displayed in HalfTime2 fields too. I would like to be able to disable the validation of one form according if it wasn't submited.
My idea was to send a variable from the controller to the view containing the name/id of the current half time and from this variable, displaying validation errors only in the ccorrect form.
Do you have any idea on how I can disable the display of the validation errors in one field?
Thank you a lot!
Set the error option to false when you use form input. ie:
echo $this->input('Halftime2.title', array('error' => false));