Custom Validation on Inline Edit.

After hours of re-search I find 1 one to do it.

In a custom js file I’ve put some code to detect inline edit :


$('div.detail-view-row-item').dblclick(function(e) 
{
	if ($(this).children().hasClass('inlineEditActive'))
	{
		var ElementCourrant = $(this).children('div').eq(1);
		var ValueElement = (ElementCourrant).children('form').find('input').attr( "value" );

		$(ElementCourrant).css('white-space','inherit');

		//Touche entrée  -> voir inlineEditSaveButton !
		$(document).keypress(function(e) 
		{
			if (e.which == 13 && !e.shiftKey) 
			{		
				var TelPrincipale = $('#phone_office').val();

			   //Téléphone Principale
			   var RegexTelephone = /^(0)[1234567789]([\s]\d{2}){4}$/;
			   if(TelPrincipale != "") //Si pas vide
			   {
					if(RegexTelephone.test(TelPrincipale)) //Si tel pas vide -> on check 
					{
						//Tous ok -> save
					}
				   else
					{
						$(ElementCourrant).removeClass('inlineEditActive');
						$('#phone_office').css("background", "red");
						alert("Le téléphone principale n'est pas valide. Format attendus : 0X XX XX XX XX. N'oubliez pas les espaces !");
						$('#phone_office').focus();
                                        }

With only this code you will be able to alert the user about the error, but the original code will be performed, so the error will be saved in the data base.

To counter that I’ve created a before_save hook :


//Téléphone principale
		if(preg_match("/^(0)[1234567789]([\s]\d{2}){4}$/", $bean->phone_office)) 
		{
			// phone is valid
		}
		else
		{
			throw new Exception("Le téléphone principale n'est pas valide. Format attendus : 0X XX XX XX XX. N'oubliez pas les espaces !");
		}

With this code, the error is not saved, It will keep the old value in the database.

The bad thing is that the validation is performed 2 times, in java and in php…

Would love to have some pro’s feedback on this. (@PGR ?)

1 Like