Jump to content

Validations not working


deepa

Recommended Posts

Hi,

 

 

My question is:

 

I have added 2-3 labels for saving address field for each multistore in backend. These fields get saved in separate table that i created(ps_shop_address).

 

To achieve this, i have changed renderForm() function in AdminShopController and added the below lines in afterAdd() function of AdminShopController

 

$object = new ShopAddress();
        $this->copyFromPost($object, "shop_address");
        $this->beforeAdd($object);
        if (!$object->add())
        {
            $this->errors[] = Tools::displayError('An error occurred while creating an object.').
                ' <b>shop_address ('.Db::getInstance()->getMsgError().')</b>';
        }  

 

I also created a ShopAddress model at override\classes\shop location with code:

 

public static $definition = array(
        'table' => 'shop_address',
        'primary' => 'id_shop_address',
        'fields' => array(
            'id_shop' =>         array('type' => self::TYPE_INT, 'required' => true),
            'address1' =>             array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'required' => true, 'size' => 128),
            'address2' =>             array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'size' => 128),
            'postcode' =>             array('type' => self::TYPE_STRING, 'validate' => 'isPostCode', 'size' => 12),
            'city' =>                 array('type' => self::TYPE_STRING, 'validate' => 'isCityName', 'required' => true, 'size' => 64),
            'id_country' =>         array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_state' =>             array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId'),
        ),
    );
 

Now the thing is if i left the address fields blank in form i get the error

 

Property ShopAddress->address1 is empty

 

And validations does not work properly.

 

FYI, I am using prestashop PrestaShop™ 1.5.6.2

 

Thanks

Link to comment
Share on other sites

  • 2 weeks later...

I saw someone on stackoverflow say you had to define the fields as public as well for it to work. so:

class ShopAddress extends ObjectModel {

public $id_shop;
public $address1;

//etc......

public static $definition = array(
        'table' => 'shop_address',
        'primary' => 'id_shop_address',
        'fields' => array(
            'id_shop' =>         array('type' => self::TYPE_INT, 'required' => true),
            'address1' =>             array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'required' => true, 'size' => 128),
            'address2' =>             array('type' => self::TYPE_STRING, 'validate' => 'isAddress', 'size' => 128),
            'postcode' =>             array('type' => self::TYPE_STRING, 'validate' => 'isPostCode', 'size' => 12),
            'city' =>                 array('type' => self::TYPE_STRING, 'validate' => 'isCityName', 'required' => true, 'size' => 64),
            'id_country' =>         array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'id_state' =>             array('type' => self::TYPE_INT, 'validate' => 'isNullOrUnsignedId'),
        ),
    );
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...