I'm looking to validate date fields on a page, which is simple (see this JSBin). BUT, when a page has multiple date fields on a page things start to get wacky...
See this JSBin and play around with invalid dates.
The invalid message doesn't know which input to bind to, causing error messages on the wrong inputs. Is there a way to trigger the correct input field?
Instead of defining a validator for the form, define a validator for each date as actually you want to validate the fields and not the form as a whole. You can do it as:
$(".datepicker").kendoDatePicker();
$(".datepicker").kendoValidator({
rules : {
//implement your custom date validation
dateValidation: function (e) {
console.log("e", e);
var currentDate = Date.parse($(e).val());
//Check if Date parse is successful
if (!currentDate) {
return false;
}
return true;
}
},
messages: {
//Define your custom validation massages
required : "Date is required message",
dateValidation: "Invalid date message"
}
});
Your JSBin modified here
Related
I have a form with a modal and one of the fields in the modal form is a date field. It is cast in the Model as:
'date_last_contact' => 'date:m/d/Y'
In the $rules section of the livewire file it is set as:
'editing.date_last_contact' => 'date|nullable',
The issue is if I someone inputs a non-date, non-null value in the field and tries to save, it throws an error because it is not validating…
Carbon\Exceptions\InvalidFormatException
Could not parse ‘adff’: DateTime::__construct(): Failed to parse time string (adff) at position 0 (a): The timezone could not be found in the database
The Save function in the livewire file looks like this:
public function save()
{
$this->validate();
$this->editing->save();
$this->showEditModal = false;
}
What it seems is happening it is trying to CAST it to a date before the validation is happening. How can this be prevented?
Versions:
Laravel: 8.24.0
Livewire: 2.3.8
I'd use date format over date, enforce the structure on the BE, as well as add some front end validation to help hold the user's hand
https://laravel.com/docs/9.x/validation#rule-date-format
Try casting the date field before submiting the form.
I have more than 50 fields those are input text and dropdowns in the reactive form. The fields are dependent to each other's value changes in order to trigger validation and to display related field after the selection.
I subscribed to the value changes in ngOnInit() as below:
ngOnInit() {
this.setPageValidation();
}
setPageValidation() {
this.NameSubscription = this.FormGroup.get('personnel').get('name').valueChanges.subscribe(data
=> {
this.enableOrders();
});
this.StateSubscription = this.FormGroup.get('personnel').get('state').valueChanges.subscribe(data
=>
{
this.enableAccount();
});
// more value changes subscription like 40 fields ............................
}
While loading the form, it is taking longer time to load due to subscribing for the value changes when the form loads.
I tried implementing it to move the code to ngOnChanges() but it is not triggering the enable and display of other fields depending on it's initial value that are filled from the table if there are values for those fields. It is just populating the first field and the rest does not display depending upon on its value.
I would like to thank you in advance. I really appreciate your help if there is any best approach to it to resolve without performance issue.
You can do with a single subscription.
this.personnelSubscription =
this.Formgroup.get('personnel').valueChanges.subscribe(data => {
if (data) {
//Console log the data here. It will print the formGroup of personnel
// then select the control and add your validations
// like this data.controls.state
}
})
[HttpPost]
public JsonResult GetSearchedResults(string searchedHotel)
{
var searchingHotels = clsRepository.GetSearchedResults(searchedHotel, );
return json etc etc
}
This is my Controller. Just Like String, I need to pass Date Datatype. I have A field Name in DataBase of Type Date.Now I need to pass this.how to pass a datatype DATE.?
I have a DatePicker is my main page. When i choose A date from Datepicker, that will be saved in a variable in my Jquery file. so I need to pass this variable to controller.i need to check whether selected date and date in DataBase are equal.
Just change the datatype of your action parameter to DateTime. The default modelbinder will try to convert the value of the sent request parameter to a DateTime value.
public JsonResult GetSearchedResults(DateTime myDateParameter)
{
// do whatever you want with your date
}
Make an ajax POST request to your action with the date as a parameter.
Use jQuery to make the request. See the dosc on how you can do it: http://api.jquery.com/jQuery.ajax/
I'm using Sharepoint 2010 .. with a custom field in visual studio 2010.
I created a custom field. This particular one is a datetime field ("Termination Date"). I want it to fail validation if it is blank and another field ( "Contract Terminates" is equal to yes ).
So I had previously did this with a calculated field. And that works but it puts the validation error at the top of the edit form, not next to the "Termination Date" field where I want it.. like it would normally be if the field failed validation using GetValidatedString in a custom field.
So because it's in the wrong place, I made a custom field. But because the date is blank, it never hits GetValidatedString method. Am I missing something? is there another way to have it fail validation and be next to the 'Termination Date' field if the 'Termination Date' field is blank?
I'm tried using an event receiver solution also.. the problem there is that it would also put the error message on the top.. not next to the Termination Date field.
Suggestions?
For custom field you could override FieldRenderingControl, write your own FieldControl. If you don't use this custom field in Whereabouts list you could inherited your fieldcontrol from DateTimeField and override Validate method e.g:
public override void Validate()
{
base.Validate();
if (IsValid)
{
if (!(your validation))
{
IsValid = false;
ErrorMessage = “youe message”;
}
}
}
My model looks like
public class Template
{
Id
Title
List<Field> Fields
}
The “Field” Entity contains information like Name, Caption, Type (TextBox/Select/Radio), Options, and validation rules (Range, Required, string length).
The standard validation in MVC is based on DataAnnotations, but I wants to validate (Both client and Server Side) the form dynamically based on Field Metadata which is dynamic and configurable.
Is it possible? Any pointers?
PS. I searched for the similar questions, but not able to find a solid answer.
I had a similar situation, this is how I handled it:
Server Side
When the POST happened I iterated over all the Fields values and did the Validation based on the validation rules I had on my objects. Then you can simply add ModelErrors to the Field object.
Since you push a Template object to the View you can access the Fields by name Fields[x].SomeProperty. Make sure you have a ValidationMessageFor for SomeProperty
ModelState.AddModelError("Fields[x].SomeProperty", "The Error Message you want to show.);
Client side
Make sure your form has an Id so you can access the Validate method().
Then you iterate over all the fields and just add the validation as you please.
For all the validations rules check the validation Jquery documentation.
$('#frmYourForm').validate();
for (var i = 0; i < 'CountOfAllFields'; i++)
{
$('#Fields_' + i + '__Foo').rules('add', { required: true, messages: { required: 'The Foo field is required'} });
$('#Fields_' + i + '__Bar').rules('add', { required: true, messages: { required: 'The Bar field is required'} });
}
I hope I helped you on your way !
Ps, use FireBug to help you find the correct names of the properties and that's how you can link them with the ModelErrors in the modelstate etc.